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.