PowerShell – simple script template

#-------------[deklarations]--------------------------
#source
$computers=Get-Content '\\host\shares\!scripts\!source\!computers.txt'
#logs
$successLog='\\host\shares\!scripts\!logs\done.LOG'
$errorLog='\\host\shares\!scripts\!logs\error.LOG'
$unreachableLog='\\host\shares\!scripts\!logs\unreachable.LOG'
#params
$source='\\host\shares\!install\'
$destination='c$\scripts\install\'
#-------------[functions]--------------------------
foreach($computer in $computers) {
    if(Test-Connection -ComputerName $computer -Count 1 -Quiet) {
        Try {
            Write-host "Copying files: $computer" -foreground 'gray'
            Copy-Item -Path $source -Destination \\$computer\$destination -Force -Recurse

            Write-host "Installation: $computer" -foreground 'gray'
            Invoke-Command -computerName $computer -ScriptBlock {$scriptBlock}
            
            Write-host "updated: $computer" -foreground 'green'
            $computer >> $successLog
        } Catch {
            Write-host "error: $computer" -foreground 'red'
            $computer >> $errorLog
        }
    } else {
        Write-host "offline: $computer" -foreground 'yellow'
        $computer >> $unreachableLog
    }
}
#-------------krolaki.eu--------------------------

Leave a Reply

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