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.
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 "------------------------------------------------"
}
When starting the script from my machine like this:
PS C:\temp\ps> .\setEnvVarRemote.ps1 MYSERVER -UserName "MYUSERNAME" -Password "MYPASSWORD" -Environment "Test"
I get the following output
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
========================================================================================
------------------------------------------------
Unfortunately the environment variable is not set and I don't get an error message... what am I doing wrong?
My user has admin rights and the password is correct.
Thanks in advance
Invoke-Commandon a remote host? Just runInvoke-Command -Computer $remoteMachines .... Add-AsJobif required. Also, what makes you think the environment variable is not set? How did you verify?New-Object SomeType(arg1, ...), useNew-Object SomeType [-ArgumentList] arg1, ...- PowerShell cmdlets, scripts and functions are invoked like shell commands, not like methods. That is, no parentheses around the argument list, and whitespace-separated arguments (,constructs an array as a single argument, as needed for-ArgumentList).$cred = Get-Credentialor$cred = Get-Credential "[email protected]"will suffice), or useGet-Credentialto create the credential object, then dump the credential object to disk usingExport-CliXmlfor which can be later read byImport-CliXmlby that same user on that same machine for automation later.