I am trying to execute a WSF script file from a PowerShell command with an argument and can not get it to work.
The powershell script I am attempting to use that is failing is:
$rSession = New-PSSession -ComputerName server01
Invoke-Command -ScriptBlock { cscript "\\server1\filepath\script.wsf ARGUMEMT" ; return $LastExitCode} -Session $rSession
Remove-PSSession $rSession
Now, if I run the PowerShell script above with out an argument the script executes as expected Also, if I just run the Cscript portion in cmd.exe with the argument it also works as expected.
Any thoughts on what I am doing wrong when trying to call the WSF file through powershell with the argument?
Thanks,
Phil
Related
Execute the remote server parametrized batch file from PowerShell.
Doesn't throw an error nor executed command on remote machine.
$path = "D:\run\test-5.2.bat";
Invoke-Command -ComputerName testserver -Scriptblock { "$path" }
Script inside the bat file is msiexec with parameters, which shall execute through Command Prompt only.
Based on this msdn link, you can run a ps1 script file on remote computers. So if it is possible to "port" the content of the bat file in a ps1 it should work. Here is the msdn example:
Example 11: Run a script on all the computers listed in a text file
PS C:\> Invoke-Command -ComputerName (Get-Content Servers.txt) -FilePath C:\Scripts\Sample.ps1 -ArgumentList Process, Service
This example uses the Invoke-Command cmdlet to run the Sample.ps1 script on all of the computers listed in the Servers.txt file. The command uses the FilePath parameter to specify the script file. This command lets you run the script on the remote computers, even if the script file is not accessible to the remote computers.
When you submit the command, the content of the Sample.ps1 file is copied into a script block and the script block is run on each of the remote computers. This procedure is equivalent to using the ScriptBlock parameter to submit the contents of the script.
Hope that helps
$path is a string. PowerShell simply echoes bare strings instead of executing them, unlike CMD or bash. Use the call operator (&):
& "$path"
or Start-Process:
Start-Process cmd.exe -ArgumentList '/c', $path -NoNewWindow -Wait
to have PowerShell execute a string as a command. Since you say you're running msiexec.exe from the batch script using the latter may be required.
On top of that you have a scope issue. The variable $path inside the scriptblock is not the same as the one in the global scope. You can mitigate that via the using: scope qualifier:
Invoke-Command -Computer testserver -Scriptblock { & "$using:path" }
or by passing $path as an argument to the scriptblock:
Invoke-Command -Computer testserver -Scriptblock { & "$($args[0])" } -ArgumentList $path
I am having an issue with running a batch file that is located on a remote server.
I have a batch file located on a remote server that i want to run that kicks off an automated Selenium test. For simplicity, let's say the name of my batch file is mybatch.bat
I have the following code in a Powershell script located on the server:
$BatchFile = "mybatch.bat"
Start-Process -FilePath $BatchFile -Wait -Verb RunAs
If I run this PowerShell script locally on the server in ISE then it runs fine and it kicks off the selenium test which takes a couple minutes to run.
Now I want to try to execute this test from another machine by using PowerShell remoting. Let's assume that remoting is already configured on the servers.
I have a PowerShell script located on another server which has the following code segment. Assume that all of the session variables have the correct information set:
$CMD = "D:\mybatch.bat"
$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution
$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }
When this script runs, it does connect to the remote machine and create a remote session. It does look like it kicks off the batch file because PowerShell does not give me an error. But it does not wait for the full 3 or 4 minutes for the Selenium test to finish. It seems like it just times out. Also if I am logged onto the other machine, I don't see any Selenium web test running. No Selenium log files or results files are created on remote server as should be expected.
I was wondering what I could be doing wrong with my code.
Also, it seems that the server always returns the echo outputs of the batch file to my local machine. I see these random blinking white screen on ISE which looks like output from the batch file
$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }
There are 2 issues with the above code:
$command inside the scriptblock and $command outside the scriptblock are different variables due to different scopes. The variable inside the scriptblock is thus undefined and the scriptblock will simply echo an emtpy value.
Even if $command weren't undefined, the scriptblock would still just echo its value, since it's defined as a string. PowerShell does not execute strings unless you're using something like Invoke-Expression (which you shouldn't).
This should do what you want:
$CMD = "D:\mybatch.bat"
$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution
Invoke-Command -Session $TargetSession -ScriptBlock { & $using:CMD }
If you would like execute a bat file from another machine by using PowerShell Remote Session, simply enter dot and then follow by a whitespace, then enter the exact path of bat file located on that remote machine.
Example
Invoke-Command -ComputerName RemoteComputerName -Credential $credential -ScriptBlock {. "D:\Temp\Install_Something.bat"}
I am able to install the software from the Windows CMD with the following command
setup.exe -inputFile C:\my_installer_input.txt
However, I want to achieve the same above using the PowerShell script.
I tried same from the PowerShell like this
Start-Process -FilePath "C:\Matlab R2018b\setup.exe" -inputFile "C:\my_installer_input.txt" -ArgumentList "/S
and it fails to run with the obvious reason -inputFile parameter is not available for Start-Process in PowerShell.
PowerShell also runs the native commands directly from PowerShell prompt, which means your command
setup.exe -inputFile C:\my_installer_input.txt
should work directly from the PowerShell prompt.
If you are executing on a remote machine, you can run with Invoke-Command as below.
Invoke-Command -Session $session -ScriptBlock { <YOUR CODE HERE> }
or
Invoke-Command -ComputerName <remote-computername> -ScriptBlock { <YOUR CODE HERE> }
If this is on a remote machine, do something like:
Invoke-Command -Computername ‘x’ -Scriptblock {
Set-Location C:\path\to\file
cmd /c setup.exe /arg1 /arg2
}
I am trying to call a batch file remotely in a one liner PowerShell command as such:
PowerShell -ExecutionPolicy UnRestricted invoke-command -ComputerName Server1 -ScriptBlock {cmd.exe /c "\\server1\d$\testPath\test.bat"}
What I want to do is return any exit codes from the test.bat file back to my command. Can anyone give me an idea on how to do this?
(PS, for multiple reasons, I am not able to use PSExec).
Cheers
You should be able to get the exit code from cmd.exe with the automatic variable $LASTEXITCODE.
If you're interested in only that, and not any output from the batch file, you could change the scriptblock to:
{cmd.exe /c "\\server1\d$\testPath\test.bat" *> $null; return $LASTEXITCODE}
If you're already running powershell, there's no need to invoke powershell again, just establish a session on the remote computer and attach Invoke-Command to it:
$session = New-PSSession remoteComputer.domain.tld
$ExitCode = Invoke-Command -Session $session -ScriptBlock { cmd /c "\\server1\d$\testPath\test.bat" *> $null; return $LASTEXITCODE }
$ExitCode # this variable now holds the exit code from test.bat
Im trying to execute a .bat file remotely in a powershell script. The code in my script file looks something like this:
$server
$cred
# Both of these methods are strings pointing to the location of the scripts on the
# remote server ie. C:\Scripts\remoteMethod.cmd
$method1
$method2
$scriptArg
Enter-PSSession $server -Credential $cred
Invoke-Command {&$method1}
if($?)
{
Invoke-Command {&$method2 $args} -ArgumentList $scriptArg
}
Exit-PSSession
But whenever I run it I get
The term 'C:\Scripts\remoteMethod.cmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
This happens even if i switch around the order the methods are called in.
I found this question Using Powershell's Invoke-Command to call a batch file with arguments and have tried the accepted answers to no avail.
I wouldn't have expected the $method1 and $method2 variables to be available inside the scriptblock for Invoke-Command. Usually you would have to pass those in via the -ArgumentList parameter e.g.:
Invoke-Command {param($method, $arg) &$method $arg} -Arg $method2 $scriptArg
But it seems in your case the path to the CMD file is making it across the remote connection. Try this to see if the problem is related to a modified ComSpec environment variable on the remote machine:
Invoke-Command {param($method, $arg) cmd.exe /c $method $arg} -Arg $method2, $scriptArg