So I am running an Invoke-CimMethod to push an Enable-PSRemoting command to target computer. Then run an Invoke-Command using PS-Session as a parameter. The two scripts work separately, but if I run them together I keep getting this error:
Copy-Item : The runspace state is not valid for this operation.
I had to restate the $session variable like so in order for it to run. I have bolded and highlighted the line below. My question is why?
$env:hostname = 'PC1'
$Session = New-PSSession $env:hostname
$DestinationPath = "C:\windows\temp"
$SessionArgs = #{
ComputerName = $env:hostname
Credential = $credential
SessionOption = New-CimSessionOption -Protocol Dcom
}
$MethodArgs = #{
ClassName = 'Win32_Process'
MethodName = 'Create'
CimSession = New-CimSession #SessionArgs
Arguments = #{
CommandLine = "powershell Start-Process powershell -ArgumentList 'Enable-PSRemoting -Force'"
}
}
Invoke-CimMethod #MethodArgs
Invoke-Command -Session $Session -ScriptBlock { Param($Destination) New-Item -Path $Destination -ItemType Directory -ErrorAction SilentlyContinue} -ArgumentList $DestinationPath
Copy-Item -Path "\\shared\drive\foo\bar\" -Destination "C:\windows\temp\ZScaler" -Recurse -force
############Restated here#############
$Session = New-PSSession $env:hostname
############Restated here#############
Invoke-Command -Session $session -ScriptBlock {
$msbuild = "C:\Windows\Temp\Installer\Installer.msi"
$arguments = "/quiet"
Start-Process -FilePath $msbuild -ArgumentList $arguments -Wait -Verbose
}
$Session | Remove-PSSession
Related
The two code below works independently, however, they cannot work in the same script. I really need help, there's got to be something incompatible.
The first part of my script uses Invoke-CimMethod to Enable-PSRemoting, and it works.
Variables
$hostname = 'PC1'
$Session = New-PSSession $hostname
$DestinationPath = "C:\windows\temp"
Part 1
$SessionArgs = #{
ComputerName = $hostname
Credential = $credential
SessionOption = New-CimSessionOption -Protocol Dcom
}
$MethodArgs = #{
ClassName = 'Win32_Process'
MethodName = 'Create'
CimSession = New-CimSession #SessionArgs
Arguments = #{
CommandLine = "powershell Start-Process powershell -ArgumentList 'Enable-PSRemoting -Force'"
}
}
Invoke-CimMethod #MethodArgs
The second part of my code works if the first part above is not present. It is to create a TEMP folder, and then copy an entire folder into TEMP.
Part 2
Invoke-Command -Session $Session -ScriptBlock { Param($Destination) New-Item -Path $Destination -ItemType Directory -ErrorAction SilentlyContinue} -ArgumentList $DestinationPath
Copy-Item -Path "\\shared\folder\foo\bar" -ToSession $Session -Destination "C:\windows\temp\" -recurse -force
Error
Copy-Item : The runspace state is not valid for this operation.
What's weird is I've inserted the Invoke-CimMethod to many other scripts that does similar things and it works fine, like for example
Example of it working
$env:hostname
$env:process
$SessionArgs = #{
ComputerName = $env:hostname
Credential = $credential
SessionOption = New-CimSessionOption -Protocol Dcom
}
$MethodArgs = #{
ClassName = 'Win32_Process'
MethodName = 'Create'
CimSession = New-CimSession #SessionArgs
Arguments = #{
CommandLine = "powershell Start-Process powershell -ArgumentList 'Enable-PSRemoting -Force'"
}
}
Invoke-CimMethod #MethodArgs
$session = New-PSSession $env:hostname
ipconfig
Invoke-Command -Session $session -ScriptBlock {param($process) Stop-Process -ProcessName $process -Force} -ArgumentList $env:process
$Session | Remove-PSSession
Please help! I've tried everything, I even tried Get-CimSession | Remove-CimSession but that didn't work. Why is it incompatible?
I was able to fix this issue by putting the variable
$Session = New-PSSession $hostname
Right before Invoke-Command because I think when I sent enable-pssession it resets the connection.
I have written a script which works fine on the local server. However I would like to run the script block on a remote server. Here's the script block that's that runs fine locally. Can I use Invoke-Command to embed the below script block and run it on a remote server?
$Folder = Read-Host "Enter the folder name"
$FilePath = "E:\$Folder\capture.bat"
$Session = New-PSSession -ComputerName "qtestwest01"
Invoke-Command -Session $Session -ScriptBlock {$pt = New-Object System.Diagnostics.ProcessStartInfo;}
Invoke-Command -Session $Session -ScriptBlock {$pt.FileName = $using:FilePath;}
Invoke-Command -Session $Session -ScriptBlock {$pt.UseShellExecute = $false;}
Invoke-Command -Session $Session -ScriptBlock {$pt.RedurectStandardInput = $true;}
Invoke-Command -Session $Session -ScriptBlock {$e = [System.Diagnostics.Process]::Start($pt);}
Invoke-Command -Session $Session -ScriptBlock {$e.StandardInput.WriteLie("`n")}
Yes, it's pretty straight forward:
$Session = New-PSSession -ComputerName "qtestwest01"
$SB =
{
$pt = New-Object System.Diagnostics.ProcessStartInfo;
$pt.FileName = "E:\testscripts\capture.bat";
$pt.UseShellExecute = $false;
$pt.RedirectStandardInput = $true;
$e = [System.Diagnostics.Process]::Start($pt);
$e.StandardInput.WriteLine("`n")
}
Invoke-Command -Session $Session -ScriptBlock $SB
An aside: You may want to look at Start-Process -PassThru. Though I'm not sure you can set UseShellExecute using that pattern. There are some details about that here , but I didn't give it a thorough reading.
Update
Responding to your implementation and the parameter question, repeatedly calling Invoke-Command is unnecessary. You're calling into the same session so it's functionally the same thing, but everything you need is available so you can run a single command. The $Using: modifier can be used in a prefabricated ScriptBlock so long as the script block is used with certain cmdlets, including and maybe primarily Invoke-Command.
A new example:
$FilePath = "C:\windows\System32\notepad.exe"
$Session = New-PSSession -ComputerName "Server1"
$SB =
{
$pt = New-Object System.Diagnostics.ProcessStartInfo;
$pt.FileName = $Using:FilePath;
$pt.UseShellExecute = $false;
$pt.RedirectStandardInput = $true;
$e = [System.Diagnostics.Process]::Start($pt);
$e.StandardInput.WriteLine("`n")
}
Invoke-Command -Session $Session -ScriptBlock $SB
A second method of passing parameters into a script block is to use the Invoke-Command -ArgumentList parameter:
Example:
$FilePath = "C:\windows\System32\notepad.exe"
$Session = New-PSSession -ComputerName "Server1"
$SB =
{
$pt = New-Object System.Diagnostics.ProcessStartInfo;
$pt.FileName = $args[0] ;
$pt.UseShellExecute = $false;
$pt.RedirectStandardInput = $true;
$e = [System.Diagnostics.Process]::Start($pt);
$e.StandardInput.WriteLine("`n")
}
Invoke-Command -Session $Session -ScriptBlock $SB -ArgumentList $FilePath
And, Either approach, $Using or $args[0] will work even if cite the script block inline with the command:
Example:
$FilePath = "C:\windows\System32\notepad.exe"
$Session = New-PSSession -ComputerName "Server1"
Invoke-Command -Session $Session -ArgumentList $FilePath -ScriptBlock {
$pt = New-Object System.Diagnostics.ProcessStartInfo;
$pt.FileName = $args[0] ;
$pt.UseShellExecute = $false;
$pt.RedirectStandardInput = $true;
$e = [System.Diagnostics.Process]::Start($pt);
$e.StandardInput.WriteLine("`n")
}
Notes:
-ComputerName argument name and $FilePath value were changed in these examples just so I could test in my environment.
The use of $FilePath instead of $Folder. So far as I can tell $pt.FileName property needs a the full path. This was either mis-typed or in error in your last sample. $FilePath because of the -FilePath parameter on Start-Process.
$folder = 'testscripts'
$Session = New-PSSession -ComputerName "qtestwest01"
Invoke-Command -Session $Session -ScriptBlock {$pt = New-Object System.Diagnostics.ProcessStartInfo;}
Invoke-Command -Session $Session -ScriptBlock {$pt.FileName = $using:folder;}
Invoke-Command -Session $Session -ScriptBlock {$pt.UseShellExecute = $false;}
Invoke-Command -Session $Session -ScriptBlock {$pt.RedurectStandardInput = $true;}
Invoke-Command -Session $Session -ScriptBlock {$e = [System.Diagnostics.Process]::Start($pt);}
Invoke-Command -Session $Session -ScriptBlock {$e.StandardInput.WriteLie("`n")}
I am trying to pass local function to remote code but not getting any success. Below is my code.
#variables defined over here
...
function comparehash ($source, $destination)
{
$sourcefiles = #{}
...
}
$securePassword = ConvertTo-SecureString -AsPlainText -Force $Password
$cred = New-Object System.Management.Automation.PSCredential $Username, $securePassword
$session = New-PSSession -ComputerName $srv -port 22 -Credential $cred –Authentication CredSSP
Invoke-Command -Session $session -ScriptBlock {
param( $source, $destination, $application)
#Write-Host "This is" $source
# Take backup of the site first
Copy-Item $source\$application $destination -Recurse -force
${function:comparehash}
} -ArgumentList $site_path_local, $backup_path, $app
Remove-PSSession -Session $session
This code is copying source files to destination. Function has been created to validate md5 sum of the copied files. When I run the script script runs fine but it doesn't call the function code. Is there anything additional need to be done to call function?
Update
Invoke-Command -Session $session -ScriptBlock {
param( $source, $destination, $application, $fundef, $comparefunc )
Write-Host "This is" $source, $destination
# Take backup of the site first
#Copy-Item $source\$application $destination -Recurse -force
[ScriptBlock]::($comparefunc).Invoke($source,$destination)
#comparehash $site_path_local $backup_path
} -ArgumentList $site_path_local, $backup_path, $app, ${function:comparehash}
Remove-PSSession -Session $session
Above code is throwing following error:
PS C:\Windows\system32> C:\Users\vijay.patel\Documents\myscripts\Env_Refresh.ps1
This is F:\inetpub F:\Env_Backup\Refresh_Backup\
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (Invoke:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
+ PSComputerName : 172.16.82.124
Since the function is defined in your own local scope, you'll have to pass it along to the remote session as well. Through testing, it seems that it gets passed as a string, but you can use [scriptblock]::Create() to "recreate" it in the remote session:
Invoke-Command -Session $session -ScriptBlock {
param( $source, $destination, $application, $comparefunc)
# do stuff
# Now invoke the function that was provided
[ScriptBlock]::Create($comparefunc).Invoke($source,$destination)
} -ArgumentList $site_path_local, $backup_path, $app, ${function:comparehash}
I found the $Using command can be your friend here. If you keep the remote PSSession open, you can transfer all the functions you will need one at a time, then invoke the final scriptblock that calls/uses those functions.
function comparehash($source, $destination)
{
Write-Output "I like hash";
if( $source -eq $destination) { PackThePipe $source; }
}
function PackThePipe($hash)
{
Write-Output "Pushing $hash through the pipe";
}
$session = New-PSSession -ComputerName $srv -port 22 -Credential $cred –Authentication CredSSP
#create the functions we need remotely
Invoke-Command $session { Invoke-Expression $Using:function:comparehash.Ast.Extent.Text; }
Invoke-Command $session { Invoke-Expression $Using:function:PackThePipe.Ast.Extent.Text; }
#call the scriptblock that utilizes those functions
Invoke-Command $session { $s = "12345"; $d="12345"; comparehash $s $d; }
Remove-PSSession $session;
Running the following code produces an error because the variable used for path is resolved as null event though it defined in the script:
$ServerName = "test01"
$RemotePath = "C:\Test\"
$TestScriptBlock = { copy-item -Path $RemotePath -Destination C:\backup\ -Force -Recurse }
$CurrentSession = New-PSSession -ComputerName $ServerName
Invoke-Command -Session $CurrentSession -ScriptBlock $TestScriptBlock
How do I call the $RemotePath defined in the parent script from within the ScriptBlock? I need to use $RemotePath in other parts of the parent script. Note, this value doesn't change, so it can be a constant.
UPDATE -- WORKING SOLUTION
You have to pass in variable as parameter to the scriptblock:
$ServerName = "test01"
$RemotePath = "C:\Test\"
$TestScriptBlock = { param($RemotePath) copy-item -Path $RemotePath -Destination C:\backup\ -Force -Recurse }
$CurrentSession = New-PSSession -ComputerName $ServerName
Invoke-Command -Session $CurrentSession -ScriptBlock $TestScriptBlock -ArgumentList $RemotePath
You've got two scripts there, not one. The $TestScriptBlock is a separate script nested inside the main one, you send it to the remote computer, and that remote computer doesn't have $RemotePath configured. Try:
$ServerName = "test01"
$TestScriptBlock = {
$RemotePath = "C:\Test\"
copy-item -Path $RemotePath -Destination C:\backup\ -Force -Recurse
}
$CurrentSession = New-PSSession -ComputerName $ServerName
Invoke-Command -Session $CurrentSession -ScriptBlock $TestScriptBlock
(I would probably call it $LocalPath then, though)
Try this syntax:
$globalvariable1 = "testoutput01"
$globalvariable2 = "testoutput02"
$Scriptblock = {
Write-Host $using:globalvariable1
Write-Host $using:globalvariable2
}
$serverName = Domain\HostNameofServer
Invoke-Command -ComputerName $serverName -ScriptBlock $ScriptBlock -ArgumentList $globalvariable1, $globalvariable2
The execution happens in a different session and all the variables in the current scope are not copied to the remote session by default.
You can use either parameters or "using:" as explained here:
How can I pass a local variable to a script block executed on a remote machine with Invoke-Command?
I have the following cmd file:-
PowerShell.exe -noexit E:\wwwroot\domains\processes\AddDirectory.ps1 -Param testdomain.co.uk
which goes through to:-
$Session = New-PSSession -ComputerName 192.168.0.25
$script = {
Param($Param1)
set-executionpolicy unrestricted -force
# Set Variables
$domain = $Param1
$sitepath = "e:\domains\" + $domain
# Check for physical path
if (-not (Test-Path -path $sitePath))
{
New-Item -Path $sitepath -type directory
New-Item -Path $sitepath\wwwroot -type directory
}
set-executionpolicy restricted -force
}
Invoke-Command -Session $Session -ScriptBlock $script
But it just runs but does nothing.
If I declare the $domain variable as $domain = 'testdomain.co.uk' it works but it doesn't want to pass through the var from the cmd file. What am I doing wrong? I've tried to put it in the Invoke-Command as -ArgumentsList -$Param1 but that doesn't work either.....
Any ideas greatfully received
Thanks
Paul
Update - I've updated my code as per below but getting same issue:-
param($domainName)
$script = {
Param($Param1)
set-executionpolicy unrestricted -force
# Set Variables
$domain = $Param1
$sitepath = "e:\domains\" + $domain
# Check for physical path
if (-not (Test-Path -path $sitePath))
{
New-Item -Path $sitepath -type directory
New-Item -Path $sitepath\wwwroot -type directory
New-Item -Path $sitepath\db -type directory
New-Item -Path $sitepath\stats -type directory
}
set-executionpolicy restricted -force
}
$Session = New-PSSession -ComputerName 192.168.0.25
Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $domainName
You need to use a param block in the script, the argument you pass to the file will be assign to $domainName and you will use it to pass the value to the scriptblock :
PowerShell.exe -noexit E:\wwwroot\domains\processes\AddDirectory.ps1 testdomain.co.uk
# script file
param($domainName)
$script = {
Param($Param1)
...
$domain = $Param1
...
}
$Session = New-PSSession -ComputerName 192.168.0.25
Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $domainName