1

So this was my script:

$ans=Read-Host "What process would you like to query?"
Get-WmiObject win32-process -Filter "name='$ans'" | Format-Table HandleCount,VirtualSize,UserModeTime,KernelModeTime,ProcessID,Name

Now I need to create a script which requires the argument be passed when the script is executed. I'm a little confused on how to do this successfully. This is what I'm trying to work with:

#!/bin/bash
echo $1

Get-WmiObject win32_process -Filter "name='$1'" | Format-Table HandleCount,VirtualSize,UserModeTime,ProcessID,Name

Charlie
  • 31
  • 2

1 Answers1

3

To make a parameter mandatory (required) in PowerShell, you must use an advanced script or function; to create a script file, save your code in a .ps1 file[1].

param(
  [Parameter(Mandatory)]
  [string] $Name
)

Get-CimInstance win32_process -Filter "name='$Name'"

Note:

  • The code uses Get-CimInstance instead of Get-WmiObject, because the CIM cmdlets superseded the WMI cmdlets in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell [Core] (version 6 and above), where all future effort will go, doesn't even have them anymore. For more information, see this answer.

  • The Format-Table call was intentionally omitted, because Format-* cmdlets should only ever be used to format data for display, never for subsequent programmatic processing, i.e. never for outputting data - see this answer.

    • Outputting just data means that PowerShell controls in what format your data is displayed; for information on how to control this format, see this answer.

[1] This is enough to make a plain-text file executable from PowerShell, without needing to include the .ps1 extension in the invocation. On Unix-like platforms, you can create an executable shell script without a filename extension via a shebang line such as #!/usr/bin/env pwsh and chmod a+x some that can be called from outside PowerShell as well.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Aww man, although I know the consequences of querying using *wmi* cmdlets, I still like to use them. Wasn't aware they went away with them:( they just have so much methods that are really useful! Good to know tho:) – Abraham Zinala Jul 04 '21 at 23:58
  • @AbrahamZinala, you can still call methods on the objects that `Get-CimInstance` returns: use [`Invoke-CimMethod`](https://learn.microsoft.com/powershell/module/cimcmdlets/invoke-cimmethod). Pragmatically speaking, the `*-Wmi*` cmdlets won't go away, and WMI will continue to _underlie_ CIM on Windows, but it's worth migrating to the `*-Cim*` cmdlets for a consistent experience across local and remote calls, the latter taking advantage of the modern, firewall-friendly CIM remoting transport (using the same infrastructure as PowerShell's own remoting) – mklement0 Jul 05 '21 at 02:49
  • 1
    Guess I've never tried to just run those methods that don't show in `Get-Ciminstance`, but do in gwmi. Will try it using `Invoke-CimMethod`. thx and happy 4th! – Abraham Zinala Jul 05 '21 at 03:14