How do I record an Error in Excell


###### Start Posh Script ######## 

<# ==============================================================================================
Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2011
NAME: ErrorView.ps1
AUTHOR: OldDog
DATE  : 5/18/2012
COMMENT: Ping does not return an Error
 Use Test-Connection instead
==============================================================================================
#>

If ($Error) {
               $ErrorView = "CategoryView"
                $y = $error | select Exception
                $e = $y -split (":")
                $x = $e[2]
                $ws1.Cells.Item($row, 3) = $x
                $ws1.rows.Item($row).Font.ColorIndex = "3"
                $row++
                $Error.Clear()
                $range = $ws1.usedRange
                [void]$range.entireColumn.Autofit()
                $ErrorView = $null
}
Advertisements

WMI hangs and how to avoid them


Once again I was asked to query a couple of thousand servers for some basic information and once again there were some servers that turn into black holes when I run a wmi query against them. They don’t error, they don’t respond and they stop my script dead in its tracks.
I had to go back into my archives to figure out how I solved this the last time and here it is.
These are functions of Powershell version II.


###### Start Posh Script ######## 
 # Use the test-port function to make sure that the RPC port is listening 
  
function Test-Port {   
 Param(    
	[string] $srv,    
	$port=135,    
	$timeout=3000,    
	[switch]$verbose    
	)    
 # Test-Port.ps1    
 # Does a TCP connection on specified port (135 by default)    
 $ErrorActionPreference = SilentlyContinue	    
 # Create TCP Client    
	$tcpclient = new-Object system.Net.Sockets.TcpClient    
 # Tell TCP Client to connect to machine on Port    
	$iar = $tcpclient.BeginConnect($srv,$port,$null,$null)    
 # Set the wait time    
	$wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)    
 # Check to see if the connection is done    
	if(!$wait)    
 {    
 # Close the connection and report timeout    
	$tcpclient.Close()    
	if($verbose){Write-Host Connection Timeout	}    
	Return $false    
 }    
 else    
 {    
 # Close the connection and report the error if there is one    
	$error.Clear()    
	$tcpclient.EndConnect($iar) | out-Null    
	if(!$?){if($verbose){write-host $error[0]};$failed = $true}    
	$tcpclient.Close()    
 }    
 # Return $true if connection Establish else $False    
	if($failed){    
	return $false    
	} else {    
	return $true    
	}    
} # http://technet.microsoft.com/en-us/library/ff730959.aspx
  
# First, make sure the computer is Pingable,
  
If (Test-Connection -computername $server -Quiet -count 1){ 
 # Use the test-port function to make sure that the RPC port is listening 
  
 $a = Test-Port $server   
	If ($a) {    
 # Run your WMI query as a JOB 
	$WMIJob = Get-WmiObject Win32_OperatingSystem -Comp $server -AsJob    
	Wait-Job -ID $WMIJob.ID -Timeout 20 # the Job times out after 20 seconds. 
	$os = Receive-Job $WMIJob.ID
  
	if ($os -ne $null) {   
		switch ($os.version) {    
			5.1.2600 {$osVer = "XP/IIS5.1"}    
			5.2.3790 {$osVer = "Server 2003/IIS6"}    
			6.0.6001 {$osVer = "Server 2008/IIS7"}    
			6.1.7600 {$osVer = "Server 2008 R2/IIS7.5"}    
		 } 

	Else {      
		 $b = $error | select Exception      
		 $E = $b -split (":")      
		 $x = $E[1]      
		 $Error.Clear()      
		 $d = $x.StartsWith(" Access")      
		 If ($d){      
		 Add-Content -Path $strFile "$server `tAccess Denied"
		 }      
		 Else {Add-Content -Path $strFile "$server `t$x"	       
		 }      
		 }
  ############# End POSH Script ###############

# Access denied brings TMI along with it, so I shortened it to just “Access Denied”
# Otherwise give me the whole error. I am writing out my results to a text file, hence
# the Add-Content lines.
Hope this helps you as much as it did me.