title: Set Environment Variable on Remote Computer via PowerShell
body: <p>I'm trying to set an environment variable via TFS Releasemanagement by invoking a powershell script which should open a remote session on a machine and set the environment variable.</p>
  
  <pre><code>param(
  [Parameter(Mandatory=$true)][String]$RemoteComputers,
  [Parameter(Mandatory=$true)][String]$UserName,
  [Parameter(Mandatory=$true)][String]$Password,
  [Parameter(Mandatory=$true)][String]$Environment
  )
  
  $credential = New-Object System.Management.Automation.PSCredential($UserName , (ConvertTo-SecureString -String $Password -AsPlainText -Force));
  $remoteMachines = $RemoteComputers -split ","
  
  $remoteMachines | ForEach-Object {
      $machineBlock = {
          $machineName = $args[0]
          $credentials = $args[1]
  
          Write-Host $machineName
  
          $scriptBlockSetEnvironmentVariable = {
              $environment = $args[0]
  
              Write-Host "ScriptBlockSetEnvi$environmentVariable $environment"
              [System.Environment]::SetEnvironmentVariable('ASPNETCORE_ENVIRONMENT', $environment , [System.EnvironmentVariableTarget]::Machine)
          }
  
          Write-Host "========================================================================================"
          Write-Host "@#@#@ Opening remote session to $machineName"
          $session = New-PsSession -ComputerName $machineName -Credential $credentials
          Invoke-Command -Session $session -ScriptBlock $scriptBlockSetEnvironmentVariable -ArgumentList $Environment
          Remove-PSSession -Session $session
          Write-Host "@#@#@ Session Closed"
          Write-Host "========================================================================================"
      }
  
      Write-Output "Starting job on $_"
      Start-Job -Name $_ -ScriptBlock $machineBlock -ArgumentList $_, $credential
  }
  
  Write-Host "Waiting for all jobs to finish"
  Wait-Job -Name $remoteMachines
  $remoteMachines | ForEach-Object {
      Write-Host "Getting output for $_"
      Receive-Job -Name $_
      Write-Host "------------------------------------------------"
  }
  </code></pre>
  
  <p>When starting the script from my machine like this:</p>
  
  <blockquote>
    <p>PS C:\temp\ps> .\setEnvVarRemote.ps1 MYSERVER -UserName "MYUSERNAME" -Password "MYPASSWORD" -Environment "Test"</p>
  </blockquote>
  
  <p>I get the following output</p>
  
  <pre><code>Starting job on MYSERVER
  
  Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
  --     ----            -------------   -----         -----------     --------             -------
  9      MYSERVER        BackgroundJob   Running       True            localhost            ...
  Waiting for all jobs to finish
  1      MYSERVER        BackgroundJob   Completed     False           localhost            ...
  3      MYSERVER        BackgroundJob   Completed     False           localhost            ...
  5      MYSERVER        BackgroundJob   Completed     False           localhost            ...
  7      MYSERVER        BackgroundJob   Completed     False           localhost            ...
  9      MYSERVER        BackgroundJob   Completed     True            localhost            ...
  Getting output for MYSERVER
  MYSERVER
  ========================================================================================
  @#@#@ Opening remote session to MYSERVER
  ScriptBlockSetEnvi
  @#@#@ Session Closed
  ========================================================================================
  ------------------------------------------------
  </code></pre>
  
  <p>Unfortunately the environment variable is not set and I don't get an error message... what am I doing wrong?</p>
  
  <p>My user has admin rights and the password is correct.</p>
  
  <p>Thanks in advance</p>