Link to home
Start Free TrialLog in
Avatar of REIUSA
REIUSAFlag for United States of America

asked on

Powersshell script stops on error, need to continue and create log file

This script works fine on several servers I ran it against but when I tried a big list of 200 servers I get a error that says access denied and the script stops, I don't know why it won't continue, it has to be a problem accessing one of the servers in the servernames.txt file, I can ping all of the servers in the list. I get another RCP error but it seems to continue fine even with this error.

How can I make it continue and then create a log file with the server name and error? Or at least just make it continue anyways, I really need to get this running today.

Errors
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\TEMP\PS\PS_Print_Info_Details.ps1:29 char:26

At C:\TEMP\PS\PS_Print_Info_Details.ps1:29 char:26
+ $Printers = Get-WMIObject  <<<< Win32_Printer -computername $ServerList

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At C:\TEMP\PS\PS_Print_Info_Details.ps1:29 char:26











# Get server list
$ServerList = Get-Content "servers.txt";



# Create new Excel workbook
$Excel = new-Object -comobject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
$Sheet.Cells.Item(1,1) = "Server Name"
$Sheet.Cells.Item(1,2) = "Printer Name"
$Sheet.Cells.Item(1,3) = "IP Address"
$Sheet.Cells.Item(1,4) = "Driver Name"
$Sheet.Cells.Item(1,5) = "Location"
$Sheet.Cells.Item(1,6) = "Comment"
$Sheet.Cells.Item(1,7) = "Share Name"
$Sheet.Cells.Item(1,8) = "Shared"


$intRow = 2
$WorkBook = $Sheet.UsedRange
$WorkBook.Font.Bold = $True

# Get printer information
$Printers = Get-WMIObject Win32_Printer -computername $ServerList
foreach ($Printer in $Printers)
{
    $Sheet.Cells.Item($intRow, 1) = $Printer.SystemName
    $Sheet.Cells.Item($intRow, 2) = $Printer.Name
    $Ports = Get-WmiObject Win32_TcpIpPrinterPort -computername $ServerList
        foreach ($Port in $Ports)
        {
            if ($Port.Name -eq $Printer.PortName)
            {
            $Sheet.Cells.Item($intRow, 3) = $Port.HostAddress
            }
        }
    $Sheet.Cells.Item($intRow, 4) = $Printer.DriverName
    $Sheet.Cells.Item($intRow, 5) = $Printer.Location
    $Sheet.Cells.Item($intRow, 6) = $Printer.Comment
    $Sheet.Cells.Item($intRow, 7) = $Printer.ShareName
    $Sheet.Cells.Item($intRow, 8) = $Printer.Shared
    $intRow = $intRow + 1
}



$WorkBook.EntireColumn.AutoFit()
$intRow = $intRow + 1
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
$Sheet.Cells.Item($intRow,1) = "Done"

Open in new window

Avatar of sgdought
sgdought

Put this at or near the top to continue on error:
$ErrorActionPreference = "SilentlyContinue"

Avatar of RobSampson
Put this at the top:
$error.clear()
$erroractionpreference = "SilentlyContinue"

and then put this at the bottom to create the log file:
$errorfile = "C:\Errors.log"
if ($Error -ne "") { Write-Output $Error[($Error.count-1)..0] | Out-File $errorfile }


Regards,

Rob.
Avatar of REIUSA

ASKER

Thanks, It didn't show the errors and it created the log file but it didn't put any info in the excel sheet. I ran it on 10 of the 200 servers and it completed fine but when I run it on the whole list it ends and doesn't place any info in the excel sheet.

Any idea how to make it gather info on what it can connect to fine and not stop the whole script because some of them throw up problems? For the log file is it possible to get it to show what server it is erring on instead of just the error message, I could manually remove the offenders from the list too?

Here is the log file for one I ran on a small batch of servers and the Access Denied error seems to be what is stopping it, it needs to just ignore that and keep running on the other servers.
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:32 char:26
+ $Printers = Get-WMIObject <<<<  Win32_Printer -computername $ServerList
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:32 char:26
+ $Printers = Get-WMIObject <<<<  Win32_Printer -computername $ServerList
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:32 char:26
+ $Printers = Get-WMIObject <<<<  Win32_Printer -computername $ServerList
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:32 char:26
+ $Printers = Get-WMIObject <<<<  Win32_Printer -computername $ServerList
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:32 char:26
+ $Printers = Get-WMIObject <<<<  Win32_Printer -computername $ServerList
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESS
DENIED))
At C:\PS\PS.ps1:32 char:26
+ $Printers = Get-WMIObject <<<<  Win32_Printer -computername $ServerList
    + CategoryInfo          : NotSpecified: (:) [Get-WmiObject], UnauthorizedA
   ccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.Pow
   erShell.Commands.GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:37 char:27
