Calling a script file on a remote server - powershell

I have a Powershell script that I need to execute a script file (no functions) on another server. I am using invoke-command for this but I cannot seem to get this to work correctly and execute the script on the remote server.
I don't have any code examples with me but what's the way to do this?
Thanks

Related

Triggering a remote powershell script to run independently

I've created a script which calls 'dfsutil', and is available for all the members in my team. Some members don't have RSAT (dfsutil) installed locally, so I'd like to 'trigger' a .ps1 script independently of the source PC (PC1) to be run on a server (Server1) which has 'dfsutil' installed.
It's easy enough to trigger a remote PowerShell script from the source 'PC1' by dot-sourcing it from 'myscript.ps1':
. \\Server1\scripts\dfsscript.ps1
As we already know, the above will fail as PowerShell is looking for 'dfsutil' locally on PC1, rather than on the server 'Server1'.
'dfsscript.ps1' has been written so it can be run independently, without need to return the values to PC1.
I can't seem to find any documentation which shows how to initialise a remote script which only uses the values and conditions of the OS it was triggered on.
Any thoughts?
Dot-sourcing is about the scope of a PowerShell session, but doesn't include the computing environment, so what you are doing won't get the script on Server1 to run from Server2, but still act like it is on Server1 with access to all of Server1's installed apps, etc.
What you need to do is use PowerShell remoting to actually run the script on the remote server (i.e. trigger from Server2, but execute on Server1). For example:
Invoke-Command -ComputerName Server1 -FilePath <path>\script.ps1

How to run an executable through PowerShell without batch files or cmd?

I am currently using batch files to run a set of simulations. Each line in the batch file reads:
"filepath\program.exe" "filepath\simulation.file"
The quotation marks exist to bound any spaces that exist within the file paths. Without any spaces in the file paths, the quotation marks can be removed.
When I run the batch file through PowerShell, using the following command, it works fine:
.\batch.bat
The executable is run and the output is written to the host, as if I was running the same batch file in cmd.
However, I want to ditch the batch files and run the command directly through PowerShell. When I run the following, I get the program to execute, though it doesn't run properly and I don't get anything written to host. It also appears to hang until I use Ctrl+C to cancel.
& "filepath\program.exe" "filepath\simulation.file"
Could you please help me with the following?
Any resources discussing how PowerShell executes batch files.
How to run an executable through PowerShell without using cmd or a batch file and have it write to host.
Have you tried Start-Process?
Start-Process -FilePath "filepath\program.exe" -ArgumentList "filepath\simulation.file" -Wait
PS: The Parameter -Wait is optional.
Although I'm not quite sure why yet, I found out that this issue only occurred while working remotely. Whenever I was connected to the network locally the command ran just fine.
Since I plan to execute the command on PCs that are situated locally on the network, I'll leave it at this for now.
Thanks to everyone who commented!

How to call a sub script from the main script on a remote server

HI i am having problem in calling a sub script from the main script using powershell on a remote server
I have the main script which is call 1.ps1
the other script is called 2.ps1
Here are the scripts
1.ps1 contains this code &("C:\Users\test\Desktop\remotely run scripts\2.ps1")
2.ps1 contains this code new-item -type file c:\itworksagain.txt
I am running the command as follows:
invoke-command -computer "remoteServer" -filepath "C:\Users\test\Desktop\remotely run scripts\1.ps1"
I am getting an error
The term 'C:\Users\test\Desktop\remotely run scripts\2.ps1 is not recognized as the name of a cmdlet, function, ...
I should have noticed this before. You have the following in the script
&("C:\Users\test\Desktop\remotely run scripts\2.ps1")
The brackets in that line are telling PowerShell to run a subexpression called "C:\Users\test\Desktop\remotely run scripts\2.ps1" which is not a cmdlet, exe.... etc.
I think your issue might be addressed if you change the line to the following.
& "C:\Users\test\Desktop\remotely run scripts\2.ps1"
Update from comments
Your first script calls the second. The invoke command runs the first on the remote server. The script executing on the remote server calls the second script. The call is origination on the remote server so the call is relative to that server. It is looking for 2.ps1 on the remote server. You need to move that script to a central location and then call it from your first with a UNC path which powershell supports. For testing you can move 2.ps1 to the remote server and it should work.

How to run powershell script that doesn't have ps1 extension

I am using ops tool Rundeck that allows for execution of arbitrary scripts. I enter the script text in the web application widget and upon execution, Rundeck saves it to temporary file and calls interpreter. The problem is that the temporary file doesn't have ps1 extension and Powershell refuses to execute it.
Is there any way to set up Powershell to ignore extension ?
=== EDIT 2018 ===
Rundeck now has an option for this within a job definition.
I know I'm not strictly answering your questions regarding setting up PowerShell to ignore extensions, but here is one way of executing code from a textfile:
Invoke-Command -ScriptBlock ([ScriptBlock]::Create((Get-Content .\file.txt)))
This reads the contents of file.txt and converts it into a scriptblock, before executing it using Invoke-Command.

Powershell, remote script access denied to network resources

I am trying to execute powershell script remotely using invoke-command. The script relies on a configuration file which is available over the local network. The script is called in a following way:
Invoke-Command -ComputerName 192.168.137.181 -FilePath c:\scripts\script.ps1 -ArgumentList \\192.168.137.1\share\config.xml
The configuration as you can see is an xml file and it's loaded using:
$xml = New-Object XML
$xml.Load(args[0])
When the script is called locally on the machine then it runs witout any problems and reads the configuration file. However when I run it from different machine using invoke command I get
"Access to the path '\\192.168.137.1\share\config.xml' is denied"
exception, which is thrown when executing Load method.
The file is accessible to everyone with read and write permissions.
Both, machine on which the scrip should be run (.181) and the machine on which it is run physically have the same credentials, thus I do not pass them in invoke-command cmdlet.
The share machine (.1) has different credential, but this was never an issue when calling the script locally from .181.
Can you please point me in the right direction? I stuck on this step and can't find solution by myself. I tried downloading the xml string using WebClient#DownloadString method and passing credentials for the share machine but it did not help.
Thanks in advance
It is probably a double hop issue. You have to use CredSSP to delegate your credentials to the remote computer.
Try the solution mentioned here: http://blogs.msdn.com/b/clustering/archive/2009/06/25/9803001.aspx