header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Checking Service/Process existence & status
Last Post 29 Sep 2008 09:27 PM by aalborz. 27 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
18 Sep 2008 03:41 PM  

I'd like to check for existence and status of specific services/processes on all the servers on our LAN.

Ideally, I'd like to run the PS script from my machine to first scan AD (we have multiple domains) and parse out only servers. Then it would look for existence of a certain service(s) and if present, list its status and also whether a certain process is running/present. The output should be in a txt or csv format.

Otherwise the script can use an input file, which has the list of the servers, to get the same service and process information.

TIA

ALEX

 

 

aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
23 Sep 2008 02:20 PM  

I wonder why nobody has responded to my post. Is that because it cannot be done with PS?!

If that's the case, then that's fine, as long as I know that and look for alternative solutions.

Thanks

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
23 Sep 2008 02:51 PM  
Sorry this one got missed. Happens sometimes. I'd use a combination of the Quest AD cmdlets to find your servers and Get-WMIObject to find the services. Now I'm not the best with LDAP queries but here's an example using simple computername wildcards:


$ServiceName = "alerter"
get-qadcomputer srv* | foreach-object {
  get-wmiobject win32_service -comp $_.Name -filter "Name = $Servicename"
} | export-csv file.csv


You may have to play with the -Filter part--I seem to have awful luck with those and rarely get the WQL syntax right the first time. :)
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
23 Sep 2008 02:58 PM  

No problem, I understand. Thanks for responding to my post...

Pardon my ignorance, but where do I get Quest AD cmdlets? Do I need to install/register them with PS?

Also any chance I can accomplish the same with Processes?

Thanks again

aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
23 Sep 2008 03:31 PM  

Actually I found the Quest AD CmdLets app and installed it, but when I run the PS script, it complains about the Quest "qad.." command. Do I need register them with PS? If so, how do I do that?

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
23 Sep 2008 03:39 PM  
Yes. You can do this one of two ways.

1. Run Quests's "ActiveRoles Management Shell for Active Directory" shortcut in your start menu. This loads a "console file" which has instructions on which snapins to load.

2. Load the snapin yourself which is easy to do. Just type this:

Add-PSSnapin Quest.ActiveRoles.ADManagement


in any powershell window. If you want it to load every time you start posh then do this:

"`nAdd-PSSnapin Quest.ActiveRoles.ADManagement" | add-content $profile
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
23 Sep 2008 03:49 PM  

I was finally able to run the script without errors (in PS Script Editor), but the output is an empty 0kb file.

Why is that?!

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
23 Sep 2008 04:05 PM  
Well do it a line at a time and see where its failing to return input.

Does get-qadcomputer return computer objects?
Does get-qadcomputer | foreach-object { $_.name } return a list of names?

This is one great thing about powershell, ad-hoc work at the shell is the same as a script
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
23 Sep 2008 04:30 PM  

Yes on both counts... On a side note, I have to say I haven't had much luck with PS!

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
23 Sep 2008 05:29 PM  
If you need more guided help, it may be easier to do in realtime. Chat (IRC) is always an option. Here's a web gateway to the very helpful #powershell channel on freenode: http://powershelllive.com/irc

As to the issue, try a single get-wmiobject command:

get-wmiobject win32_service -comp $computer

where $computer is the name of a remote computer. If that works, try piping it to export-csv $filename so you get a feel for the results.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
23 Sep 2008 06:54 PM  

That worked also, including piping it out to a file. I think the problem is this part: "get-qadcomputer srv*", because when I pipe that to a file, it creates an empty file.

