简体   繁体   中英

Powershell Getting $myIvocation of calling script

I am trying to create an Elevate function that Elevates the script that called the function However While the function in it's current form works I am trying to make it more elegant by removing the Invocation Parameter and getting the function to automatically determine the information by using scope.

Here is what works currently

Function Elevate ($Invocation)
{
    $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
    $myWindowsPrincipal=New-Object System.Security.Principal.WindowsPrincipal($myWindowsID)
    $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
    if (-not $myWindowsPrincipal.IsInRole($adminRole))
    {
        $newProcess = New-Object System.Diagnostics.ProcessStartInfo "Powershell";
        $newProcess.Arguments = $Invocation.MyCommand.Definition
        $newProcess.Verb = "runas";
        [System.Diagnostics.Process]::Start($newProcess);
        exit;
    }
}

I want to do away with the $Invocation variable if possible. Is there a way to get this from the parent?

Thanks in advance

Tim

The Get-PSCallStack command might get you there. The second to last CallStackFrame would be your caller. That object has various bits of info:

57> function foo { (Get-PSCallStack) | Select -Last 2 -Skip 1 }
58> foo 12

Command                             Arguments                           Location
-------                             ---------                           --------
foo                                 {12}                                <No file>

If you examine the object returned it also has an InvocationInfo property.

I figured out what I did wrong and modified the function, and it now works without explicitly passing the $Myinvocation to the function

I used the Script: scope to pick up the value of $MyInvocation from the parent.

Turns out that the first time I did it I tried using $Script.Myinvocation, which of course didn't work.

Here's the working function.

# Elevate.ps1

Function Elevate
{
    $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
    $myWindowsPrincipal=New-Object System.Security.Principal.WindowsPrincipal($myWindowsID)
    $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
    if (-not $myWindowsPrincipal.IsInRole($adminRole))
    {
        $newProcess = New-Object System.Diagnostics.ProcessStartInfo "Powershell";
        $newProcess.Arguments = $Script:MyInvocation.MyCommand.Definition;
        $newProcess.Verb = "runas";
        [System.Diagnostics.Process]::Start($newProcess);
        exit;
    }
}

And a short test script

# TestElevate.ps1

. .\Elevate.ps1
Elevate

# Whatever code you want executed in Elevated mode would go here
Write-Host "Elevate done"
sleep -s 2

Tim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM