2

I have written a script that generates some information but I need to run it on over 160 computers in an active directory. I don't want to run this script on each machine individually, is there any way of executing this script all all the machines from one centralized location?

invoke-command -ComputerName test-pc -ScriptBlock {gwmi win32_service | Select-object Name, PathName | where-object {$_.PathName -notlike '"*' -and $_.PathName -like "*\* *\*"}}

I don't want to be logging on to each machine individually. Is there quicker way? There must be.

Any help will be much appreciated.

Sohail.

Updated version:

invoke-command -ComputerName @(Get-ADComputer -Filter {Name -like "GBST*"} | Select-Object Name)  -ScriptBlock {gwmi win32_service | Select-object Name, PathName | where-object {$_.PathName -notlike '"*' -and $_.PathName -like "*\* *\*"}}

Error message:

invoke-command : One or more computer names are not valid. If you are trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects 
instead of strings.
At line:1 char:2
+     invoke-command -ComputerName @(Get-ADComputer -filter {Name -like "GBSU1*"} | S ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException
    + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand
9
  • What if you apply your command to a list of computers ? Would that fit ? Do you know how to list computers in AD ? (you can reply by editing your question and adding more information) Commented Nov 18, 2015 at 15:24
  • "apply your command to a list of computers"? Please can you elaborate? Example maybe? Commented Nov 18, 2015 at 15:24
  • 1
    Or you can take Mathias's perfect one-liner but I advise you to try to understand the code BEFORE running it. Imagine this is malicious and deletes every found object ? ^_^ Commented Nov 18, 2015 at 15:27
  • 1
    Should it not be @(Get-ADComputer -Filter {Name -like "GBST*"} | Select-Object -Expand Name) Note the expand so you get a string array. Commented Nov 18, 2015 at 15:44
  • 1
    @MuhammadSohailArif Notice the -Expand parameter with select Commented Nov 18, 2015 at 15:59

2 Answers 2

3

Just to make it shorter :

(Get-ADComputer -Filter 'Name -like "GBSU*"').Name | % {
    Get-WMIObject Win32_Service |
    Select-Object Name, PathName |
    Where-Object { $_.PathName -notlike '"*' -and $_.PathName -like "** **" }
}

As pointed out by @bluuf :

This is actually not a 'good' solution since Get-WmiObject supports the ComputerName parameter : Invoke-Command shouldn't be used at all in this case.

Sign up to request clarification or add additional context in comments.

1 Comment

This command works in respects to displaying all the services, but it does not display the results I was expecting. The 'solution' provided by @McKenning was useful in a way that it displayed service path names that contained spaces and that were not enclosed between quotations.
2

There is a syntax error in your revised example where you kept the "*" in the Filter. You can't filter for all then narrow your results. Asterisk is implied when filtering based on properties.

I also added the ".Name" property to the computer search to only pass that to the invoke-command section. I tried the following code and it seemed to work.

invoke-command -ComputerName @((Get-ADComputer -Filter 'Name -like "GBST*"').Name) -ScriptBlock {gwmi win32_service | Select-object Name, PathName | where-object {$_.PathName -notlike 'C:\Windows\system32\*' -and $_.PathName -like 'C:\Program Files\*'}}

3 Comments

I placed your command in PS and it still gave me a few errors, but this is the script I finished with and it worked. So thumbs up for your answer: invoke-command -ComputerName @((Get-ADComputer -Filter 'Name -like "GBSU*"').Name) -ScriptBlock {gwmi win32_service | Select-object Name, PathName | where-object {$_.PathName -notlike '"*' -and $_.PathName -like "** **"}}
This is actually not a 'good' solution since Get-WmiObject supports the -computername parameter : invoke-command shouldn't be used at all in this case.
@bluuf is correct. It will probably be faster than invoke-command as well. The following should work with that model: (Get-ADComputer -Filter 'Name -like "GBSU*"').Name | % {gwmi win32_service -ComputerName $_ | Select-object Name, PathName | where-object {$_.PathName -notlike 'C:\Windows\system32\*' -and $_.PathName -like 'C:\Program Files\*'}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.