$computers=@(
'pc1',
'pc2'
)
$commandString="choco install pgadmin4 -y --ignore-checksums"
function invokeCommand{
param(
$computers,
$commandString,
$credentials
)
$command={
param($commandString);
write-host $env:computername;
invoke-expression $commandString;
}
foreach ($computer in $computers){
try{
$session=if($credentials){New-PSSession -ComputerName $computer}else{New-PSSession -ComputerName $computer -Credential $credentials}
if($session.state -eq 'Opened'){
invoke-command -session $session -scriptblock $command -Args $commandString
Remove-PSSession $session
}else{
write-warning "Cannot connect to $computer"
}
}catch{
write-warning $_
}}
}
Categories: