Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 645
  • Last Modified:

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
0
IT_Crowd
Asked:
IT_Crowd
  • 6
  • 6
1 Solution
 
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:
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
 
NavdeepCommented:
btw it's a powershell script.

Change 8 with a -
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now