在PowerShell中的远程计算机上注册环境变量

问题描述:

我只是想学习PowerShell,并通过一些例子,我发现想出了以下脚本。我试图在不同的服务器上注册一些环境变量。当我运行它时,出现错误 -在PowerShell中的远程计算机上注册环境变量

Invoke-Command : A positional parameter cannot be found that accepts argument 'System.Object[]'. 
At C:\Users\gswartz\Desktop\regvalue.ps1:3 char:16 
+  Invoke-Command <<<< -ComputerName $server [Environment]::SetEnvironmentVariable("xxxx", "xxxx", "Machine") 
    + CategoryInfo   : InvalidArgument: (:) [Invoke-Command], ParameterBindingException 
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand 

这是脚本。有人能告诉我我做错了什么吗?

function set_vars($server) { 

    Invoke-Command -ComputerName $server [Environment]::SetEnvironmentVariable("xxxx", "xxxx", "Machine") 
    Invoke-Command -ComputerName $server [Environment]::SetEnvironmentVariable("xxxx", "xxxx", "Machine") 
    Invoke-Command -ComputerName $server [Environment]::SetEnvironmentVariable("xxxx", "xxxx", "Machine") 
    Invoke-Command -ComputerName $server [Environment]::SetEnvironmentVariable("xxxx", "xxxx", "Machine") 

    Write-Host "vars set for $server" 
} 

$servers = @("server1","server2") 

$servers | ForEach{ 
    set_vars $_ 
} 
+1

'{'和'}'失踪。 – PetSerAl

+0

建议使用组策略来执行此操作而不是脚本。 –

Invoke-Command -ComputerName $Server -ScriptBlock { ... }