I'm trying to set some global environment variables on a target machine using the following script snippet:
Invoke-Command -ScriptBlock ${function:SetEnvironmentVariableOnTarget} -ArgumentList $location
...
function SetEnvironmentVariableOnTarget($location)
{
try
{
[System.Environment]::SetEnvironmentVariable($environmentVarName, $environmentVarValue, $envVarTarget)
Write-Host "$location - Successfully set $environmentVarName=$env:environmentVarValue in $environmentVarTarget"
}
catch
{
Write-Host "$location : ERROR: $($Error[0].Exception)"
}
}
Where the $environmentVarName and $environmentVarValue are both strings, and $envVarTarget is an integer, which I set to 2 (Machine).
However I'm getting the following error:
ERROR: System.Management.Automation.MethodException: Cannot find an overload for "SetEnvironmentVariable" and the argument count: "3".
I have tried to access the target machine remotely, and I am able to successfully execute the above command [System.Environment]::SetEnvironmentVariable("TEST","DEMO", 2)
using Powershell command line.
I have also tried to invoke the command on a remote target using [System.EnvironmentVariableTarget]::Machine
, but that fails with the same error message.
Not sure why this fails.. thoughts?