Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameters to the script block when using PowerShell module "RunAsUser"

I am writing a PowerShell script to be used in a component in Datto RMM.

To run as the logged in user I am using the PowerShell module RunAsUser.

I would like to be able to pass variables in from the Datto job to a command inside the script block. Here is my code

$scriptblock = {
    param($Message, $WaitSeconds, $Title, $BoxType)

    $wshell = New-Object -ComObject Wscript.Shell
    $Output =  $wshell.Popup($Message, $WaitSeconds, $Title, $BoxType)
 
    # Due to scope, none of the variables in the scriptblock
    # can be seen outside the block so we need to
    # provide an ouput to the calling object if it wants it.
    Write-Host $Output
}

# see https://github.com/KelvinTegelaar/RunAsUser for specifics
# of invoke-ascurrentuser
$str = invoke-ascurrentuser -scriptblock $scriptblock -ArgumentList $env:Message, $env:WaitSeconds, $env:Title, $env:BoxType -CaptureOutput

I don't get an error but I also don't get a message box.

Some ways of doing it I have tried get me no error but a minimal message box as if the parameters were all blank.

like image 554
Tony Williams Avatar asked Sep 18 '26 02:09

Tony Williams


1 Answers

Invoke-AsCurrentUser, part of the RunAsUser module, as of v2.4.0 doesn't support argument-passing, and therefore has no -ArgumentList parameter.

Instead, you must "bake into" the script block any values from the caller's scope you want the script block to operate on:

# Create the script block from an expandable string that references
# variables from the caller's scope and expands them up front.
# Note: This assumes that the environment variables referenced below
#       (e.g. $env:Message) exist and have appropriate values.
$scriptblock = [scriptblock]::Create(@"
    # Note: NO param(...) declaration.

    # Note the need to `-escape those $ chars. that shouldn't be
    # expanded up front.
    `$wshell = New-Object -ComObject Wscript.Shell
    
    # Reference the environment variables here, using appropriate
    # embedded quoting:
    `$wshell.Popup("$env:Message", $env:WaitSeconds, "$env:Title", $env:BoxType)

    # Note: No need for an explicit output command:
    #       The .Popup() call's return value is *implicitly* output.
"@)

$str = Invoke-AsCurrentUser -ScriptBlock $scriptblock -CaptureOutput

Note:

  • The above uses the here-string variant of an expandable (interpolating), double-quoted string ("...") to create the source code for the script block with the caller's values embedded.

  • The source code is then converted to a script block with [scriptblock]::Create().

  • An upcoming version of the RunAsUser module will have built-in support for this, via a newly introduced -ExpandStringVariables switch (source code), which allows you to pass regular script-block literals ({ ... }).

    • Caveat: You'll still need to manually `-escape $ characters in front of variable references that you do not want expanded (interpolated up front), such as in the case of $wshell above, and you'll still need to use embedded quoting around variable references that may expand to values with spaces or other metacharacters, such as around $env:Message above.
like image 190
mklement0 Avatar answered Sep 20 '26 19:09

mklement0