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 {
Param([string]$propertyTableKey)
$hashValue = -1
$script = {
$db = Get-Database "core"
$previousValue = $db.Properties.GetLongValue($using:propertyTableKey, -1000)
$previousValue
}
$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?