Solved

how can I test get-wmiobject

Posted on 2013-01-04
7
1,103 Views
Last Modified: 2013-01-11
Hello,

How can I modify this code for testing a wmi query without having the following error :
Get-WmiObject : Invalid namespace
At line:3 char:23
+ $cluster=get-wmiobject <<<<  -class "MSCluster_Cluster" -namespace "root\mscluster" -computername "$mach_name" | select -ExpandProperty Name
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

The query :
try
{
$cluster=get-wmiobject -class "MSCluster_Cluster" -namespace "root\mscluster" -computername "$mach_name" | select -ExpandProperty Name    
}
catch [Exception]
{ 
    echo "$cluster" | out-file $LOG_FILE -ErrorVariable ERR1 -encoding "default" -append -ErrorAction silentlycontinue 
}

Open in new window

Thanks
bibi
0
Comment
Question by:bibi92
  • 3
  • 3
7 Comments
 
LVL 37

Expert Comment

by:Neil Russell
Comment Utility
The namespace should be....

-namespace "root\cluster"
0
 
LVL 37

Expert Comment

by:Neil Russell
Comment Utility
P.S.

If this is in a windows server 2008 R2 environment, use directly the Powershell cluster cmdletts

http://blogs.msdn.com/b/clustering/archive/2009/05/21/9633316.aspx
0
 

Author Comment

by:bibi92
Comment Utility
This script can be execute on windows server 2008 R2 or 2003.
Same error with -namespace "root\cluster" .

Thanks

Regards
bibi
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 37

Expert Comment

by:Neil Russell
Comment Utility
On a server that is part of a cluster can you run the following in powershell?

get-wmiobject -class "MSCluster_Cluster" -namespace "root\cluster"
0
 

Author Comment

by:bibi92
Comment Utility
Already done, I want to test if the server is a clustered or not. If I execute the script and the server is not a cluster, the error occurs :
Get-WmiObject : Invalid namespace
At line:3 char:23
+ $cluster=get-wmiobject <<<<  -class "MSCluster_Cluster" -namespace "root\mscluster" -computername "$mach_name" | select -ExpandProperty Name

Thanks
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
The exception thrown is not caught by the Try/Catch, since it is not a "terminating error". You can tell tell PowerShell to treat it as a terminating error by adding -ErrorAction Stop
Try this..
try
{
$cluster=get-wmiobject -class "MSCluster_Cluster" -namespace "root\mscluster" -computername "$mach_name" -ErrorAction Stop | select -ExpandProperty Name    
}
catch [Exception]
{ 
    echo "$cluster" | out-file $LOG_FILE -ErrorVariable ERR1 -encoding "default" -append -ErrorAction silentlycontinue 
}

Open in new window

Also you may have to write a custom error message in Catch section as the Var $cluster will be null if there is an exception..
0
 

Author Closing Comment

by:bibi92
Comment Utility
Thanks regards bibi
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

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…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This video discusses moving either the default database or any database to a new volume.
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…

763 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

15 Experts available now in Live!

Get 1:1 Help Now