Powershell will not Check First item in list

Hello Im teaching myself Powershell and i cannot seem to run this code

Get-Process -computerName (import-csv .\computer_names.csv|
select -property hostname)

Instead of testing each Computername it tests only the first listed computer

In the book it says it should test each computer name. If i change the first computer name it only scans the first and works but never goes to the next ones :frowning:

Below is a pic of my CSV file
CSV


Below is my output

trickyspeed,
welcome to the forums.

Please do not post pictures of code or error messages or console output or sample data. Instead you should post the text and format it as code with the code posting icon </>. Thanks in advance.

Do you have computers in your network with the names “Win8”, “win10” and " * " ?? :wink: When you carefully read the error message what you should do always you will notice that it says it couldn’t connect to the remote computer. So it tries to check but because there are no computers with these names it throws an error.

You probably need to expand the hostname, it’s looking for an string array of computers:

Get-Process -ComputerName (Import-Csv -Path .\computer_names.csv | Select-Object -ExpandProperty HostName)
2 Likes

So this looks like it tries to work. We now get a delay of 5 seconds and get this error below.

<Get-Process : Couldn’t connect to remote machine.
At line:1 char:1

  • Get-Process -ComputerName (Import-Csv -Path .\computer_names.csv | Se …
  •   + CategoryInfo          : NotSpecified: (:) [Get-Process], InvalidOperationException
      + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetProcessCommand /> 
    
    

It only provides this error and still doesn’t check any other iterations

Olaf, Thank you for your patience, Some of these machines do not exist, but the book i’m reading seems to make it seem like it should check each machine name in the list whether or not a error is thrown

Heres a raw quote

Because the Hostname property contains text strings, -ExpandProperty is able to expand those values into plain String objects, which is what -ComputerName wants. This means the following works:

PS C:\> Get-Process -computerName (import-csv .\computers.csv |
select -expand hostname)

OK, if you excpected to get some errors everything is fine though. :wink: :+1:t4:

BTW: in the picture of code you posted initially you’re not using -ExpandProperty. That’s why you’ve got the unexpected errors.

My fault I think I just misunderstood how PS handles Errors with that cmdlet.

Basically the cmdlet will error out and not check all Instances of names in the string array.

I changed my CSV file to something along the lines of:

< hostname,OperatingSystem
localhost,windows
localhost,windows />

and Wallah! It even seems to grab all the services TWICE and combines it into onel

So if I ran the command with the new csv I get this

< andles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName


292      16     6068      22000              1408   0 backgroundTaskHost                                                                                                                                                                                     
292      16     6068      22000              1408   0 backgroundTaskHost                                                                                                                                                                                     
415      18     2420       5356               344   0 csrss                                                                                                                                                                                                  
415      18     2420       5356               344   0 csrss                                                                                                                                                                                                  
292      16     2220       5704               416   0 csrss                                                                                                                                                                                                  
292      16     2220       5704               416   0 csrss                                                                                                                                                                                                  
384      15     4100      15496              1956   0 ctfmon                                                                                                                                                                                                 
384      15     4100      15496              1956   0 ctfmon                                                                                                                                                                                                 
378      31    15380      21340              2388   0 dfsrs                                                                                                                                                                                                  
378      31    15380      21340              2388   0 dfsrs                                                                                                                                                                                                  
153       8     2012       6032              2664   0 dfssvc                                                                                                                                                                                                 
153       8     2012       6032              2664   0 dfssvc                                                                                                                                                                                                 
224      22     4620      12116              1992   0 dllhost                                                                                                                                                                                                
224      22     4620      12116              1992   0 dllhost                                                                                                                                                                                                

10311 7412 128548 126356 2408 0 dns

==============================================

But running <PS C:\Users\Administrator> Get-Process -ComputerName localhost
/>

returns

< 402 17 2420 5340 344 0 csrss
287 16 2220 5700 416 0 csrss
384 15 4100 15496 1956 0 ctfmon
381 31 15312 21332 2388 0 dfsrs
153 8 2012 6032 2664 0 dfssvc
213 16 3284 11296 1992 0 dllhost
10311 7412 128548 126356 2408 0 dns
632 39 43964 80488 872 0 dwm
1620 70 30412 22200 3200 0 explorer
49 6 1756 4756 2228 0 fontdrvhost
49 6 1424 3700 2236 0 fontdrvhost
0 0 56 8 0 0 Idle
129 12 1968 5600 2420 0 ismserv
1632 111 51632 52108 544 0 lsass
475 27 32820 42592 2324 0 Microsoft.ActiveDirectory.WebServices
221 13 3128 10212 3912 0 msdtc
529 68 173888 90144 2476 0 MsMpEng
205 40 3308 9736 2824 0 NisSrv
264 14 2800 20036 272 0 notepad
242 14 2664 9028 4612 0 notepad
813 53 126236 62132 1072 0 powershell_ise
/>

Is it just me or is this just odd error handling?

(sorry the code tags do not mix with copying table data :frowning:

You may try it again as they usually work just as expected. :wink: You can even go back and edit your existing posts and correct them.

For your local computer you can omit the parameter -ComputerName and just run the cmdlet as it is without any parameter: (If you want to post code click on the </> icon first and then post your code)

Get-Process

The output looks something like this:

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    180      10     2460       6540              9816   0 aesm_service
    381      23    12684      28808       5,81  16776   1 ApplicationFrameHost
    251      14    11632      17096   1.982,30  17532   0 audiodg
    341      17    35288      59116       5,39  13936   1 chrome
     76       5     2288       1148       0,00   1480   1 cmd
    588      46   361660     100780      38,58  12268   1 Code
    453      52    44628       1800       4,89  12612   1 ColorPickerUI
    139       7     1460       8156       0,02  10688   1 CompPkgSrv
    109       8     1452       6456       0,03  10168   1 conhost
    992      42     3972       5856               876   1 csrss
    498      18    30428      21492      51,92   8308   1 ctfmon
    335      12     3120       7372              5384   0 dasHost
   2396      21     6800      12508       5,83   5608   1 dllhost

If you want to have control about errors you should read the help about_Try_Catch_Finally

You can shorten Rob-Simmers code sample somewhat:
Get-Process -ComputerName (Import-Csv -Path .\computer_names.csv).HostName