I'm trying to build a script which copies files and execute them on a specific amount of servers.
To do so, I need to run a remote command. I stored it in a variable.
I know that variables can't be used within a remote session. Except you use the -ArgumentList
parameter. But this doesn't work for me either.
Here's the code and the error message:
$CMD_AgentDirectory = "New-Item -Path 'C:\FlexAgent' -ItemType Directory"
Invoke-Command -ComputerName Server01 -ScriptBlock {
Invoke-Expression $CMD_AgentDirectory
} -ArgumentList $CMD_AgentDirectory
Error Message:
Cannot bind argument to parameter 'Command' because it is null. + CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand + PSComputerName : Server01
What am I doing wrong?