lavhmrps
lavhmrps

Reputation: 21

How do I use arguments in powershell-scripts

How can i make this work:

I'm trying to use it it a script so that it lists the processes i send with the script as arguments:

for ($i=0; $i -lt $args.length; $i++)
  echo (Get-Process | Where-Object {$_.name -eq $($args[$i])})
}

It will however not list anything when i run it:

./list_process notepad svchost

It works fine when i do it manually:

Get-Process | Where-Object {$_.name -eq "notepad")}

Upvotes: 0

Views: 123

Answers (3)

Nitesh
Nitesh

Reputation: 874

Perfect @Frode.

You can also build an advanced function to do a lot more:

Function Get-SelectiveProcesses{
[CmdletBinding()]
Param
(
 [Parameter(Mandatory = $true)]
 [string[]] $process
 )

 Get-Process $process
 #Do other Things

} 

And then call the function in your script like

PS C:\> Get-SelectiveProcesses notepad,svchost

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     85       6     3672       7580    75     1.23   5260 notepad
    711      51    19148       7588   123             568 svchost
    404      10     3492       2760    56             844 svchost
    373      11     3540       2896    41            1220 svchost
    514      52    23436       7016   100            1324 svchost
    631      31    85248      74368   200            1384 svchost
   1458     109    31540      19140   180            1428 svchost
    322      31     8388       3976    64            1628 svchost
    372      30    32908      12736   171            1688 svchost
    336      54    13672       4768    77            2096 svchost
    126      47     6916       2344    61            2272 svchost
    131       8     3228        532    55            3972 svchost
    160      23     7456       1880    63            4032 svchost

Type help about_functions_advanced to know more.

Upvotes: 0

Ocaso Protal
Ocaso Protal

Reputation: 20267

The problem is that every scriptblock gets new $args, so use a temp variable:

for ($i=0; $i -lt $args.length; $i++){
    $outerArg = $args[$i]
    Get-Process | Where-Object {$_.name -eq $outerArg}
}

Upvotes: 0

Frode F.
Frode F.

Reputation: 54981

In this case, you could simplify it ALOT. Get-Process supports an array of processnames, so you could simply pass it the whole $args-array.

PS > Get-Help Get-Process -Parameter name

-Name <String[]>
    Specifies one or more processes by process name. 
    You can type multiple process names (separated by commas) and use wildcard characters. 
    The parameter name ("Name") is optional.

Also, the echo is unnecessary, as Write-Output(which echo is an alias for) is the default output for PowerShell.

list_process.ps1

Get-Process $args

Test:

PS > ./list_process notepad svchost

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s)    Id ProcessName
------- ------ ----- ----- ----- ------    -- -----------
     77      7  1228  5856    88   0,09  6728 notepad    
    862     31 32164 33268   101          316 svchost    
    904     31 40508 42412   121          504 svchost    
   4463     62 92328 97088   417          512 svchost    
    519     16  9324 12768    49          808 svchost    
    827     46 35964 42348   156          832 svchost    
    601     19 11740 14540    57          840 svchost

Upvotes: 2

Related Questions