Solved

How to create al Logfile

Posted on 2009-07-08
6
698 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
A brief introduction to what I consider to be the best editor for PowerShell.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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