PowerShell Passing Local Parameters to a Remote Script via Invoke-Command - powershell

Using PowerShell 4.0. I'm trying to run a script remotely that resides on the remote server. I need to pass a variable from my local script to the remote script. I've gone over dozens of posts and tried at least 10 variations but none seem to work. My initial command was:
Invoke-Command -ComputerName $ServerList -FilePath $piDeployScript -ArgumentList $SDFEnvironment
Where $piDeployScript is the full UNC path to the script on the remote server and $SDFEnvironment is a simple string parameter passed into my script. It looks like the parameter is not getting passed to the script. Is there a simple way to do this? How far off am I?
Update: I don't write the remote scripts, they're written by other groups in the company. The only thing I know is what field(s) need to be passed and they're all positional. I don't know the actual parameter names in the remote scripts. I could get them but I'm trying to write something that will work regardless of the parameters on the remote script. I get passed a list of parameter values (in this case 1 parameter) and send that list of strings to the remote script.
Update: Per #dugas this is the test script I'm running on my own server to test the functionality
Param
(
# SDF Environment
[Parameter(Position=0)]
[string]$SDFEnvironment
)
if ($PSBoundParameters.ContainsKey('SDFEnvironment'))
{
"Script Ran Env $SDFEnvironment"|Out-File -FilePath .\$SDFEnvironment"_LogFile.txt" -Encoding ascii -Force
}
else
{
"Environment Parameter missing"|Out-File -FilePath .\"ERR_LogFile.txt" -Encoding ascii -Force
}
Update: Below is the entire section of my local script that runs the Invoke-Command:
# Run the specifed script on all computers
Write-Output ("Running command ""{0}"" on server(s) ""{1}"" for Environment ""{2}"" " -f $piDeployScript,$SDFServerList,$SDFEnvironment)
try
{
if (Test-Connection -ComputerName $SDFServerList -Quiet)
{
Invoke-Command -ComputerName $SDFServerList -FilePath $piDeployScript -ArgumentList $SDFEnvironment
}
else
{
Write-Error "Remoting not enabled on one or more of ""$SDFServerList"" "
exit 1
}
}
catch
{
Write-Error ("Error running script {0} on server(s) {1}. Check log for more detail." -f $piServerScript,$SDFServerList)
exit 1
}
and here's the output from that code:
Running command "D:\AHP\pi_exceed_presentation\pi_exceed_presentation_deploy.ps1" on server(s) "ad1hfdahp802" for Environment "INT"
D:\a5\RWS_pi_exceed_presentation\Content_Deploy\trunk\deploy\Deploy-Content.ps1
: Error running script pi_exceed_presentation_deploy.ps1 on server(s)ad1hfdahp802. Check log for more detail.
My bad on the $piDeployScript variable. It's not the UNC to the script, it's the local file system address on the remote server. Is that the problem? Is it looking for the script on my local D: drive? Did I just answer my question?
Just to test my questions above I ran this code in PowerShell from my desktop:
$s="ad1hfdahp802"
$SDFEnvironment="INT"
$fp="\\ad1hfdahp802\D$\AHP\pi_exceed_presentation\pi_exceed_presentation_deploy.ps1"
try
{
Invoke-Command -ComputerName $s -FilePath $fp -ArgumentList $SDFEnvironment
}
catch
{
write-output "error running $fp on $s for $SDFEnvironment"
}
The code returned with no error, but the remote script didn't run. I logged into the remote server via RDC and ran the remote script locally in PowerShell with and without a parameter and it worked as expected. Now I'm really stumped.

