?
Solved

WMI Query to all Domain PCs

Posted on 2013-01-22
13
Medium Priority
?
639 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
[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
  • 6
  • 6
13 Comments
 
LVL 59

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 2000 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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

718 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