Run PowerShell custom function in new window

后端 未结 1 1047
天涯浪人
天涯浪人 2020-12-11 12:09
Function Check-PC
{
$PC = Read-Host \"PC Name\"
If($PC -eq \"exit\"){EXIT}
Else{
Write-Host \"Pinging $PC to confirm status...\"
PING -n 1 $PC
}

Th

相关标签:
1条回答
  • 2020-12-11 12:38

    Note: It is the involvement of Start-Process that complicates the solution significantly - see below. If powershell were invoked directly from PowerShell, you could safely pass a script block as follows:

    powershell ${function:Check-PC}  # !! Does NOT work with Start-Process
    

    ${function:Check-PC} is an instance of variable namespace notation: it returns the function's body as a script block ([scriptblock] instance); it is the more concise and faster equivalent of
    Get-Content Function:Check-PC.

    If you needed to pass (positional-only) arguments to the script block, you'd have to append -Args, followed by the arguments as an array (,-separated).


    Start-Process solution with an auxiliary self-deleting temporary file:

    See the bottom half of this answer to a related question.


    Start-Process solution with -EncodedCommand and Base64 encoding:

    Start-Process powershell -args '-noprofile', '-noexit', '-EncodedCommand', `
      ([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes(
        (Get-Command -Type Function Check-PC).Definition)))
    

    The new powershell instance will not see your current session's definitions (unless they're defined in your profiles), so you must pass the body of your function to it (the source code to execute).

    (Get-Command -Type Function Check-PC).Definition returns the body of your function definition as a string.

    The string needs escaping, however, in order to be passed to the new Powershell process unmodified.
    " instances embedded in the string are stripped, unless they are either represented as \" or tripled (""").
    (\" rather than the usual `" is needed to escape double quotes in this case, because PowerShell expects \" when passing a command to the powershell.exe executable.)

    Similarly, if the string as a whole or a double-quoted string inside the function body ends in (a non-empty run of) \, that \ would be interpreted as an escape character, so the \ must be doubled.Thanks, PetSerAl.

    The most robust way to bypass these quoting (escaping) headaches is to use a Base64-encoded string in combination with the -EncodedCommand parameter:

    • [Convert]::ToBase64String() creates a Base64-encoded string from a [byte[]] array.

    • [Text.Encoding]::Unicode.GetBytes() converts the (internally UTF-16 - "Unicode") string to a [byte[]] array.


    In case you want to pass the complete function, so it can be called by name in order to pass parameters, a little more work is needed.

    # Simply wrapping the body in `function <name> { ... }` is enough.
    $func = (Get-Command -Type Function Check-PC)
    $wholeFuncDef = 'Function ' + $func.Name + " {`n" + $func.Definition + "`n}"
    
    Start-Process powershell -args '-noprofile', '-noexit', '-EncodedCommand', `
      ([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("$wholeFuncDef; Check-PC")))
    

    Start-Process solution with regex-based escaping of the source code to pass:

    PetSerAl suggests the following alternative, which uses a regex to perform the escaping. The solution is more concise, but somewhat mind-bending:

    Start-Process powershell -args '-noprofile', '-noexit', '-Command', `
      ('"' + 
       ((Get-Command -Type Function Check-PC).Definition -replace '"|\\(?=\\*("|$))', '\$&') +
       '"')
    
    • "|\\(?=\\*("|$)) matches every " instance and every nonempty run of \ chars. - character by character - that directly precedes a " char. or the very end of the string.

      • \\ is needed in the context of a regex to escape a single, literal \.
      • (?=\\*("|$)) is a positive look-ahead assertion that matches \ only if followed by " or the end of the string ($), either directly, or with further \ instances in between (\\*). Note that since assertions do not contribute to the match, the \ chars., if there are multiple ones, are still matched one by one.
    • \$& replaces each matched character with a \ followed by the character itself ($&) - see this answer of mine for the constructs you can use in the replacement string of a -replace expression.

    • Enclosing the result in "..." ('"' + ... + '"') is needed to prevent whitespace normalization; without it, any run of more than one space char. and/or tab char. would be normalized to a single space, because the entire string wouldn't be recognized as a single argument.

      • Note that if you were to invoke powershell directly, PowerShell would generally automatically enclose the string in "..." behind the scenes, because it does so for arguments that contain whitespace when calling an external utility (a native command-line application), which is what powershell.exe is - unlike the Start-Process cmdlet.
      • PetSerAl points out that the automatic double-quoting mechanism is not quite that simple, however (the specific content of the string matters with respect to whether automatic double-quoting is applied), and that the specific behavior changed in v5, and again (slightly) in v6.
    0 讨论(0)
提交回复
热议问题