Powershell 2.0 sudo limitations

2020-03-31 04:11发布

问题:

Long time reader but new poster. I've been wrestling with this issue for an entire day and it's driving me nuts. After scouring this site and Google, I'm still stuck. Basically, I can't figure out how to implement a "sudo" in Powershell that works with the Copy-Item CmdLet. Here's my code:

PROFILE.PS1:

# Trigger UAC to elevate the supplied command
function Elevate-Process() {
    if($args.Length -eq 0) {
        error ('USAGE: ' + $MyInvocation.InvocationName + ' <executable> [arg1 arg2 arg3 ...]');
    } else {
        try {
            if($args.Length -eq 1) {
                Start-Process $args[0] -Verb RunAs;
            } else {
               Start-Process $args[0] -ArgumentList $args[1..$args.Length] -Verb RunAs;
            }
        } catch {
            error $_.Exception.Message;
        }
    }
}

# Display pretty-formatted error messages
function error() {
    # Validate function parameters
    Param(
        [Parameter(Mandatory=$true)]
        [ValidateScript({$_.GetType().Name -eq 'String'})]
        $sMessage
    );

    Write-Host $sMessage -BackgroundColor Yellow -ForegroundColor Red;
}

# Set aliases
set-alias sudo Elevate-Process;

APPLY-INI.PS1:

sudo Copy-Item '.\standard_menu.ini' 'C:\Program Files (x86)\Opera\ui\';
#sudo [System.IO.File]::Copy '.\webmailproviders.ini' 'C:\Program Files (x86)\Opera\defaults\webmailproviders.ini' $true;

Write-Host 'Press any key to exit...';
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null;

When I execute powershell.exe .\apply-ini.ps1, I get the following error:

This command cannot be executed due to the error: The system cannot find the file specified.

I've tried EVERYTHING I could possibly find without any luck. Hopefully someone can point me in the right direction.

回答1:

There are several subtleties to getting a good invoke-elevated command implemented. This is the current implementation of Invoke-Elevated that we use in the PowerShell Community Extensions (2.1 Beta). You might find it useful in gettings yours to work or you could just grab PSCX 2.1 Beta. :-)

 Invoke-Elevated
    Opens a new PowerShell instance as admin.
.EXAMPLE
    C:\PS> Invoke-Elevated Notepad C:\windows\system32\drivers\etc\hosts
    Opens notepad elevated with the hosts file so that you can save changes to the file.
.EXAMPLE
    C:\PS> Invoke-Elevated {gci c:\windows\temp | export-clixml tempdir.xml; exit}
    Executes the scriptblock in an elevated PowerShell instance.
.EXAMPLE
    C:\PS> Invoke-Elevated {gci c:\windows\temp | export-clixml tempdir.xml; exit} | %{$_.WaitForExit(5000)} | %{Import-Clixml tempdir.xml}
    Executes the scriptblock in an elevated PowerShell instance, waits for that elevated process to execute, then
    retrieves the results.
.NOTES
    Aliases:  su
    Author:   Keith Hill
#>
function Invoke-Elevated 
{
    Write-Debug "`$MyInvocation:`n$($MyInvocation | Out-String)"

    $startProcessArgs = @{
        FilePath     = "PowerShell.exe"
        ArgumentList = "-NoExit", "-Command", "& {Set-Location $pwd}"
        Verb         = "runas"
        PassThru     = $true
        WorkingDir   = $pwd
    }

    $OFS = " "
    if ($args.Count -eq 0)      
    {
        Write-Debug "  Starting Powershell without no supplied args"
    }
    elseif ($args[0] -is [Scriptblock]) 
    {
        $script  = $args[0]
        $cmdArgs = $args[1..$($args.Length)]
        $startProcessArgs['ArgumentList'] = "-NoExit", "-Command", "& {Set-Location $pwd; $script}"
        if ($cmdArgs.Count -gt 0)
        {
            $startProcessArgs['ArgumentList'] += $cmdArgs
        }
        Write-Debug "  Starting PowerShell with scriptblock: {$script} and args: $cmdArgs"
    }
    else
    {
        $app     = Get-Command $args[0] | Select -First 1 | ? {$_.CommandType -eq 'Application'}
        $cmdArgs = $args[1..$($args.Length)]
        if ($app) {
            $startProcessArgs['FilePath'] = $app.Path
            if ($cmdArgs.Count -eq 0)
            {
                $startProcessArgs.Remove('ArgumentList')
            }
            else
            {
                $startProcessArgs['ArgumentList'] = $cmdArgs
            }
            Write-Debug "  Starting app $app with args: $cmdArgs"
        }
        else {
            $poshCmd = $args[0]
            $startProcessArgs['ArgumentList'] = "-NoExit", "-Command", "& {Set-Location $pwd; $poshCmd $cmdArgs}"
            Write-Debug "  Starting PowerShell command $poshCmd with args: $cmdArgs"
        }
    }

    Write-Debug "  Invoking Start-Process with args: $($startProcessArgs | Format-List | Out-String)" 
    Microsoft.PowerShell.Management\Start-Process @startProcessArgs
}


回答2:

You are invoking Start-Process with a cmdlet (copy-item) as its filepath argument instead of the filepath of an executable file.

sudo xcopy '.\standard_menu.ini' 'C:\Program Files (x86)\Opera\ui\';

will do the trick.