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

REIUSAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

0
RobSampsonCommented:
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.
0
REIUSAAuthor Commented:
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
 
0
Introducing the "443 Security Simplified" Podcast

This new podcast puts you inside the minds of leading white-hat hackers and security researchers. Hosts Marc Laliberte and Corey Nachreiner turn complex security concepts into easily understood and actionable insights on the latest cyber security headlines and trends.

Brent ChallisPrincipal: ITCommented:
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

0
wls3Commented:
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.
0
REIUSAAuthor Commented:
What syntax would you use to make it a CSV instead of excel?
It uses $Excel and Excel.Application
0
DhaestCommented:
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.
0
REIUSAAuthor Commented:
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?
0
wls3Commented:
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.
0
REIUSAAuthor Commented:
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.
0
wls3Commented:
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.
0
REIUSAAuthor Commented:
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,
0
wls3Commented:
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.
0
REIUSAAuthor Commented:
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.
0
wls3Commented:
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.
0
REIUSAAuthor Commented:
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?
0
wls3Commented:
Yes.  The Csv cmdlets can easily do this as an intermediary transformation tool:

PS > get-help *csv*

Name                              Category  Synopsis
----                              --------  --------
epcsv                             Alias     Export-Csv
ipcsv                             Alias     Import-Csv
Export-CSV                        Cmdlet    Converts Microsoft .NET Framework objects into a series of comma-separat...
Import-CSV                        Cmdlet    Converts object properties in a comma-separated value (CSV) file into CS...
ConvertTo-CSV                     Cmdlet    Converts Microsoft .NET Framework objects into a series of comma-separat...
ConvertFrom-CSV                   Cmdlet    Converts object properties in comma-separated value (CSV) format into CS...

Open in new window


Also, the number of threads can be increased, but, I will have to research that one.  Monday's memory drain has already taken its toll.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
REIUSAAuthor Commented:
Thanks for the info. I'll open another question if I still have problems.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.