+     $Ports = Get-WmiObject <<<<  Win32_TcpIpPrinterPort -computername $Server
List
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:37 char:27
+     $Ports = Get-WmiObject <<<<  Win32_TcpIpPrinterPort -computername $Server
List
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:37 char:27
+     $Ports = Get-WmiObject <<<<  Win32_TcpIpPrinterPort -computername $Server
List
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:37 char:27
+     $Ports = Get-WmiObject <<<<  Win32_TcpIpPrinterPort -computername $Server
List
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\PS\PS.ps1:37 char:27
+     $Ports = Get-WmiObject <<<<  Win32_TcpIpPrinterPort -computername $Server
List
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMExcept
   ion
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
   .GetWmiObjectCommand
 
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESS
DENIED))
At C:\PS\PS.ps1:37 char:27
+     $Ports = Get-WmiObject <<<<  Win32_TcpIpPrinterPort -computername $Server
List
    + CategoryInfo          : NotSpecified: (:) [Get-WmiObject], UnauthorizedA
   ccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.Pow
   erShell.Commands.GetWmiObjectCommand
 
You could try wrapping the getting of the printers in  a loop so you process each server at a time so you can test for the access, log any problems and process the ones that you can access.  The logging could be done by writing to the system logs or using a text base system such as the one I use (http://powershell.com/cs/media/p/10676.aspx)
foreach ($server in $ServerList)
{
  try
  {
	# Get printer information
	$Printers = Get-WMIObject Win32_Printer -computername $server
	foreach ($Printer in $Printers)
	{
	    $Sheet.Cells.Item($intRow, 1) = $Printer.SystemName
	    $Sheet.Cells.Item($intRow, 2) = $Printer.Name
	    $Ports = Get-WmiObject Win32_TcpIpPrinterPort -computername $ServerList
	        foreach ($Port in $Ports)
	        {
	            if ($Port.Name -eq $Printer.PortName)
	            {
	            $Sheet.Cells.Item($intRow, 3) = $Port.HostAddress
	            }
	        }
	    $Sheet.Cells.Item($intRow, 4) = $Printer.DriverName
	    $Sheet.Cells.Item($intRow, 5) = $Printer.Location
	    $Sheet.Cells.Item($intRow, 6) = $Printer.Comment
	    $Sheet.Cells.Item($intRow, 7) = $Printer.ShareName
	    $Sheet.Cells.Item($intRow, 8) = $Printer.Shared
	    $intRow = $intRow + 1
	}
  }
  catch
  {
  	#Log failure
  }
}

Open in new window

Unless you absolutely need Excel try using a .csv.  They do not fail if you encounter errors as easily as regular Excel files.  Also, if you are having trouble with specific machines, you can identify the trouble children by writing a single .csv for each machine.  When you are done, simple merge the .csv files into one.  That way, you have a clear approach for troubleshooting specific files and getting info that you need without losing it along the way.
Avatar of REIUSA

ASKER

What syntax would you use to make it a CSV instead of excel?
It uses $Excel and Excel.Application
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Avatar of REIUSA

ASKER

I'm still trying to find out why the script won't run on a lot of servers at one time. Should I open a new question?
The error code indicates port 135 is not open on the remote machine: http://msdn.microsoft.com/en-us/library/windows/desktop/aa389286(v=vs.85).aspx.  You need to verify they are open, and, either use a GPO to set them up to accept remote connections over 135 or this approach will not work.
Avatar of REIUSA

ASKER

What's weird is if I take a large chunk of the list of server it will stop and not populate the spread sheet but if I do it in smaller chunks I was able to run it on all the servers.

Any ideas what would cause a large list to not work but when ran 5-10 at a time it works? The machine I am using has 3 gb of RAM and page file is set right.
I would imagine you are hitting specific servers, erroring, and, invalidating the output in the process.  But, that's a guess.  You may also be hitting time out issues with some of the connections.
Avatar of REIUSA

ASKER

What about getting it to use a CSV file instead of Excel like mentioned above? How could I edit the code for that, it seems like what it is using now is specifically for a .XLS file.

Thanks,
That's more an issue of how the data is output the file, but, it seems like the main issue (the error) lies not in the output, but, rather, in the collection of data.
Avatar of REIUSA

ASKER

What's odd though is I can run it on 50 servers and the excel file pops up, runs for maybe 1-2 minutes and the sheet shows the headers, blank below that and then Done to indicate the scrip has finished.

But then if I run it 5-10 at a time on the exact same servers it will run for about 5-10 minutes for each server and populate the sheet.
Hmmm.....  It's possible you've run into a limitation on the number of threads Powershell can spawn when it calls up instances of Excel.  If I remember correctly, the default is 16, but, I am going off memory.  You may want to look at doing the 5-10 in separate jobs as that seems to work.
Avatar of REIUSA

ASKER

What if I exported it to a text file and then converted to a spread sheet after the fact, if that is even possible. Or is there a way to increase the number of threads?
ASKER CERTIFIED SOLUTION
Avatar of wls3
wls3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of REIUSA

ASKER

Thanks for the info. I'll open another question if I still have problems.