I recently started working with PowerShell and are working on a small script for installing Sitecore Packages using Sitecore PowerShell Extensions and remoting.
I have created the following script:
$session = New-ScriptSession -Username admin -Password b -ConnectionUri http://sitecore
$identity = "admin"
$date = [datetime]::Now
$jobId = Invoke-RemoteScript -Session $session -ScriptBlock {
[Sitecore.Security.Accounts.User]$user = Get-User -Identity $using:identity
$user.Name
} -AsJob
Wait-RemoteScriptSession -Session $session -Id $jobId -Delay 5 -Verbose
My script is running fine in the PowerShell ISE but in the PowerShell Command Prompt I get the following error:
Get-Variable : Cannot find a variable with the name 'date'.
At C:\Users\Administrator\Documents\WindowsPowerShell\Modules\SPE\SPE.psm1:21 char:26
+ ... $value = Get-Variable -Name $Var.SubExpression.VariablePath.UserPa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (date:String) [Get-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
Get-Variable : Cannot find a variable with the name 'identity'.
At C:\Users\Administrator\Documents\WindowsPowerShell\Modules\SPE\SPE.psm1:21 char:26
+ ... $value = Get-Variable -Name $Var.SubExpression.VariablePath.UserPa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (identity:String) [Get-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
Apparantly I do not have access to the variables in command prompt but in ISE it cannot find the $using:
variables.
I only have little experience with PowerShell and im not sure why this is happening.
Thomas, I suspect you have an old version of the SPE Remoting module. Can you upgrade to 4.3?
For some reason while testing using Windows PowerShell 5.1 and SPE 4.3 the module was not working for me in a slightly different scenario in the Windows PowerShell ISE.
I reported an issue to address my problem which hopefully accounts for yours.
The example you provided should work correctly given it looks like it came from the docs.
Update
Issue #772
I was finally able to reproduce your issue of missing variables in the Console; I've updated the SPE Remoting module to account for that scenario.
I've also fixed an issue where the
Start-ScriptSession
did not properly use the$using
variables when callingInvoke-RemoteScript
as a job.Expect to see the fixes in 4.3.1+.