Solved

How to create al Logfile

Posted on 2009-07-08
6
695 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
  • 3
  • 3
6 Comments
 
LVL 70

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 70

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 70

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now