I played around with OSName and even Get-OSName, but no matter how I do it, I get syntax errors. :(

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
23 Sep 2008 06:57 PM  
Oh sorry, I didn't explain that fully.

Get-QAdComputer's name property occupies position #1 and can be implicitly supplied w/o the "-name" bit. This property is used to query your AD by the computer name. "srv*" in my example was a hypothetical example that would grab all computers that have names which begin with srv. Just run get-qadcomputer by itself and you'll see a big list.

Then if you know your LDAP, you can create a search filter or canonical name pointing straight to a particular OU. Read the help files that come with the Quest cmdlets for how to use those paramters.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
23 Sep 2008 09:50 PM  

Thanks for the suggestions. The guys at irc helped me out a lot and ALMOST got it working, but not quiet.

I guess PS is not capable of doing everything, or at least not the tasks I've come up with!

Appreciate your time!

 

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
23 Sep 2008 10:53 PM  
What? What! Well, bring us up to date, what's lacking now? I can't let a challenge like that lie, and neither can Shay, I imagine.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
24 Sep 2008 01:58 PM  

Well...the script runs and it generates a file, but it lists only 12 servers. I know we have a whole lot more servers than that! And also I get an "Access is Denied" error when running the script.

The guys at IRC were thinking this could be due to permission issues, but I'm a member of Domain Admins group and I have admin rights on all the servers.

aleksandarUser is Offline
New Member
New Member
Posts:11
Avatar

--
24 Sep 2008 02:45 PM  
Check to see if the Windows firewall on that machine is blocking you.
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
24 Sep 2008 02:56 PM  

If you're referring to the machine I'm running the script from, No, WF is not running. The targets are servers & this doesn't apply to them.

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
24 Sep 2008 03:13 PM  
I assume you have verified that the results from Get-QADComputer match what you expect? Obviously garbage-in-garbage-out, so if the servers aren't provided as input to Get-WmiObject, you won't see output on those.

Otherwise, sounds like you need to start debugging. You may want to write details to a log and check the security events on the remote servers to see what sort of failure audit events are there.

Note that this technique to query services on remote boxes is using WMI and you would run into the exact same issue when using Vbscript. This is not some failing of PowerShell.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
24 Sep 2008 03:22 PM  

Yes I have verified that Get-QADComputer gives me all the servers I want.

I'm not sure how do I go about debugging, since I don't know which servers the Get-WMIobject is failing on. Besides, we have a lot of servers and looking manually through each server's logs is just cumbersome and kinda defeats the whole purpose of this.

I realize this is not PS's shortcoming, but I wish there was a way PS could perhaps bypass those non-responding/problematic servers and finish up the rest.

 

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
24 Sep 2008 05:26 PM  
Well actually you only need to worry about one server most likely. Do something like this:

$server = get-qadcomputer #etc.
$server | foreach-object {
    $output = "" | select Name, WMISuccess
    $output.Name = $_
    get-wmiobject win32_service -comp $_ | out-null
    $output.WMISuccess = $?
    write-output $output
}


This _should_ spit out lots of red errors as well with some perhaps helpful text. Tell us what some of them say.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
24 Sep 2008 05:48 PM  

I'm not sure what you were looking for, but it gave me only 1 error: RPC server not available.

I wish I coulld attach screenshot of $output , but it's larger than 60kb attachment size here.

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
24 Sep 2008 06:40 PM  
The output was for you, not me. I already know what the problem is. :) Don't know the /cause/ of it though.

Try looking at this page, there's tons of WMI troubleshooting tips.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
24 Sep 2008 06:42 PM  
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
24 Sep 2008 08:14 PM  
Checking the server listed in $Output...Server is pingbale, both RPC & WMI services are running. The only odd thing is the RPC services is running under "NT Authority\NetworkService", which I'm not what or where it is.

Assuming that my user account permission is the issue, how can I run my script under a different account?
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
25 Sep 2008 04:11 PM  
NetworkService is a built-in account. Most services run under this one or the SYSTEM account. System doesn't have access to the network stack, the other one does. In other words, you should be fine.

You'll need to perform a test against one server (get-wmiobject win32_service -comp server1), then go to that server and look at the failure audits in the security log. Then hit Google or let us know if you can't figure it out. To be sure, this is a Windows problem, not a PowerShell problem. Not that that helps, I know. :)
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
29 Sep 2008 08:44 PM  

But when I execute that command on that server...I do get the output (Services information)!

I'm totally confused here...I don't know what's going on!!!

halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
29 Sep 2008 08:58 PM  
That's good to know. So did you try doing it remotely then checking the security log?
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
aalborzUser is Offline
New Member
New Member
Posts:56
Avatar

--
29 Sep 2008 09:27 PM  

No I didn't, because it worked. I would have...if it had failed.

I still don't get it...this was the first server the original script had stopped on, supposedly because of permission issues.

But how come when I ran it by itself, it worked?!!

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer