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 TCP Connections of Server by Port Numbers

# Check-TCP-Connections.ps1# This function will output progress onto the console as well as returning a…

Bash Shell Quick If Then and Case Switch Statements

protocol=udp # or tcp # If-then implementation if [$protocol == udp] then prefix=@ else prefix=@@…

PowerShell: Use Win-SCP to Download Files from SFTP Server

Version 2: # downloadFilesViaSftp.ps1 # Version 0.0.2 # # Description: # This simple script is…