?
Solved

how can I test get-wmiobject

Posted on 2013-01-04
7
Medium Priority
?
1,179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 38743832
The namespace should be....

-namespace "root\cluster"
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 38743845
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
ID: 38743868
This script can be execute on windows server 2008 R2 or 2003.
Same error with -namespace "root\cluster" .

Thanks

Regards
bibi
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 37

Expert Comment

by:Neil Russell
ID: 38744058
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
ID: 38745220
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 2000 total points
ID: 38745598
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
ID: 38767192
Thanks regards bibi
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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