Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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}  
}
share|improve this question

1 Answer 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.