Check if a Process is running. If not start it and send an Email


Get-Process (gps) Gets the processes that are running on the local computer or a remote computer.

# 
# =======PowerShell Script========================================
# COMMENT: Check if Process is running. If not start it and send an Email
$myproc = "program my process.exe"
if (!(gps myproc -ea 0)) { 
    $MyProcCMD = "c:\program files\$myproc /switches"  #<-- cmd to start proc
    $wmi = ([wmiclass]"win32_process").Create($MyProcCMD) 
    if ($wmi.returnvalue -eq 0) { 
        $smtp = new-object system.net.mail.smtpClient("smtp.domain.com") 
        $mail = new-object System.Net.Mail.MailMessage 
        $mail.from = "someone@domain.com" 
        $mail.to.add = "someone else@domain.com"
        $mail.subject = "MyProc was in Stopped state. Successfully restarted." 
        $mail.body = "bla-bla-bla"
        $mail.IsBodyHtml = $true 
        $smtp.send($mail) 
		}
    } 
	
############# End POSH Script ###############
Advertisements

Remote Software Inventory

So you want to check a server named firefly and see what’s installed on it and the version
This script querys the remote  computer’s uninstall keys and writes out the Display Name and Display version
of everything it finds.
 
 
#<—-Start PowerShell Script ——->
$computer = "firefly"
$uninstallkey="SoftwareMicrosoftWindowsCurrentVersionUninstall"
$keytype=[Microsoft.Win32.RegistryHive]::LocalMachine
$remotebase=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($keytype,$computer)
$regkey=$remotebase.OpenSubKey($uninstallkey)
 
$regkey.GetSubKeyNames() | foreach {
     $DisName = $remotebase.OpenSubKey("$uninstallkey$_").GetValue("DisplayName")
  $DisVer = $remotebase.OpenSubKey("$uninstallkey$_").GetValue("DisplayVersion")
  Write-Host $DisName "`t" $DisVer
}
 
#<—- End Script ———>
 
Looking for something more specific? Like Adobe Flash?
 
#<—-Start Script ——->

 

$computer = "firefly"
$uninstallkey="SoftwareMicrosoftWindowsCurrentVersionUninstall"
$keytype=[Microsoft.Win32.RegistryHive]::LocalMachine
$remotebase=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($keytype,$comp­uter)
$regkey=$remotebase.OpenSubKey($uninstallkey)
$regkey.GetSubKeyNames() | where {$_ -like "adobe flash*"} | foreach {
        $remotebase.OpenSubKey("$uninstallkey$_").GetValue("DisplayVersion")
}

#<—- End Script ———>