Solved

How to create al Logfile

Posted on 2009-07-08
6
705 Views
Last Modified: 2012-05-07
Hi there,

I wrote a script to detemire the OS and Servername for specefic servers stored in a textfile:

function get-os ($file) {
      $names = get-content $file
      foreach ($name in $names) {
            $name
            gwmi win32_OperatingSystem -prop Caption,CSName -comp $name
}
}

So it's possible that the script can't query a server, e.g. the server is down or I have no access to it. How can I log which queries failed and write it into a textfile (servername and cause should be in teh logfile).

Any ideas?! - Thanks
0
Comment
Question by:ptea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24811837

Hey,

Get-WMIObject is able to handle errors via the common parameters mentioned in Get-Help.

For example...

Chris
Get-Content $File| %{
  gwmi win32_OperatingSystem -prop Caption,CSName -comp $_ -ErrorAction SilentlyContinue -ErrorVariable WMIErr
  If ($WMIErr) { "WMI Error: $WMIErr" >> "ErrorLog.txt" }
}

Open in new window

0
 

Author Comment

by:ptea
ID: 24838583
Hi Chris,

thanks for your good input. That's what I need, I tried several things but I'm not able to get the hostname written in the errorlog too. Your piece of code provided me with error logging what WMI says - that's good - but only helpful when I know on which machine the script failed.

I tried several things to get the hostname but I wasn't sucessful - what's the trick?

Thanks & regards
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24838858

Tried this?

Chris
Get-Content $File| %{
  gwmi win32_OperatingSystem -prop Caption,CSName -comp $_ -ErrorAction SilentlyContinue -ErrorVariable WMIErr
  If ($WMIErr) { "WMI Error: $_ $WMIErr" >> "ErrorLog.txt" }
}

Open in new window

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:ptea
ID: 24860596
...it's the same piece of code as you provided me before - and yes I tried this, the errorlog file looks like this:

WMI Error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
WMI Error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
WMI Error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
WMI Error: System.Management.Automation.CmdletInvocationException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) ---> System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Management.ManagementScope.InitializeGuts(Object o)
   at System.Management.ManagementScope.Initialize()
   at System.Management.ManagementObjectSearcher.Initialize()
   at System.Management.ManagementObjectSearcher.Get()
   at Microsoft.PowerShell.Commands.GetWmiObjectCommand.BeginProcessing()
   at System.Management.Automation.Cmdlet.DoBeginProcessing()
   at System.Management.Automation.CommandProcessorBase.DoBegin()
   --- End of inner exception stack trace ---
   at

As you see the hostname is not observed, so I know whats going wrong but not where.
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 250 total points
ID: 24860740

> ...it's the same piece of code as you provided me before

Almost, but not quite, I added "$_" to the output since that contains the server name. It should have given you:

WMI Error: SomeServer The RPC server is unavailable ...

Did you try it with that?

Chris
0
 

Author Closing Comment

by:ptea
ID: 31601449
Hey Chris - I have to open my eyes ;-) Thank you works perfect!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question