views:

21

answers:

1

Hi there,

I'm trying to write a powershell script that will take a list of commands and then run them on a remote machine, I have the following code:

foreach($command in $commands)
{
  Invoke-Command -computer "BNEBAK" -scriptblock{"$command"}
}

Which does not throw any error but also does not actually run the command (e.g stop-service servicename). $commands is read in from a text file passed as an argument when the script is called, I know the rest of this script works because I have been using it to do local commands with Invoke-Expression for some time.

Any help would be fantastic.

A: 

The correct code would be

$commands = @(get-content com.txt)
for($command in $commands) { 
  $scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock($command) 
  Invoke-Command -computer $computer -scriptblock $scriptblock 
}
bEUY