I'm having issues with variables when using SPE Remoting with OctopusDeploy, where method-local variables can't be used.

I get this error

The value of the using variable '$using:propertyTableKey' cannot be retrieved because it has not been set in the local session.

Has anyone else encountered this?

To get around it, I need to set a global variable in the script just so it's available...but even then, it can only be initialized, and never updated. (the initialized value is always passed in).

Here is my original code, how I wish to use it:

function Invoke-SPERemoteScriptForPropertyTableValue {

  $hashValue = -1
  $script = {
    $db = Get-Database "core"
    $previousValue = $db.Properties.GetLongValue($using:propertyTableKey, -1000)


  $session = New-ScriptSession -Username $User -Password $Pass -ConnectionUri $Url
  $myValue = Invoke-RemoteScript -Session $session -ScriptBlock $script -ErrorAction Stop -Verbose   
  Stop-ScriptSession  -Session $session
  $session = $null

  return $myValue

function Invoke-Script($propertyKeys)
  foreach ($propertyKey in $propertyKeys)
    $propertyValue = Invoke-SPERemoteScriptForPropertyTableValue $propertyKey
    Write-Host "Property Value for Key $propertyKey is $propertyValue"

$User = $OctopusParameters['SitecoreUserName']
$Pass = $OctopusParameters['SitecoreUserPassword']
$Url = $OctopusParameters['Url']
$myArray = "Prop1", "Prop2","Prop3"

Invoke-Script $myArray

This is placed in a StepTemplate in Octopus Deploy. The connection to Sitecore works, and I can run other code in the script block successfully, however I can't have dynamic values being passed in to my script block.

Does anyone know why this isn't working correctly?

1 Answer 1


Without having an instance of Octopus Deploy, here are some items for consideration:

  1. The syntax $using:VARIABLE_NAME is used in the context of a remote server command. So you will only see it for a command such as Invoke-Command which runs through WinRM or Invoke-RemoteScript which runs through the SPE Remoting services. In the case of SPE, the $using: is converted to a different variable name before being serialized and channeled to the web service.

  2. Try using the -ArgumentList parameter on Invoke-RemoteScript. I believe the data is passed to a variable called $params.

# The following remotely executes a script in Sitecore with arguments.

$script = {
    [Sitecore.Security.Accounts.User]$user = Get-User -Identity admin

$args = @{
    "date" = [datetime]::Now

$session = New-ScriptSession -Username admin -Password b -ConnectionUri http://remotesitecore
Invoke-RemoteScript -Session $session -ScriptBlock $script -ArgumentList $args
Stop-ScriptSession -Session $session

Name                     Domain       IsAdministrator IsAuthenticated
----                     ------       --------------- ---------------
sitecore\admin           sitecore     True            False          
6/25/2015 11:09:17 AM
  • Perfect, thank you! Using the @args hashTable and passing them in through -ArgumentList worked! Still not sure about 1. though. As the parameter gets passed into SPE find when it's single-use and a global....but I'm happy to just use 2. instead for this anyway. Thanks! Mar 13, 2018 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.