Solved

how can I test get-wmiobject

Posted on 2013-01-04
7
1,138 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating 2 network Adapters via PowerCli 7 35
output in HTML format powershell 6 48
Read WSUS log on Window 10 5 83
Poweshell to Ping and return Last 3 users 3 49
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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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