I am wondering which is best practice considering both examples will probably work. Using the built in help examples I have written a script to install windows features on remote servers. Here is my code:
$servers = ('server1', 'server2', 'server3', 'server4')
ForEach ($server in $servers) {
Install-WindowsFeature -Name Desktop-Experience -ComputerName $server -IncludeAllSubFeature -IncludeManagementTools -Restart
}
Would the above be preferred OR should I wrap the "Install-WindowsFeature ..." in an "Invoke-Command" block like the following?
Invoke-Command -ComputerName server1, server2, server3, server4 -command {
Install-WindowsFeature -Name Desktop-Experience -ComputerName $server -IncludeAllSubFeature -IncludeManagementTools -Restart
}
Thanks for your insight!
Personally I would use the latter (directly call
Install-WindowsFeature -ComputerName $server
rather than do a separateInvoke-Command
) in this case for the following reasons:Invoke-Command
's script block. This is entirely possible, but more work.Invoke-Command
in this case because you're running a single command on the remote computer (as opposed to running multiple commands with-ComputerName
parameters vs. running multiple commands inside the script block).