簡體   English   中英

通過 PowerShell 在遠程計算機上設置環境變量

[英]Set Environment Variable on Remote Computer via PowerShell

我正在嘗試通過調用 powershell 腳本通過 TFS Releasemanagement 設置環境變量,該腳本應該在機器上打開遠程 session 並設置環境變量。

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 "------------------------------------------------"
}

當像這樣從我的機器啟動腳本時:

PS C:\temp\ps>.\setEnvVarRemote.ps1 MYSERVER -用戶名“MYUSERNAME”-密碼“MYPASSWORD”-環境“測試”

我得到以下 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
========================================================================================
------------------------------------------------

不幸的是,環境變量沒有設置,我沒有收到錯誤消息......我做錯了什么?

我的用戶具有管理員權限,並且密碼正確。

提前致謝

這應該足夠了,您使用會話和Foreach-Object循環使代碼過於復雜:

$result = Invoke-Command -ComputerName $remoteMachines -Credential $credentials -ArgumentList $Environment {
  [System.Environment]::SetEnvironmentVariable( 'ASPNETCORE_ENVIRONMENT', $args, [System.EnvironmentVariableTarget]::Machine )
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM