Check if an update has been installed

Windows update! – Patching Tuesday!

The 2 words any system admin would not love to hear 😛 the process of installing the updates is no the hardest job. it’s that random compliance check from the Security team which make you run like headless chickens.

Below script will save the time by getting the Machines in an OU and cross check it against the given KB######## and will let you know if the machine is compliant or not.

$machines= Get-ADComputer -Filter * -Searchbase "Specific OU" -Properties *| Select name | sort asc


$UpdateNo = 'Update number KB######'
foreach ($machine in $machines) { 

        if (test-Connection -ComputerName $machine -Count 2 -Quiet ) {  
         $PatchCheck = Get-Hotfix -computername $machine | where {$_.HotfixID -eq $UpdateNo} | Select-Object HotfixID 
            Try{
                $ErrorActionPreference = 'stop'
                if($PatchCheck.HotfixID.Contains($UpdateNo)-eq $true){
                write-Host "$machine is Alive and Updated with $UpdateNo patch." -ForegroundColor Green
                }
            }catch{
                $ErrorActionPreference = 'continue'
                Write-Host "$machine Is Up and Not Patched!!" -ForegroundColor Red
            }
               
         }else{ 
         Write-Warning "$machine seems dead not pinging"     
         }     
} 

If you have only a handful of machines that needs a quick check the script will still be able to help you with that


$machines= Get-content -path "list of machine names.txt"

$UpdateNo = 'Update number KB######'
foreach ($machine in $machines) { 

        if (test-Connection -ComputerName $machine -Count 2 -Quiet ) {  
         $PatchCheck = Get-Hotfix -computername $machine | where {$_.HotfixID -eq $UpdateNo} | Select-Object HotfixID 
            Try{
                $ErrorActionPreference = 'stop'
                if($PatchCheck.HotfixID.Contains($UpdateNo)-eq $true){
                write-Host "$machine is Alive and Updated with $UpdateNo patch." -ForegroundColor Green
                }
            }catch{
                $ErrorActionPreference = 'continue'
                Write-Host "$machine Is Up and Not Patched!!" -ForegroundColor Red
            }
               
         }else{ 
         Write-Warning "$machine seems dead not pinging"     
         }     
} 

Check C Drive Space

In a co-operate environment checking Drive space is challenging job when it comes to patching. like i said in the previous blog checking one machine could be a easy job but checking in a bulk could be challenging.

Majority of the patches that gets failed – primary reason could be not enough space in the C Drive to it to be installed.

having a clear picture of this issue would give an upper hand. Script below will be able to let you know the free space in the drive.

To show the result in the screen

$Computername = [System.IO.File]::OpenText("machines name in a file.txt")

while($null -ne ($Computer= $Computername.ReadLine())) {

    if($?){
        Get-WmiObject –ComputerName $Computer–Class Win32_Volume `
              |ft –auto $line,`
                        DriveLetter,`
                        Computer,`
                        Label,`
                         @{Label=”Free(GB)”;Expression={'{0:N0}’ –F ($_.FreeSpace/1GB)}}
     }     
 }
 $Computername.Close()

To export to the result to a file use the below.

$Computername = [System.IO.File]::OpenText("machines name in a file.txt")

while($null -ne ($Computer= $Computername.ReadLine())) {

    if($?){
        Get-WmiObject –ComputerName $Computer–Class Win32_Volume `
              |ft –auto $line,`
                        DriveLetter,`
                        Computer,`
                        Label,`
                         @{Label=”Free(GB)”;Expression={'{0:N0}’ –F ($_.FreeSpace/1GB)}} >> "Output path .txt"
     }     
 }
 $Computername.Close()

Add machine/computer to a Domain

Adding a machine to domain is easy task if its in single digit (Just Kidding). It can be time consuming and it can be come challenging in an environment where machine are built frequently

Powershell will make this task seamless with the script below.

Note: The Administrator should have appropriate access to run the script and to join the domain.

$domain = "Your Domain Name"
$password = "Password" | ConvertTo-SecureString -asPlainText -Force
$username = "$domain\Admin Account name" 
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
Add-Computer -DomainName $domain -Credential $credential -OUPath ' OU PATH '
Restart-Computer

Add Multiple Users to a AD Group

Adding User to a new group is annoying task when the amount of the users that need to be adding is more.

Powershell comes handy for this task with the script below it should be a piece of cake.

Things to note before running the script:

  • Active Directory module needs to be import to Powershell.
  • The Administrator should have relevant access to run the script.

To display the result in the screen.

Import-module ActiveDirectory

$reader = [System.IO.File]::OpenText("list of username.txt")

while($null -ne ($user= $reader.ReadLine())){

    Try{
    Add-ADGroupMember -Identity "Group name" -Member $line
    $user+" has been Added to the group!!"
    }catch{
    $user+ "not added to the group!!!"
    }
}

$reader.Close()

if you need a report of users added to a specific group in a text use the below script.

Import-module ActiveDirectory

$reader = [System.IO.File]::OpenText("list of username.txt")

while($null -ne ($user= $reader.ReadLine())){

    Try{
    Add-ADGroupMember -Identity "Group name" -Member $user
    $user+" has been Added to the group!!"
 >> "Path to save.txt"
    }catch{
    $user+ "not added to the group!!!"
 >> "Path to save.txt"
    }
}

$reader.Close()