Local script:
$server='ad1hfdahp802'
$remotepath='\\ad1hfdahp802\d$\AHP\pi_exceed_presentation\pi_exceed_presentation_deploy.ps1'
$scriptPath='d:\AHP\pi_exceed_presentation\'
$SDFEnvironment='INT'
Invoke-Command -ComputerName $server -FilePath $remotepath -ArgumentList($SDFEnvironment,$scriptPath)
Remote Script:
Param
(
# SDF Environment
[Parameter(Position=0)]
[string]$SDFEnvironment,
[Parameter(Position=1)]
[string]$scriptPath
)
Write-Output "Running Script - $SDFEnvironment"
Write-Output "Path: $scriptPath"
$outpath=("{0}\{1}_LogFile.txt" -f $scriptPath,$SDFEnvironment)
"Script Ran Env $SDFEnvironment"|Out-File -FilePath $outpath -Encoding ascii -Force
Write-Output "Script Complete"
This combination works. Originally the remote script used $PSCommandPath (also tried $PSScriptRoot) to determine the output path but that doesn't work on scripts run remotely. The actual path has to be passed in if you want deterministic placement of output files.

Related

Start-process: both positional parameter and can't locate file specified

Ok, So I am working on the powershell remote deployment of software to ADUsers.
I have remote access to the computer and all, no problems there.
Right now I am at the point where I have a exe file from Ninite to just install 7zip onto the client pc just to see when it works so i can start deploying some bigger programs to it.
The guide I have found to help me deploy out the software for now looks like this:
Invoke-Command -ComputerName *REDACTED* -Scriptblock {
Start-Process C:\Users\Administrator\Documents\ninite_7_zip\Ninite 7Zip Installer.exe '/silent' -wait
}
When I do run this code, I get the error:
A positional parameter cannot be found that accepts argument 'Installer.exe'.
So I thought to myself, that it might be because of the spaces in the name. So therefore I changed it to:
Invoke-Command -ComputerName *REDACTED* -Scriptblock {
Start-Process C:\Users\Administrator\Documents\ninite_7_zip\Ninite_7Zip_Installer.exe '/silent' -wait
}
And ofcourse also changed it's name within the folder to match the "newly made" code.
But the error now changed into:
This command cannot be run due to the error: The system cannot find the file specified
Even though I use Powershell ISE, and I used it's guideboxes when writing, to enter the folder and find it, when I wrote the directory.
My only goal in this, is that I want to remotely run and complete this installer on the client PC when deployed from the DC upon which the file lies.
Anybody got a qualified guess? Or maybe even so, a solution.
Thanks in advance for your kind answers.
When I do run this code, I get the error:
A positional parameter cannot be found that accepts argument 'Installer.exe'.
You'll want to use quotation marks to qualify path names with spaces in them:
Start-Process 'C:\Users\Administrator\Documents\ninite_7_zip\Ninite 7Zip Installer.exe' '/silent' -wait
But the error now changed into:
This command cannot be run due to the error: The system cannot find the file specified
Even though I use Powershell ISE, and I used it's guideboxes when writing, to enter the folder and find it, when I wrote the directory.
ISE is not smart enough to realize that the scriptblock is to be executed on a remote computer, so it completes the path based on your local file system.
You still need to copy the executable to the remote machine in order to execute it:
# first copy the installer to remote file system
$remoteSession = New-PSSession -ComputerName $computerName
$localInstaller = 'C:\Users\Administrator\Documents\ninite_7_zip\Ninite 7Zip Installer.exe'
$remotePath = Invoke-Command -Session $remoteSession -ScriptBlock { $env:TEMP }
Copy-Item $localInstaller -Destination (Join-Path $remotePath "7zInstaller.exe") -ToSession $remoteSession
# now we can invoke the executable on the remote machine (re-using the same remoting session)
Invoke-Command -Session $remoteSession -ScriptBlock {
Start-Process (Join-Path $env:TEMP "7zInstaller.exe") '/silent' -Wait
}
# clean up
$remoteSession |Remove-PSSession |Out-Null

Unable to run the parametrized batch file on remote machine using PowerShell

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

Using PowerShell to execute a remote script that calls a batch file

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"}

PowerShell Invoke-Command using Start-Process is not creating log file

