WMI Query to all Domain PCs

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
LVL 13
IT_CrowdAsked:
Who is Participating?
 
NavdeepConnect With a Mentor Commented:
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
 
Cliff GaliherCommented:
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
 
NavdeepCommented:
btw it's a powershell script.

Change 8 with a -
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
IT_CrowdAuthor Commented:
Thanks Navdeep - I will check this out today and let you know!   :)
0
 
IT_CrowdAuthor Commented:
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
 
NavdeepCommented:
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
 
IT_CrowdAuthor Commented:
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
 
NavdeepCommented:
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
 
IT_CrowdAuthor Commented:
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
 
NavdeepCommented:
Good, this will pull computer scross the domain
0
 
IT_CrowdAuthor Commented:
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
 
NavdeepCommented:
Ok, I will try to contribute so basically you want to initiate computer shutdown wait and proceed to next .. is it?
0
 
IT_CrowdAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.