Problem Solver
Problem Solver

Reputation: 23

powershell get-process behavior different when in script

If I type this command, the results are correct:

Get-Process -ComputerName localhost -Id 1112,2436  
Get-Process -ComputerName localhost -Id 1112, 2436  

Notice the space in the list in the second command.

But if I try doing this in a script, it fails:

$str = '1112, 2436'  
Get-Process -ComputerName localhost -Id $str  

Get-Process : Cannot bind parameter 'Id'. Cannot convert value "1112, 2436" to type "System.Int32". Error: "Input string was not in a correct format."

This fails too:

$str = '1112,2436'  
Get-Process -ComputerName localhost -Id $str 

Get-Process : Cannot find a process with the process identifier 11122436.

Any idea how I can pass the two ids to the command in a Powershell script?

Upvotes: 2

Views: 187

Answers (1)

krontogiannis
krontogiannis

Reputation: 1939

1112, 2436 in -Id parameter is an array declaration, not a string. Remove the quotes:

$Ids = 1112, 2436 # spaces don't matter
Get-Process -ComputerName localhost -Id $Ids

Upvotes: 3

Related Questions