Solved

How to create al Logfile

Posted on 2009-07-08
6
692 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
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.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

21 Experts available now in Live!

Get 1:1 Help Now