Solved

WMI Query to all Domain PCs

Posted on 2013-01-22
13
619 Views
Last Modified: 2013-01-25
Experts

I would like a WMI query to poll all the computers on our domain and gather the computer description from each PC. If the description starts with a hyphen, I want the PC to shutdown.

If it is helpful, our computers are stored in separate OUs divided by different offices. For instance, and they are not stored in the default Computers OU.

Corp|RemoteOffice1|Windows7
Corp|RemoteOffice2|Windows7
Corp|RemoteOffice3|WindowsXP

etc.

Thank you!
IT_Crowd
0
Comment
Question by:IT_Crowd
  • 6
  • 6
13 Comments
 
LVL 56

Expert Comment

by:Cliff Galiher
ID: 38808696
you won't be able to do so with a single command. But a script should do this. I have no interest in writing an entire script for you (and honestly if is unfair for you to ask without a paying...developers get paid money to do this) ...but if you have a script you are trying to get work, Il happily take a look at it.
0
 
LVL 12

Accepted Solution

by:
Navdeep earned 500 total points
ID: 38808769
Hi IT_Crowd,

Kindly Test on bunch of test computers first
Here you go, save the code in a text file and save it .ps1. Update the searchbase to reflect correct location

Get-ADComputer -SearchBase "OU=Windows7,OU=RemoteOffice1,OU=Corp,DC=exchangeadtech,DC=com" |`
%{ 
    $dsc = (Get-WmiObject -Class Win32_OperatingSystem -computerName $_.Name).description;
    $chkName = $dsc.StartsWith("8");
    if($chkName)
     {
      write-host Initiating Shutdown of Remote Host $_.Name -ForegroundColor DarkYellow
      Stop-Computer -ComputerName $_.Name -Force
     }


 }

Open in new window


Launch powershell, browse to directory where script is located and
>./scriptname.ps1

Hope it helps

Regards,
Navdeep [ExchangeADTech]
0
 
LVL 12

Expert Comment

by:Navdeep
ID: 38808773
btw it's a powershell script.

Change 8 with a -
0
 
LVL 13

Author Comment

by:IT_Crowd
ID: 38814277
Thanks Navdeep - I will check this out today and let you know!   :)
0
 
LVL 13

Author Comment

by:IT_Crowd
ID: 38814380
Well I changed the 8 to a hyphen, and put in our real AD OU values, but the script doesnt look like its doing anything. I needed to change my execution policy for powershell to get the script to run. It seems to execute fine and doesnt throw any error messages, but the remote computer I have in my test OU never shuts down.

Here are some of things I have double checked
-Correct spelling of all OUs
-Disabled Firewall on remote PC
-I can ping/remote into test PC from my technician PC
-Opened a normal and Administrator PS window to execute the script
-Made sure the test PC is actually in the correct OU.   :)

Anything else come to mind?
I noticed at the top of our AD tree it does say CORP.com - do I need to change the PS script to say DC=CORP.com instead of DC=CORP?
0
 
LVL 12

Expert Comment

by:Navdeep
ID: 38814394
Hi,

No worries, You need to add the following line at the top of the script

import-module ActiveDirectory

This module will be available on windows server 2008 on a domain controller, you can get it on client/member server as well however you need to install ADDS feature and install active directory module.
0
 
LVL 13

Author Comment

by:IT_Crowd
ID: 38814467
Cool - we are definitely getting further. Now I am getting a box/prompt that pops up and asks for a filter. If I type in "*" the script executes and my machine shuts down.

Is there a way to pass that * along right away, or is there a way to bypass that filter question all together?

PS C:\users\IT\desktop> .\ShutdownIfHyphen.ps1

cmdlet Get-ADComputer at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Filter: !?
A filter, such as 'samAccountName -like "*DC*"', which is used to search the directory for matching computer accounts.
Filter:
0
 
LVL 12

Expert Comment

by:Navdeep
ID: 38815315
You can see the examples here
get-help get-adcomputer -examples

It will show you the possible examples. What i suggested earlier was to get all computers from specific ou, as per your requirement. You can run the command individually to check the results

Get-ADComputer -SearchBase "OU=Windows7,OU=RemoteOffice1,OU=Corp,DC=exchangeadtech,DC=com"

change OU's distinguishedName.
0
 
LVL 13

Author Comment

by:IT_Crowd
ID: 38815739
Looks good - thanks. Here is what i ended up with.

import-module ActiveDirectory
Get-ADComputer -Filter * |`
%{ 
    $dsc = (Get-WmiObject -Class Win32_OperatingSystem -computerName $_.Name).description;
    $chkName = $dsc.StartsWith("-");
    if($chkName)
     {
      write-host Initiating Shutdown of Remote Host $_.Name -ForegroundColor DarkYellow
      Stop-Computer -ComputerName $_.Name -Force
     }
 }

Open in new window

0
 
LVL 12

Expert Comment

by:Navdeep
ID: 38816599
Good, this will pull computer scross the domain
0
 
LVL 13

Author Comment

by:IT_Crowd
ID: 38816604
I'm trying to make it multithreaded now if you are interested:
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28007711.html#a38816597

:)
0
 
LVL 12

Expert Comment

by:Navdeep
ID: 38817748
Ok, I will try to contribute so basically you want to initiate computer shutdown wait and proceed to next .. is it?
0
 
LVL 13

Author Comment

by:IT_Crowd
ID: 38818747
Well the current script does one at a time and takes forever (especially if a computer is turned off). I was hoping to multithread the script so it could do a couple computers at once. Does that make sense?
0

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

11 Experts available now in Live!

Get 1:1 Help Now