Solved

retrieve services from the remote computer

Posted on 2013-12-31
8
275 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
[X]
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
  • 4
  • 4
8 Comments
 
LVL 18

Assisted Solution

by:Jeremy Weisinger
Jeremy Weisinger earned 500 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 18

Assisted Solution

by:Jeremy Weisinger
Jeremy Weisinger earned 500 total points
ID: 39749082
Just tested it and it works for me. What errors are you getting?
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

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 18

Accepted Solution

by:
Jeremy Weisinger earned 500 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 18

Expert Comment

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

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
A procedure for exporting installed hotfix details of remote computers using powershell
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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