Search code examples
powershellpowershell-remoting

register environment variables on remote computers in powershell


I am just trying to learn powershell and through some examples I found came up with the following script. I'm attempting to register some environment variables on the different servers. When I run it I get the error -

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

Here's the script. Can someone tell me what I'm doing wrong?

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 $_ 
}

Solution

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