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

Expanding System Volume C Drive of Windows Hyper-V Virtual Machine

There are three three steps to expand a disk of a virtual machine: Connect to…

Basic CSS: Use Clockwise Notation to Specify the Margin of an Element

<style>.injected-text {margin-bottom: -25px;text-align: center;}.box {border-style: solid;border-color: black;border-width: 5px;text-align: center;}.yellow-box {background-color: yellow;padding: 20px 40px 20px 40px;}.red-box…

One-Liner: Capture Report of Logons in AD

echo %date%,%time%,%username%,logon,%computername% >> \\FILESERVER01\IT\scripts\logons.csv
Other project: DragonCoin.com