Pages

1/30/2014

Run a command on every server in domain. (Powershell v2 compatible)



#############################################################################################################
#
#   run-remote.ps1
#
#   run a command on each server in the domain
#   log results to file and e-mail report of failures
#

$rcmd = "ping cmsweb"
$success = "Reply from"
$logfile = ".\run-remote.log"
$outfile = ".\cmsweb-test.html"
$rptname = "cmsweb reachability"
$recipient = "user@domain.com"

############################################################################################
# Create $list of server names for all Windows servers in Active Directory
#
$strCategory = "computer" 
$strOS = "Windows*Server*"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("OperatingSystem=$strOS")
$colProplist = "dnshostname"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults) 
 { 
  $objComputer = $objResult.Properties;  
  $Server = $objComputer.dnshostname
  $Server = $Server -replace "\s{2,}", ""
  $Server = $Server -replace "\.usa\.domain\.com", ""
  if ($Server) { $list = $list + $Server } #skip a null value
  } 
$list = $list | sort-object
###

clear-host
$today = get-date
$today | out-file $logfile

"Run:  $rcmd"
$username = read-host "Logon"
$pw = read-host -AsSecureString "Password"
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
   [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw))
############################################################################################
# Report Heading

$report=@'
<STYLE>
BODY{font-family: Verdana, Arial, Helvetica, sans-serif;font-size:12;font-color: #000000}
TABLE{border-width: 2px;padding: 1px;border-style: solid;border-color: black;border-collapse: collapse;} 
TH{border-width: 2px;padding: 1px;border-style: solid;border-color: black;background-color: #dddddd;font-size:16;font-weight:bold}
TD{border-width: 2px;padding: 2px;border-style: solid;border-color: black;background-color: #efefef; font-size:12;font-weight:normal} 
TD.error{border-width: 2px;padding: 2px;border-style: solid;border-color: black;background-color: #ffffff;font face="monospace";font-size:10;font-color: #cccccc}
</STYLE> 
<HTML> 
<HEAD> 
<TITLE></TITLE> 
</HEAD> 
<BODY> 
<H2>Server Time Issues</H2> 
<H4>
'@
$report+=$today
$report+="Command:  $rcmd"
$report+="</H4><table><th>Server</th><th>IP Number</th><th>Result</th></tr>"

############################################################################################
# Run for every server in list 


foreach ($computer in $list) {
 If (Test-Connection -computername $computer -Quiet -count 1){ #respond to ping?
  #get IP address
  $conn = test-connection -computername $computer -count 1
  $ip = $conn.IPV4Address.IPAddressToString
  "Executing remotely from $computer - $ip"
  $cmd = "c:\util\psexec.exe /acceptEula \\$computer -u $username -p $pass -w c:\ $rcmd"
  $result = invoke-expression $cmd
  if ($result -like "$success*") { #success 
   "$computer - $ip" + ": <$rcmd> -> Success" | out-file $logfile -append
#   $report+=('<tr><td>' + $computer + '</td>') 
#   $report+=('<td >$ip</td>')  
#   $report+=('<td >Success</td>')  
#   $report+=('</tr>')
   } 
  else { #fail 
   "$computer - $ip" + ": <$rcmd> -> Fail" | out-file $logfile -append
   $report+=('<tr><td>' + $computer + '</td>') 
   $report+=('<td >$ip</td>')  
   $report+=('<td >Fail</td>')  
   $report+=('</tr>')
   }
  }#end if connection
 }#end foreach computer
clear-host
notepad $logfile

$report+="</TABLE></BODY></HTML>" 

$report | out-file $outfile 

############################################################################################
#e-mail the report

$messageSubject = $rptname
$smtpServer = "smtp.domain.com"
$smtpFrom = "noreply@domain.com"
$smtpTo = $recipient
$message = $report
send-mailmessage -to $smtpTo -cc "admin@domain.com" -from $smtpFrom -subject $messageSubject -body $message -smtpserver $smtpServer -BodyAsHtml 
###

1/29/2014

NMCap: the easy way to Automate Capturing - Network Monitor - Site Home - TechNet Blogs

NMCap: the easy way to Automate Capturing - Network Monitor - Site Home - TechNet Blogs: NMCap: the easy way to Automate Capturing

newsoft's tech blog: Pentester trick #8: command-line sniffing made easy

newsoft's tech blog: Pentester trick #8: command-line sniffing made easy: command-line sniffing made easy



The most reliable and lightweight tool I know is ... the one made by Microsoft, a.k.a. Microsoft Network Monitor. It relies on Windows built-in packet capture features, therefore leaving minimal footprint on the target system. It can run without install. It works on all Microsoft-supported Windows versions, in x86, x64 and even IA64 flavors.

How to use it ?
  1. Download and install Microsoft Network Monitor on a standalone computer.
  2. Upload nmconfig.exe and nmcap.exe on the target computer.
  3. Enable the Microsoft Network Monitor Driver: nmconfig /install
  4. Test: nmcap /displaynetworks
  5. Sniff all TCP traffic on every local interface: nmcap /network * /capture tcp /File tcp.cap
  6. Disable the Microsoft Network Monitor Driver: nmconfig /uninstall
(Caveat: the capture file format is not Winpcap-compatible. However, Wireshark (and others) know how to read it.)

Technical thoughts: Dependence between ARP max age time and MAC-address aging on Cisco switches

Technical thoughts: Dependence between ARP max age time and MAC-address aging on Cisco switches: Dependence between ARP max age time and MAC-address aging on Cisco switches

1/15/2014

Check Time on VMWare Hosts

Check Time on VMware Hosts Similar to my previous script reporting on Windows Server time.

##########################################################################################
# esx-time.ps1
#
# Gather local time and time zone for all servers in AD.
# Report servers with time different from machine on which script is run
# E-Mail report to designated recipient.
#
# Requires vmware power cli
# "set-powercliconfiguration -InvalidCertificateAction Ignore" to ignore cert errors
#
#
##########################################################################################

Add-PSSnapin VMware.VimAutomation.Core  -ErrorAction SilentlyContinue

#VCS
$vcs="privcs01" # PRIVDIVC1, PRIDEVVC1, PRIVCSVDI01, SECVCS01

# E-Mail Recipient 
$recipient="administrator@DOMAIN.com"

# Greatest acceptable time difference
$maxdiff=59

$outfile = "c:\dev\esx-time.html"
$user = "usa\svc_vmware"
$pw = Read-Host "Password for $user" -AsSecureString
$rootpw = Read-Host "Password for root" -AsSecureString
#convert $pw to plain text
    $pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
        [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw))
#convert $rootpw to plain text
    $rootpass = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
        [Runtime.InteropServices.Marshal]::SecureStringToBSTR($rootpw))

############################################################################################
# Get List of Host Names from VCS server
#
write-host "Getting Server List from VCS"
connect-viserver -Server $vcs -User $user -Password $pass -ErrorAction SilentlyContinue > $null
$hosts = get-vmhost
disconnect-viserver -confirm:$false -ErrorAction SilentlyContinue > $null
$list = @()
foreach ($Server in $hosts) { 
  $Server = $Server -replace "\s{2,}", ""
  $Server = $Server -replace "\.usa\.DOMAIN\.com", ""
  if ($Server) { $list = $list + $Server } #skip a null value
  } 
$list = $list | sort-object
write-host "COMPLETE"
###


#clear-host
$today = get-date

############################################################################################
# Report Heading

$report=@'
<STYLE>
BODY{font-family: Verdana, Arial, Helvetica, sans-serif;font-size:12;font-color: #000000}
TABLE{border-width: 2px;padding: 1px;border-style: solid;border-color: black;border-collapse: collapse;} 
TH{border-width: 2px;padding: 1px;border-style: solid;border-color: black;background-color: #dddddd;font-size:16;font-weight:bold}
TD{border-width: 2px;padding: 2px;border-style: solid;border-color: black;background-color: #efefef; font-size:12;font-weight:normal} 
TD.error{border-width: 2px;padding: 2px;border-style: solid;border-color: black;background-color: #ffffff;font face="monospace";font-size:10;font-color: #cccccc}
</STYLE> 
<HTML> 
<HEAD> 
<TITLE></TITLE> 
</HEAD> 
<BODY> 
<H2>VMWare Host Time Issues</H2> 
<H4>
'@
$report+=$today
$report+="</H4><table><th>Server</th><th>Date</th><th>Time</th><th>Off(s)</th></tr>"

############################################################################################
# Query every server in list 

write-host "Checking each server:"

foreach ($item in $list) {
  $flag=" "
  $dt=""
  $srvtime=""
  $hour = ""
  $min = ""
  $sec = ""
  $month = ""
  $day = ""
  $year = ""
  $comp = ""
  $d = ""
  $t = ""
   $computer = $item 
  $comp = "{0,-16}" -f $computer
  $comp = $comp.padright(16," ")

write-host ">$computer<"

 If (Test-Connection -computername $computer -Quiet -count 1){ #respond to ping?
write-host "    PING - SUCCESS"
write-host "    Time Query"
  $output=""
  $block = @'
  $machine = $args[0]
  $pass = $args[1]
  Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
  connect-viserver -server $machine -user "root" -password $pass -WarningAction SilentlyContinue -ErrorAction SilentlyContinue > $null
#  $time = (get-view serviceinstance).CurrentTime()
#  $time
  ((get-view serviceinstance).CurrentTime())
'@
  $sb = [scriptblock]::create($block)
  $args=@()
  $args = ($computer, $rootpass)
  start-job -name $computer -scriptblock $sb -argumentlist $args > $null
  Wait-Job -name $computer -Timeout 60 > $null 
  $output = Receive-Job $computer
#$output
  #disconnect-viserver -server $computer -confirm:$false #-ErrorAction SilentlyContinue > $null
  if ($output) {  
  $dt = $output
write-host "DT=$dt"
  }
  else {   
  write-host "$computer`tERROR:  No time Query Output"
   $report+='<tr><td class="error">' + $comp + '</td>' 
    $report+='<td colspan="4" class="error">' + "ERROR - No response to date/time query" + '</td>'  
   $report+='</tr>' 
  continue
  } # end if output
 }#end if ping
 else { # No PING response
  write-host "$computer`tERROR:  No PING Response"
   $report+='<tr><td class="error">' + $comp + '</td>' 
          $report+='<td colspan="4" class="error">' + "CONNECTION ERROR:  No response to PING" + '</td>'  
   $report+='</tr>' 
  continue
  }
 [string] $month = [System.Convert]::ToString($dt.Month)
 $month = $month.padleft(2,"0")
 [string] $day = [System.Convert]::ToString($dt.Day)
 $day = $day.padleft(2,"0")
 [string] $year = [System.Convert]::ToString($dt.Year)
 $year = $year.padleft(4,"0")
 $d = $month + "/" + $day + "/" + $year
write-host "    Date=$d"
 [string] $hour = [System.Convert]::ToString($dt.Hour)
 $hour = $hour.padleft(2,"0")
 [string] $min = [System.Convert]::ToString($dt.Minute)
 $min = $min.padleft(2,"0")
 [string] $sec = [System.Convert]::ToString($dt.Second)
 $sec = $sec.padleft(2,"0")
 $t = $hour + ":" + $min + ":" + $sec
write-host "    Time=$t"
 if ($dt) { #$dt not null
  $srvtime=($dt.Minute)*60+($dt.Second)
  $refmin = ""
  $refsec = ""
  $now = get-date
  $cmptime = ($now.Minute)*60+($now.Second)
  [string] $min = [System.Convert]::ToString($now.Minute)
  $refmin = $min.padleft(2,"0")
  [string] $sec = [System.Convert]::ToString($now.Second)
  $refsec = $sec.padleft(2,"0")
  $reftime=$refmin+":"+$refsec
  #Compare server time to script machine time.
  $diff=$cmptime-$srvtime
write-host "    Time Diff=$diff"
  if (($diff -gt $maxdiff) -or ($diff -lt (-1*$maxdiff))) { $flag="*" }
   write-host "$flag$comp`t$d`t$t`t$flag$diff$flag`t" $timezone.Description 
   if ($flag -eq "*") { #time difference is too great
    $report+=('<tr><td>' + $comp + '</td>') 
    $report+=('<td >' + $d + '</td>')  
    $report+=('<td >' + $t + '</td>')  
    $report+=('<td >' + $diff + '</td>')  
    $report+=('</tr>') 
    }
  }# end if null 
}#end foreach computer
###
$report+="</TABLE></BODY></HTML>" 

$report | out-file $outfile 

############################################################################################
#e-mail the report

$messageSubject = "VMWare Host Time Report"
$smtpServer = "smtp.DOMAIN.com"
$smtpFrom = "noreply@DOMAIN.com"
$smtpTo = $recipient
$message = $report
send-mailmessage -to $smtpTo -from $smtpFrom -subject $messageSubject -body $message -smtpserver $smtpServer -BodyAsHtml 
###


remove-job * -force

1/03/2014

Putting code example in HTML documents

Use the XMP tag
<pre><code><xmp>
   ...code...
</xmp></code></pre>

Powershell Format Operator

Powershell Format Operator "-f"




OperatorExampleResultsDescription


{0}Display a particular element"{0} {1}" -f "a", "b"a b
{0:x}Display a number in Hexadecimal"0x{0:x}" -f 1813420x2c45e
{0:X}Display a number in Hexadecimal uppercase"0x{0:X}" -f 1813420x2C45E
{0:dn}Display a decimal number left justified, padded with zeros"{0:d8}" -f 300000003
{0:p}Display a number as a percentage"{0:p}" -f .12312.30 %
{0:c}Display a number as currency"{0:c}" -f  12.34$12.34
{0,n}Display with field width n, left aligned"|{0,5}|" -f "hi"|   hi|
{0,-n}Display with field width n, right aligned"|{0,-5}| -f "hi"|hi   |
{0:hh}
{0:mm}
Display the hours and minutes from a date time value"{0:hh}:{0:mm}" -f (Get-Date)01:34
{0:C}Display using the currency symbol for the current culture"|{0,10:C}|" -f 12.3|  $12.40|

1/02/2014

Powershell:  WMI Timeouts locking up scripts

There are a few folks with suggestions out there.  One is to use a job to run the query.  Unfortunately this leaves those jobs stuck out there so if a lot of my servers have WMI screwed up I suppose doing this could crash my management machine.
This example is to get the current time and time zone from every server machine in my AD domain. 

##########################################################################################
# CHECK-TIME.PS1
#
# Gather local time and time zone for all servers in AD.  Convert times to GMT & compare
# Report servers with time different from machine on which script is run
# E-Mail report to designated recipient.
#
##########################################################################################


# E-Mail Recipient 
$recipient="user@domain.com"

# Greatest acceptable time difference
$maxdiff=59

$outfile = "c:\dev\check-time.html"
If (Test-Path $outfile){
	Remove-Item $outfile
}

$logfile = "c:\dev\check-time.log"
If (Test-Path $logfile){
	Remove-Item $logfile
}

$mytimezone = get-wmiobject Win32_Computersystem -property CurrentTimeZone, daylightineffect
$myoffset = $mytimezone.CurrentTimeZone #GMT offset in minutes
$mydst = $mytimezone.daylightineffect
   if ($mydst) {
   	$myoffset+=60
   	}

##########################################################################################
# FUNCTION:  Check for TCP response on $port

function Test-Port {  
    Param(  
      [string] $srv,  
      $port=135,  
      $timeout=1500,  
      [switch]$verbose  
    )  
    # TCP connect $port
    $ErrorActionPreference = "SilentlyContinue"
    $tcpclient = new-Object system.Net.Sockets.TcpClient 
    $iar = $tcpclient.BeginConnect($srv,$port,$null,$null) 
    $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false) 
    # Check to see if the connection is done 
    if(!$wait) 
    { 
    # Close connection, report timeout 
    $tcpclient.Close()  
        if($verbose){write-host "Connection Timeout" }  
        Return $false  
    }  
    else  
    {  
        # Close connection, report any error
         $error.Clear()  
         $tcpclient.EndConnect($iar) | out-Null  
         if(!$?){if($verbose){write-host $error[0]};$failed = $true}  
         $tcpclient.Close()  
     }  
     # Return $true if connection established  
      if($failed){  
          return $false  
      } else {  
          return $true  
      }  
    } # http://technet.microsoft.com/en-us/library/ff730959.aspx


#
# End of functions
##########################################################################################


############################################################################################
# Create $list of server names for all Windows servers in Active Directory
#
$strCategory = "computer" 
$strOS = "Windows*Server*"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("OperatingSystem=$strOS")
$colProplist = "dnshostname"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults) 
 { 
  $objComputer = $objResult.Properties;  
  $Server = $objComputer.dnshostname
  $Server = $Server -replace "\s{2,}", ""
  $Server = $Server -replace "\.usa\.domain\.com", ""
  if ($Server) { $list = $list + $Server } #skip a null value
  } 
$list = $list | sort-object
###

clear-host
$today = get-date

############################################################################################
# Report Heading

$report=@'
<STYLE>
BODY{font-family: Verdana, Arial, Helvetica, sans-serif;font-size:12;font-color: #000000}
TABLE{border-width: 2px;padding: 1px;border-style: solid;border-color: black;border-collapse: collapse;} 
TH{border-width: 2px;padding: 1px;border-style: solid;border-color: black;background-color: #dddddd;font-size:16;font-weight:bold}
TD{border-width: 2px;padding: 2px;border-style: solid;border-color: black;background-color: #efefef; font-size:12;font-weight:normal} 
TD.error{border-width: 2px;padding: 2px;border-style: solid;border-color: black;background-color: #ffffff;font face="monospace";font-size:10;font-color: #cccccc}
</STYLE> 
<HTML> 
<HEAD> 
<TITLE></TITLE> 
</HEAD> 
<BODY> 
<H2>Server Time Issues</H2> 
<H4>
'@
$report+=$today
$report+="</H4><table><th>Server</th><th>Date</th><th>Time</th><th>Off(s)</th><th>Time Zone</th></tr>"

############################################################################################
# Query every server in list 

foreach ($result in $list) {
	 $flag=" "
	 $timezone=""
	 $dt=""
	 $srvtime=""
	 $hour = ""
	 $min = ""
	 $sec = ""
	 $comp = ""
	 $d = ""
	 $t = ""
 	 $computer = $result 
	 $comp = "{0,-16}" -f $computer
	 $comp = $comp.padright(16," ")
#write-host ("$Computer"+":") 
#("$Computer"+":")|out-file $logfile -append
	If (Test-Connection -computername $computer -Quiet -count 1){ #respond to ping?
	    $a = Test-Port $computer 
	    $dst=""
	    $timezone=""
	    $output=""
	    $offset=""
	    if ($a) { # resonds on RPC?
		#timezone
			$block = "get-wmiobject Win32_Computersystem -computer $computer -property CurrentTimeZone, daylightineffect"
			$sb = [scriptblock]::create($block)
			start-job -name $computer $sb > $null
			Wait-Job -name $computer -Timeout 10 > $null
			$output = Receive-Job $computer
			$block = "Get-WmiObject -class Win32_TimeZone -ComputerName $computer"
			$sb = [scriptblock]::create($block)
			start-job -name $computer $sb > $null
			Wait-Job -name $computer -Timeout 10 > $null
			$timezonedesc = Receive-Job $computer
			$tz = $timezonedesc.description
			if ($output -ne $null) {  
			    $timezone = $output
			    $offset = $timezone.CurrentTimeZone #GMT offset in minutes
			    $dst = $timezone.daylightineffect
			    if ($dst) {
			    	$offset+=60
			    	}
				 }
			else {   
			write-host "$computer`tERROR:  No WMI Query Output"
			"$computer`tERROR:  No WMI Query Output" | out-file $logfile -append
			 $report+='<tr><td class="error">' + $comp + '</td>' 
		         $report+='<td colspan="4" class="error">' + "WMI ERROR - No response to timezone query" + '</td>'  
			 $report+='</tr>' 
			continue
			}#end if output
		#date/time
			$block = "Get-WmiObject -class win32_localtime -ComputerName $computer"
			$sb = [scriptblock]::create($block)
			start-job -name $computer $sb > $null
			Wait-Job -name $computer -Timeout 10 > $null 
			$output = Receive-Job $computer
			if ($output -ne $null) {  
			$dt = $output
			}
			else {   
			write-host "$computer`tERROR:  No WMI Query Output"
			"$computer`tERROR:  No WMI Query Output" | out-file $logfile -append
			 $report+='<tr><td class="error">' + $comp + '</td>' 
		         $report+='<td colspan="4" class="error">' + "WMI ERROR - No response to date/time query" + '</td>'  
			 $report+='</tr>' 
			continue
			} # end if output
	    }#end if port
	    else {# No response on port
		write-host "$computer`tERROR:  No RPC Response"
		"$computer`tERROR:  No RPC Response" | out-file $logfile -append
		 $report+='<tr><td class="error">' + $comp + '</td>' 
	         $report+='<td colspan="4" class="error">' + "RPC ERROR - No response on port " + $port + '</td>'  
		 $report+='</tr>' 
		continue
		}    
	}#end if ping
	else { # No PING response
		write-host "$computer`tERROR:  No PING Response"
		"$computer`tERROR:  No PING Response" | out-file $logfile -append
		 $report+='<tr><td class="error">' + $comp + '</td>' 
	         $report+='<td colspan="4" class="error">' + "CONNECTION ERROR:  No response to PING" + '</td>'  
		 $report+='</tr>' 
		continue
		}
	[string] $month = [System.Convert]::ToString($dt.Month)
	[string] $day = [System.Convert]::ToString($dt.Day)
	[string] $year = [System.Convert]::ToString($dt.Year)
	[string] $hour = [System.Convert]::ToString($dt.Hour)
	$hour = $hour.padleft(2,"0")
	[string] $min = [System.Convert]::ToString($dt.Minute)
	$min = $min.padleft(2,"0")
	[string] $sec = [System.Convert]::ToString($dt.Second)
	$sec = $sec.padleft(2,"0")
	$d = $month + "/" + $day + "/" + $year
	$t = $hour + ":" + $min + ":" + $sec
	if ($dt) { #$dt not null
		#convert to object
		$dObj = get-date "$d $t"
		#adjust to GMT
		$srvtime=$dObj.addminutes(-$offset)
#write-host "    $computer time=$srvtime"	
#"    $computer time=$srvtime" | out-file $logfile -append
		$now = get-date
		$cmptime = $now.addminutes(-$myoffset)
		[string] $min = [System.Convert]::ToString($now.Minute)
		$diff = $cmptime-$srvtime
		$diff = $diff.totalseconds
		$diff = [decimal]::round($diff)
		if (($diff -gt $maxdiff) -or ($diff -lt (-1*$maxdiff))) { $flag="*" }
			write-host "$flag$comp`t$d`t$t`t$flag$diff$flag`t$tz"
			"$flag$comp`t$d`t$t`t$flag$diff$flag`t$tz" | out-file $logfile -append
			$zone = $timezonedesc.Description.tostring()
			if (-not($timezonedesc )) { #no zone returned
				$flag="*"
				$zone = "ERROR:  Invalid Time Zone Response"
				write-host "*$zone*"
				"*$zone*" | out-file $logfile -append
				}
			if ($flag -eq "*") { #time difference is too great
				$report+=('<tr><td>' + $comp + '</td>') 
				$report+=('<td >' + $d + '</td>')  
				$report+=('<td >' + $t + '</td>')  
				$report+=('<td >' + $diff + '</td>')  
				$report+=('<td >' + $zone + '</td>')  
				$report+=('</tr>') 
				}
	 }# end if null 
}#end foreach computer
###
$report+="</TABLE></BODY></HTML>" 

$report | out-file $outfile 

############################################################################################
#e-mail the report

$messageSubject = "Server Time Report"
$smtpServer = "smtp.domain.com"
$smtpFrom = "noreply@domain.com"
$smtpTo = $recipient
$message = $report
send-mailmessage -to $smtpTo -cc "admin@domain.com" -from $smtpFrom -subject $messageSubject -body $message -smtpserver $smtpServer -BodyAsHtml 
###
remove-job * -force

Dealing with WMI Timeouts — Steven Murawski

Dealing with WMI Timeouts — Steven Murawski: Dealing with WMI Timeouts

Get-WmiCustom (aka: Get-WMIObject with timeout!) - musc@> $daniele.work.ToString() - Site Home - MSDN Blogs

Get-WmiCustom (aka: Get-WMIObject with timeout!) - musc@> $daniele.work.ToString() - Site Home - MSDN Blogs: Unfortunately, the Get-WmiObject cmdlet does not let you specify a timeout. Therefore I cooked my own function which has a compatible behaviour to that of Get-WmiObject, but with an added “-timeout” parameter which can be set.

Function Get-WmiCustom([string]$computername,[string]$namespace,[string]$class,[int]$timeout=15) 
{ 
$ConnectionOptions = new-object System.Management.ConnectionOptions 
$EnumerationOptions = new-object System.Management.EnumerationOptions 

$timeoutseconds = new-timespan -seconds $timeout 
$EnumerationOptions.set_timeout($timeoutseconds) 

$assembledpath = "\\" + $computername + "\" + $namespace 
#write-host $assembledpath -foregroundcolor yellow 

$Scope = new-object System.Management.ManagementScope $assembledpath, $ConnectionOptions 
$Scope.Connect() 

$querystring = "SELECT * FROM " + $class 
#write-host $querystring 

$query = new-object System.Management.ObjectQuery $querystring 
$searcher = new-object System.Management.ManagementObjectSearcher 
$searcher.set_options($EnumerationOptions) 
$searcher.Query = $querystring 
$searcher.Scope = $Scope 

trap { $_ } $result = $searcher.get() 

return $result 
}