Home > OS >  Minimal Powershell script with Process block gives system process list (MacOS, Pwsh 7.1.3)
Minimal Powershell script with Process block gives system process list (MacOS, Pwsh 7.1.3)

Time:02-03

I was writing a Powershell script using a pipeline with a Process block and it started doing something unexpected: listing all the running processes and then dumping the script contents. I kept minimizing the script to try to figure out what was going on and ended up with this:

[CmdletBinding()]
Param()
$varname = "huh"

Process
{
    # nothing here
}

So it looks like this:

PS /Volumes/folder> cat ./test.ps1
[CmdletBinding()]
Param()
$varname = "huh"

Process
{
    # nothing here
}
PS /Volumes/folder> pwsh ./test.ps1

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
      0     0.00       0.00       0.00       0 639 
      0     0.00       0.00       0.00       1   1 
      0     0.00       0.00       0.00      60  60 
      0     0.00       0.00       0.00      61  61 
      0     0.00       0.00       0.00      65  65 
      0     0.00       0.00       0.00      67  67 
      0     0.00       0.00       0.00      68  68 
      0     0.00       0.00       0.00      69  69 
      0     0.00       0.00       0.00      71  71 
      0     0.00       0.00       0.00      73  73 
      0     0.00       0.00       0.00      75  75 
      0     0.00      25.60      75.82   68475   1 Activity Monito
      0     0.00      11.74      97.63    1053   1 Adobe Crash Han
      0     0.00      11.76      97.62    1084   1 Adobe Crash Han
      0     0.00      11.69      97.64    1392   1 Adobe Crash Han
      0     0.00     112.50      83.59     973   1 Adobe Desktop S
      0     0.00      11.94      97.31     986   1 AdobeCRDaemon
      0     0.00      16.95     105.99     966   1 AdobeIPCBroker
      0     0.00      61.52     168.92     721   1 Adobe_CCXProces
      0     0.00      18.57       3.01     454   1 adprivacyd
      0     0.00      16.46      23.16     700   1 AGMService
      0     0.00      13.65       4.43     701   1 AirPlayUIAgent
   --snip--
      0     0.00       9.11      12.72   89003 …03 VTDecoderXPCSer
      0     0.00      13.32       4.69     418   1 WiFiAgent
      0     0.00      12.21       1.58     543 543 WiFiProxy

    # nothing here

I haven't done much in Powershell for a long time so if this is something stupid simple I'm going to laugh but I couldn't find anything searching the net.

Can someone tell me what's happening?

CodePudding user response:

In order to use a process block (possibly alongside a begin, end, and, in v7.3 , the clean block), there must not be any code OUTSIDE these blocks - see the conceptual about_Functions help topic.

Therefore, remove $varname = "huh" from the top-level scope of your function body (possibly move it into one of the aforementioned blocks).


As for what you tried:

  • By having $varname = "huh" in the top-level scope of your function body, you've effectively made the function in one whose code runs in an implicit end block only.

  • process - because it is on its own line - was then interpreted as a command, which - due to the best-avoided default-verb logic - was interpreted as an argument-less call to the Get-Process cmdlet.

    • The output therefore included the list of all processes on your system.
  • The { ... } on the subsequent lines was then interpreted as a script block literal. Since that script block wasn't invoked, it was implicitly output, which results in its stringification, which is its _verbatim content, excluding { and }, resulting in output of the following string:


# nothing here

  • Related