Solved

How to create al Logfile

Posted on 2009-07-08
6
690 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
Comment Utility

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
Comment Utility
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
Comment Utility

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ptea
Comment Utility
...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
Comment Utility

> ...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
Comment Utility
Hey Chris - I have to open my eyes ;-) Thank you works perfect!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

12 Experts available now in Live!

Get 1:1 Help Now