Powershell Remoting: Execute local function on two target servers

Posted by icnivad on Server Fault See other posts from Server Fault or by icnivad
Published on 2010-03-22T13:52:00Z Indexed on 2010/03/22 14:01 UTC
Read the original article Hit count: 460

Filed under:
|

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

© Server Fault or respective owner

Related posts about powershell

Related posts about remoting