Coding

[POWERSHELL] CHECK QUEST KACE ONE AGENT SERVICE FOR MULTIPLE COMPUTER (VERSION #4)

Here is the #4 version, changes as below:

  • Limit concurrent running background job with $maxConcurrentJobs to 50
  • use Start-Job to check online status as well as check service

 

#The OU to search
 $SearchBase = "OU=COMPUTER,OU=World_First,DC=domain,DC=local"

#Search result of computers (Windows only)
$Computers = Get-ADComputer -Filter {OperatingSystem -Like "Windows*"} -SearchBase $SearchBase -SearchScope Subtree -ErrorAction Stop -ErrorVariable NoComputers|sort Name
if($noComputers){
 Write-Warning -Message "Unable to get computer from AD"
 Exit
}

$ServiceStatusFile = "ServiceStatus.csv"
$OfflineFile = "Offline.csv"
$noServiceFile = "noService.csv"
$maxConcurrentJobs = 50 #Max. number of simultaneously running jobs

#Array for online computers
$onlineComputers = @()
#Array for offline computers
$offlineComputers = @()

$ping={

function ping{
 param (
 [parameter(Mandatory=$true,ValueFromPipeline=$true)]
 [string]$client
 )
 
 
 #win32_PingStatus variables
 $timeout= 1500
 $filter='Address="{0}" and Timeout={1}' -f $client, $timeout
 
 
 
 
 return Get-WmiObject Win32_PingStatus -Filter $filter
 
 
 
 }

}




$func={

function checkService{

param (
 [parameter(Mandatory=$true,ValueFromPipeline=$true)]
 [string]$client
 )
 
 try{

$serviceStatus=get-service -DisplayName "Quest KACE One Agent" -ComputerName $client -ErrorAction SilentlyContinue -ErrorVariable ServiceNotAvailable|select-object MachineName,DisplayName,Status
 
 if($serviceStatus){
 $output = $serviceStatus
 return $serviceStatus
 }

else{
 
 write-warning -Message "$client,$ServiceNotAvailable"
 $output = $client +"," + $ServiceNotAvailable
 return $output
 }
 
 }
 
 Catch [System.Management.Automation.CmdletInvocationException]{
 
 write-warning -Message "$client,$ServiceNotAvailable"
 $output = $client +"," + $ServiceNotAvailable.Message
 return $output
 }
 
 Catch{
 $ErrorMessage = $_.Exception.Message
 $FailedItem = $_.Exception.ItemName
 }
 
 } 
} 
 
#Ping all computers
foreach($computer in $computers) {
 $client = $computer.Name 
 Write-Progress -Activity "Checking computer availablilty....." -status "Ping $client" -percentComplete ($computers.IndexOf($computer) / $Computers.count*100)
 if ((Get-Job -State 'Running').Count -lt $maxConcurrentJobs) {
 Start-Job -ScriptBlock {ping $args[0]} -ArgumentList @($client) -Name "$client" -InitializationScript $ping
 }else{
 Start-Sleep -Seconds 3
 Start-Job -ScriptBlock {ping $args[0]} -ArgumentList @($client) -Name "$client" -InitializationScript $ping
 }
}

#Wait for job to complete
Get-Job|Wait-Job

#Get result of ping
foreach($computer in $computers) {
 
 $client = $computer.Name
 Write-Progress -Activity "Retrieving results of availability....." -status "Ping result for $client" -percentComplete ($computers.IndexOf($computer) / $Computers.count*100)
 $pingStatus=Receive-Job -Name $client
 #Removes the ping job when result is retrieved
 Remove-Job -Name $client

#If PC is online
 if($pingStatus.StatusCode -eq '0'){
 
 $onlineComputers= $onlineComputers+($computer)
 #Create a checkService job for online PC
 if ((Get-Job -State 'Running').Count -lt $maxConcurrentJobs) {
 Start-Job -ScriptBlock {checkService $args[0]} -ArgumentList @($computer.Name) -Name "$client" -InitializationScript $func 
 }
 else{
 Start-Sleep -Seconds 5
 Start-Job -ScriptBlock {checkService $args[0]} -ArgumentList @($computer.Name) -Name "$client" -InitializationScript $func 
 }
 }
 else{
 $offlineComputers= $offlineComputers+($computer)
 Write-Warning -Message "$client is offline"
 
 $client|Out-File -FilePath $OfflineFile -Append
 
 }
}

#Wait for job to complete
Get-Job|Wait-Job

#Get result from checkService
foreach($onlinecomputer in $onlineComputers) {
 $result=receive-job -Name $onlineComputer.Name -Keep
 
 Write-Progress -Activity "Retrieving results of availability....." -status "Service result for $onlineComputer.Name" -percentComplete ($onlineComputers.IndexOf($onlinecomputer) / $onlineComputers.count*100)
 #Contains MachineName, service available
 if([bool]($result.PSobject.Properties.name -match "MachineName")){
 
 
 $result|select-object MachineName,DisplayName,Status |export-csv -Path $ServiceStatusFile -NoTypeInformation -Append
 
 }
 
 else{
 
 $result|Out-File -FilePath $noServiceFile -Append
 
 }

}
#Remove all completed job
Remove-Job -State Completed

Import-Csv $ServiceStatusFile|ft -AutoSize
Advertisements

[POWERSHELL] CHECK QUEST KACE ONE AGENT SERVICE FOR MULTIPLE COMPUTER (VERSION #3)

Here is the #3 version, changes as below:

  • use of Win32_PingStatus for a ping timeout of 1500ms because Test-Connection defaults to 4 seconds and there is no option to change that.
  • Added error handling with try/catch
  • use Start-Job to start checking for service status once found PC is online.

 

#The OU to search
 $SearchBase = "OU=COMPUTER,OU=World_First,DC=domain,DC=local"

#output files
 $ServiceStatusFile = "ServiceStatus.csv"
 $OfflineFile = "Offline.csv"
 $noServiceFile = "noService.csv"

#Search result of computers (Windows only)
 $Computers = Get-ADComputer -Filter {OperatingSystem -Like "Windows*"} -SearchBase $SearchBase -SearchScope Subtree|sort Name

#Array for online computers
 $onlineComputers = @()

#Array for offline computers
 $offlineComputers = @()

$func={

function checkService{

param (
 [parameter(Mandatory=$true,ValueFromPipeline=$true)]
 [string]$client
 )

try{

$serviceStatus=get-service -DisplayName "Quest KACE One Agent" -ComputerName $client -ErrorAction SilentlyContinue -ErrorVariable ServiceNotAvailable|select-object MachineName,DisplayName,Status

if($serviceStatus){
 return $serviceStatus
 }

else{

write-warning -Message "$client,$ServiceNotAvailable"
 $output = $client +"," + $ServiceNotAvailable
 return $output
 }

}
 Catch [System.Management.Automation.CmdletInvocationException]{

write-warning -Message "$client,$ServiceNotAvailable"
 $output = $client +"," + $ServiceNotAvailable.Message
 return $output

}

Catch{
 $ErrorMessage = $_.Exception.Message
 $FailedItem = $_.Exception.ItemName

}

}
 }




#Check if computers are online
 foreach($computer in $computers) {

$client = $computer.Name
 #win32_PingStatus variables
 $filter='Address="{0}" and Timeout={1}' -f $client, $timeout
 $timeout= 1500
 Write-Progress -Activity "Checking computer availablilty....." -status "Ping computer $client" -percentComplete ($computers.IndexOf($computer) / $Computers.count*100)

if((Get-WmiObject Win32_PingStatus -Filter $filter).StatusCode -eq '0'){
 Start-Job -ScriptBlock {checkService $args[0]} -ArgumentList @($client) -Name "$client" -InitializationScript $func
 $onlineComputers= $onlineComputers+($computer)

}
 else{

$offlineComputers= $offlineComputers+($computer)
 Write-Warning -Message "$client is offline"

$client|Out-File -FilePath $OfflineFile -Append
 }

}







Get-Job|Wait-Job

foreach($onlineComputer in $onlineComputers){




$result=receive-job -Name $onlineComputer.Name -Keep

#Contains MachineName, service available
 if([bool]($result.PSobject.Properties.name -match "MachineName")){
 $result|select-object MachineName,DisplayName,Status|export-csv -Path $ServiceStatusFile -NoTypeInformation -Append

}

else{

$result|Out-File -FilePath $noServiceFile -Append

}

}

Remove-Job -State Completed

import-csv -Path ServiceStatus.csv

[POWERSHELL] CHECK QUEST KACE ONE AGENT SERVICE FOR MULTIPLE COMPUTER (VERSION #2)

Here is the version #2 where the source of computer is retrieve from the AD by searching on specific OU defined on $SearchBase. The search will be recursive and include only Windows machine with a progress bar.

Once the search is completed, it will start checking the availablity of the machine with test-connection. It will then check the service status of the online PC.

Once done, the result will be saved to 3 files.

  1. ServiceStatus.csv = Machine that had successfully return the service status
  2. Offline.csv = Offline machine
  3. noService.csv = Machine that have issue with getting the service status

 

#The OU to search
$SearchBase = "OU=COMPUTER,DC=domain,DC=local"

$ServiceStatusFile = "ServiceStatus.csv"
$OfflineFile = "Offline.csv"
$noServiceFile = "noService.csv"

#Search result of computers (Windows only)
$Computers = Get-ADComputer -Filter {OperatingSystem -Like "Windows*"} -SearchBase $SearchBase -SearchScope Subtree -ErrorAction Stop -ErrorVariable NoComputers|sort Name
if($noComputers){
 Write-Warning -Message "Unable to get computer from AD"
 Exit
}

#Service to check
$Service = "Quest KACE One Agent"

#Array for offline computers
$offlineComputers = @()

#Array for online computers
$onlineComputers = @()

##Array for computers without service
$noServiceComputers = @()

#Check if computers are online
foreach($computer in $computers) {
 
 $client = $computer.Name

if (Test-Connection -Computername $client -BufferSize 16 -Count 1 -Quiet){

Write-Progress -Activity "Checking computer availablilty....." -status "Ping computer $client" -percentComplete ($computers.IndexOf($computer) / $Computers.count*100)
 $onlineComputers= $onlineComputers+($computer)


 }
 else{
 
 $offlineComputers= $offlineComputers+($computer)
 Write-Warning -Message "$client is offline"
 
 $client|Out-File -FilePath $OfflineFile -Append
 }

}

#Check if service is available on online computers
foreach($onlineComputer in $onlineComputers){
 $client = $onlineComputer.Name
 Write-Progress -Activity "Checking service availablilty....." -status "Check service on computer $client" -percentComplete ($onlineComputers.IndexOf($onlineComputer) / $onlineComputers.count*100)
 $Servicestatus = get-service -DisplayName $Service -ComputerName $client -ErrorAction SilentlyContinue -ErrorVariable ServiceNotAvailable|select-object MachineName,DisplayName,Status|export-csv -Path $ServiceStatusFile -NoTypeInformation -Append
 if($ServiceNotAvailable){
 Write-Warning -Message "$client : $ServiceNotAvailable"
 #Write-Warning -Message "$client does not have Quest Kace One Agent"
 $noServiceComputers = $noServiceComputers+($computer)
 $client|Out-File -FilePath $noServiceFile -Append
 }
 
 }

import-csv -Path ServiceStatus.csv

Creating a form with .NET library in PowerShell

Below is an example of calling .NET library with PowerShell to build a form. The form will have a textbox for user to input a computer name to ping.

Web link reference included in the comment

#http://social.technet.microsoft.com/wiki/contents/articles/28824.how-to-add-a-graphical-user-interface-to-your-powershell-functions-using-the-net-systemwindowsform-class.aspx
#region Boring beginning stuff
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
#endregion
  
#region begin to draw forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Computer Pinging Tool"
$Form.Size = New-Object System.Drawing.Size(300,170)
$Form.StartPosition = "CenterScreen"
$Form.KeyPreview = $True
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
  
$label = New-Object System.Windows.Forms.label
$label.Location = New-Object System.Drawing.Size(5,5)
$label.Size = New-Object System.Drawing.Size(240,30)
$label.Text = "Type any computer name to test if it is on the network and can respond to ping"
$Form.Controls.Add($label)
$textbox = New-Object System.Windows.Forms.TextBox
$textbox.Location = New-Object System.Drawing.Size(5,40)
$textbox.Size = New-Object System.Drawing.Size(120,20)
#$textbox.Text = "Select source PC:"
$Form.Controls.Add($textbox)
  
$ping_computer_click =
{
#region Actual Code
  
$statusBar1.Text = "Pinging..."
$ComputerName $textbox.Text
  
if (Test-Connection $ComputerName -quiet -Count 2){
Write-Host -ForegroundColor Green "Computer $ComputerName has network connection"
$result_label.ForeColor= "Green"
$result_label.Text = "System Successfully Pinged"
}
Else{
Write-Host -ForegroundColor Red "Computer $ComputerName does not have network connection"
$result_label.ForeColor= "Red"
$result_label.Text = "System is NOT Pingable"
}
  
$statusBar1.Text = "Testing Complete"
#endregion
}
  
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(140,38)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click($ping_computer_click)
$Form.Controls.Add($OKButton)
  
$result_label = New-Object System.Windows.Forms.label
$result_label.Location = New-Object System.Drawing.Size(5,65)
$result_label.Size = New-Object System.Drawing.Size(240,30)
$result_label.Text = "Results will be listed here"
$Form.Controls.Add($result_label)
  
$statusBar1 = New-Object System.Windows.Forms.StatusBar
$statusBar1.Name = "statusBar1"
$statusBar1.Text = "Ready..."
$form.Controls.Add($statusBar1)
  
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){& $ping_computer_click}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$Form.Close()}})
#endregion begin to draw forms
  
