0

Environment:
Windows 2016 Standard
Windows 10 Pro
PowerShell 5.1

Anything glaringly wrong with following PowerShell script? It runs fine with no errors but doesn't actually set/create the environment variable.

$session = New-PSSession -ComputerName $hostName -Credential $cred
$sspSharedConnStr = Invoke-Command -Session $session -ScriptBlock{[Environment]::GetEnvironmentVariable("hello_world", 'Machine')}
if ($null -eq $sspSharedConnStr) { 
    $sspSharedConnStr = "Server=$dbServer1;Database=$database1;User ID=$username;Password=$password;MultipleActiveResultSets=true;TrustServerCertificate=true"
    Invoke-Command -Session $session -ScriptBlock{[Environment]::SetEnvironmentVariable("hello_world", $sspSharedConnStr, 'Machine')}
}
Rod
  • 13,333
  • 28
  • 106
  • 203
  • You can use [`$using:sspSharedConnStr`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.2#example-9-include-local-variables-in-a-command-run-on-a-remote-computer) to reference a local variable in the remote scope or `Invoke-Command -ArgumentList $sspSharedConnStr ...` to pass the variable to the remote scope. – Santiago Squarzon Apr 14 '22 at 18:34

0 Answers0