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
Solved

WMI Query to all Domain PCs

Posted on 2013-01-22
13
626 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 57

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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

790 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