#Show form
$Form.Topmost = $True
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

[PowerShell] Send email from Gmail with attachment on PowerShell

Credits goes to 

I modified the script a little bit to pick up the log from c:\temp\ daily where the filename is made up of yyyymmdd

$a = Get-Date -Format yyyyMMdd

$smtpClient = new-object system.net.mail.smtpClient 
$smtpClient.Host = 'smtp.gmail.com'
$smtpClient.Port = 587
$smtpClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username@gmail.com", "password");

$emailFrom = "sender@domain.com"
$emailTo = "receipt1@domain.com,receipt2@domain.com"
$Subject = "Backup log for " +$a
$Body = "Please refer to attached logfile."

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $EmailFrom
$emailMessage.To.Add($EmailTo)
$emailMessage.Subject = $Subject
$emailMessage.Body = $Body
$emailMessage.Attachments.Add("C:\temp\libra_backup"+ $a+".txt")
$SMTPClient.Send($emailMessage)
$emailMessage.Attachments.Dispose()

 

For more information on Get-Date format, please click here

[PowerShell] Retrieving hard disk health through SMART status

To get the SMART status of the hard disk with PowerShell, you could run the following:

Get-WmiObject win32_diskdrive|Select-Object Status

 

Below is a sample script to check multiple PC’s SMART status by getting a list of PC names from a text file.

$allComputers=Get-Content -Path C:\temp\pc.txt

foreach($computer in $allComputers)
{

$isonline=test-connection -Quiet -count 2 -ComputerName $computer
if($isonline)
{

$diskStatus= Get-WmiObject win32_diskdrive -ComputerName $computer|Select-Object -ExpandProperty Status

Write-Host -ForegroundColor Green "$computer is online and disk status is $diskStatus";
$diskStatus="";
}
else
{
Write-Host -ForegroundColor Red "$computer is offline";
}

}

WMI Explorer for those who are interest in WMI programming

For those of you who are interest in WMI programming, tools like WMI code Creator and Scriptomatic can be useful. The command wbemtest might come in handy sometimes as well but I find that WMI Explorers are more useful when you would like to know what classes are avaiable and when you would like to run and see what results does the Query produces.

I find the following three WMI Explorer to be very useful:

Happy programming 🙂