I have a running PS1-script (start-webserver.ps1) running as a "web server" that is listening for http calls that executes a script (example: script2.ps1) executed in the call to the "web server". I am executing the script with Start-Job. How can I in the executed script (script2.ps1) access variables in start-webserver.ps1?
Start-WebServer.ps1
$allObjects = @()
foreach ($item in $items) {
$objectUID = $item.Attributes.Value
$propertiesHash = [ordered]@{UID = $objectUID}
$properties = $items.ChildNodes
foreach ($property in $properties.ChildNodes) {
$propertyName = $property.Attributes.Value
$propertyValue = $property.innerText
$propertiesHash.Add($propertyName, $propertyValue)
}
$object = New-Object PSObject -Property $propertiesHash
$allObjects += $object
}
$job = Start-Job -Name "$identifier" -FilePath "Path\To\ScriptToExecute.ps1" -InputObject $allObjects -ArgumentList $propertiesHash
ScriptToExecute.ps1
'Script executed!' | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$propertiesHash | Get-Member | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
$allObjects | Get-Member | Out-File -Path ".\output.txt" -Encoding UTF8 -Append
I end up with an "output.txt" with the following content:
Script executed!
Empty line