I have a command line exe on a remote server which I need to execute remotely (running on the remote server). This exe connects to the DB, and writes a log file.
I have enabled WinRM by executing these PowerShell commands:
netsh http add iplisten localip {add local ip here}
netsh http add iplisten 127.0.0.1
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*" -Force
Enable-PSRemoting -Force
This works and I was able to test by executing
Test-WSMan {computername}
I am now trying to execute the command line exe via Invoke-Command and expecting a log file entry to be created with this script.
Invoke-Command –ComputerName MyServer –ScriptBlock {
Start-Process "C:\TheRemotePath\ToTheExe.exe"
}
PowerShell does not display any errors. However, no log file is generated. I have full rights to the directory of the exe as well as the log file directory. I am able to successfully run the exe via Windows Explorer (navigate to the remote exe and double click to run).
Any idea why this does not seem to work or what tools I can use to try and diagnose?
I would try testing the path and switching to the call operator as Ansgar mentioned, i.e.:
Invoke-Command –ComputerName MyServer –ScriptBlock {
$path = "C:\TheRemotePath\ToTheExe.exe"
if (Test-Path $path) {
Write-Host -ForegroundColor Green "Confirmed access to $path!"
& $path
}
else {
Write-Host -ForegroundColor Yellow "Unable to reach $path!"
}
}
Doing those will help you diagnose where it's getting tripped up. Depending on the EXE, I've had to use different methods of starting the process from Powershell.
Change the working directory to the location of your executable before running it. I would also recommend using the call operator (&) instead of Start-Process, unless you have reason to use the latter.
Invoke-Command –ComputerName MyServer –ScriptBlock {
Set-Location 'C:\MyDirectory'
& 'MyApp.Console.exe'
}

How to remote execute an ELEVATED remote script in PowerShell

I have two servers:
serverA (windows 2003 server)
serverB (windows 7)
ServerA contains a folder with a batch file (deploy.bat) that needs to be executed from an elevated powershell prompt. In ServerA, if I run it from a normal prompt or powershell prompt it fails. If I run it from an elevated prompt it works. (run as administrator).
The problem I have is when I try to execute batch file from serverB using a remote powershell execution. I am able to execute with this command:
Invoke-Command -computername serverA .\remotedeploy.ps1
The content of remotedeploy.ps1 is:
cd D:\Builds\build5
.\Deploy.bat
I have looked a lot questions in stackoverflow about:
Execute a remote powershell (This works for me)
Execute a local powershell with elevated prompt (I can do it)
This question is about both at the same time. So the exact question is:
Is possible to execute an ELEVATED REMOTE script in PowerShell?
If you're using PowerShell 4, you can execute the command using Desired State Configuration, which run as SYSTEM:
Invoke-Command -ComputerName ServerA -ScriptBlock {
configuration DeployBat
{
# DSC throws weird errors when run in strict mode. Make sure it is turned off.
Set-StrictMode -Off
# We have to specify what computers/nodes to run on.
Node localhost
{
Script 'Deploy.bat'
{
# Code you want to run goes in this script block
SetScript = {
Set-Location 'D:\Builds\build5'
# DSC doesn't show STDOUT, so pipe it to the verbose stream
.\Deploy.bat | Write-Verbose
}
# Return $false otherwise SetScript block won't run.
TestScript = { return $false }
# This must returns a hashtable with a 'Result' key/value.
GetScript = { return #{ 'Result' = 'RUN' } }
}
}
}
# Create the configuration .mof files to run, which are output to
# 'DeployBot\NODE_NAME.mof' directory/files in the current directory. The default
# directory when remoting is C:\Users\USERNAME\Documents.
DeployBat
# Run the configuration we just created. They are run against each NODE. Using the
# -Verbose switch because DSC doesn't show STDOUT so our resources pipes it to the
# verbose stream.
Start-DscConfiguration -Wait -Path .\DeployBat -Verbose
}
Do you try to change remoteDeploy.ps1 to start CMD.EXE with elevated rights :
cd D:\Builds\build5
start-process CMD.EXE -verb runas -argumentlist "-C",".\Deploy.bat"