Posted On October 15, 2021

Invoke Commands on Remote Computers [To Install Applications]

kimconnect 0 comments
blog.KimConnect.com >> Codes >> Invoke Commands on Remote Computers [To Install Applications]
$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 $_
        }}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: check whether the current user is a member of Domain Admins

# short snippet to check whether the currently login user is a domain admin$CurrentUser =…

PowerShell: Add Quorum to Clusters

# SetClusterQuorum.ps1$clustersAndQuorums=@();$clustersAndQuorums+=[PSCustomObject]@{ClusterName='CLUSTER1';Quorum='\\FILESHERVER007\CLUSTER1$'};$clustersAndQuorums+=[PSCustomObject]@{ClusterName='CLUSTER2';Quorum='\\FILESHERVER007\CLUSTER2$'};function getViableNode{ param($nodes) foreach ($node in $nodes){ $bingo=Test-NetConnection $node -InformationLevel Quiet; if($bingo){return $node} }}…

PowerShell: Microsoft Dynamics Update All Organizations

# updateCrmOrgs.ps1 function updateCrmOrgs{ try{ Add-PSSnapin Microsoft.Crm.Powershell $servers=Get-CrmServer $highestVersion=($servers.Version|measure-object -Maximum).Maximum $lowVersionServers=$servers|?{[version]$_.Version -lt [version]$highestVersion} if($lowVersionServers){ write-warning…