Powershell 2.0 sudo limitations

后端 未结 2 599
無奈伤痛
無奈伤痛 2021-01-16 12:00

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.

相关标签:
2条回答
  • 2021-01-16 12:35

    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
    }
    
    0 讨论(0)
  • 2021-01-16 12:36

    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.

    0 讨论(0)
提交回复
热议问题