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

x
?
Solved

retrieve services from the remote computer

Posted on 2013-12-31
8
Medium Priority
?
280 Views
Last Modified: 2014-01-08
I have the script below that retrieves services from a local computer. I need an Expert to tweak this script so that I can retrieve services from remote computer.

I am not sure if using Psexec will be able to achieve the same goal

Thank you




$style = @"
  <style>
    TABLE {border-collapse: collapse}
    TD {border-width: 1px;padding: 5px;border-style: dotted;border-color: black}
  </style>
"@

Get-WmiObject win32_service |
  % {
    Add-Member -PassThru -InputObject $_ NoteProperty Delayed $(
      if ((Get-ItemProperty "HKLM:System\CurrentControlSet\Services\$($_.Name)").DelayedAutoStart -eq 1) { "x" }
    )
  } |
  select-object DisplayName, State, Startmode, Delayed |
  sort startmode, DisplayName |
  ConvertTo-HTML  -PreContent "<h1>$env:ComputerName</h1>$style" |
  % { $_ -Replace '<th>', '<td><strong>' -Replace '</th>', '</strong></td>'  } |
  Out-File C:\Test.html
0
Comment
Question by:jskfan
  • 4
  • 4
8 Comments
 
LVL 22

Assisted Solution

by:Jeremy Weisinger
Jeremy Weisinger earned 2000 total points
ID: 39748664
Add the -ComputerName parameter to the Get-WMIObject.

$style = @"
  <style>
    TABLE {border-collapse: collapse}
    TD {border-width: 1px;padding: 5px;border-style: dotted;border-color: black}
  </style>
"@

Get-WmiObject win32_service -ComputerName nameofcomputer|
  % {
    Add-Member -PassThru -InputObject $_ NoteProperty Delayed $(
      if ((Get-ItemProperty "HKLM:System\CurrentControlSet\Services\$($_.Name)").DelayedAutoStart -eq 1) { "x" }
    )
  } | 
  select-object DisplayName, State, Startmode, Delayed |
  sort startmode, DisplayName |
  ConvertTo-HTML  -PreContent "<h1>$env:ComputerName</h1>$style" |
  % { $_ -Replace '<th>', '<td><strong>' -Replace '</th>', '</strong></td>'  } |
  Out-File C:\Test.html 

Open in new window

0
 

Author Comment

by:jskfan
ID: 39748908
That 's what I thought too...the scrip will throw errors....
have you tried it ?
0
 
LVL 22

Assisted Solution

by:Jeremy Weisinger
Jeremy Weisinger earned 2000 total points
ID: 39749082
Just tested it and it works for me. What errors are you getting?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jskfan
ID: 39749101
one of the errors:

Get-ItemProperty : Cannot find path 'HKLM:\System\CurrentControlSet\Services\FCRegSvc' because it does not exist.
0
 

Author Comment

by:jskfan
ID: 39749102
it works when you run it against local computer only
0
 
LVL 22

Accepted Solution

by:
Jeremy Weisinger earned 2000 total points
ID: 39749133
You're right. I didn't see the Get-ItemProperty command in there.

The easiest way would be to go back to the original script and use the Invoke-Command. Save the script and then run:
Invoke-Command -ComputerName server1 -FilePath C:\servicesscript.ps1

Open in new window

If you have a list of computers you want to run it against then you can easily do that by running:
Foreach($srv in (get-content C:\computers.txt)){
    Invoke-Command -ComputerName $srv -FilePath C:\servicesscript.ps1
}

Open in new window

Also, I would change the output path to a centralized location and append the computer name to the file name.

More info on the Invoke-Command and remoting:
http://technet.microsoft.com/library/hh849719.aspx
http://technet.microsoft.com/en-US/library/hh847900.aspx
0
 

Author Closing Comment

by:jskfan
ID: 39766433
Thanks
0
 
LVL 22

Expert Comment

by:Jeremy Weisinger
ID: 39767014
Glad to help.
0

Featured Post

Problems using Powershell and Active Directory?

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

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
The viewer will learn how to dynamically set the form action using jQuery.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Suggested Courses

926 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