1

I have a function foo on my local Machine. (In my profile, but calling c:\scripts\foo.ps1 would be also OK!) How do i load this on ServerA and ServerB so i can execute the function in the next statement?

This is what i tried with no success..

$serverlist = 
"192.168.20.1",
"192.168.20.12"  
foreach ($item in $serverlist) {
    New-PSSession -ComputerName "$item" -Credential $cred -Name  ($item + "_session")
    Invoke-Command -ComputerName $item -Credential $cred -filepath scripts:\foo.ps1 
    Invoke-Command -ComputerName $item -Credential $cred -scriptblock {foo}  
}
icnivad
  • 327
  • 3
  • 12

1 Answers1

1

Two mistakes here:

  1. First of all, Invoke-Command can use either the -ComputerName parameter or the -Session parameter. You are creating a session here but are not using it, therefore the commands run are executed in different sessions and don't know anything about each other.

  2. -ComputerName takes a string[] so you could drop the foreach. You can then probably just put the function definition and the call in a single script block.

Joey
  • 1,823
  • 11
  • 13