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.
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 ({ ... }).
`-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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With