Home > other >  Running script only prints script source code!@#$%
Running script only prints script source code!@#$%

Time:02-24

This is weird to me. Running a script will only output its source code. Here is an MRE. Naturally, the problem is on a much larger script as well. What kind of crazy am I going to?

PS C:\src\t> Get-Content -Path .\junk.ps1
[CmdletBinding()]
param (
    [Parameter()]
    [string]$S
)
{
    Write-Host $S
}
PS C:\src\t> .\junk.ps1 -S 'now'

    Write-Host $S

PS C:\src\t> $PSVersionTable.PSVersion.ToString()
7.2.1

CodePudding user response:

The {} braces in your script defines a script block literal - an anonymous function that can be invoked later.

Remove the curly braces and the Write-Host statement will work:

[CmdletBinding()]
param (
    [Parameter()]
    [string]$S
)

Write-Host $S
  • Related