【问题标题】:Powershell 2.0 sudo limitationsPowershell 2.0 sudo 限制
【发布时间】:2012-01-31 19:26:43
【问题描述】:

长期阅读,但新海报。 我一整天都在为这个问题苦苦挣扎,这让我发疯了。在搜索了这个网站和谷歌之后,我仍然卡住了。基本上,我不知道如何在 Powershell 中实现与 Copy-Item CmdLet 一起使用的“sudo”。这是我的代码:

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;

应用-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;

当我执行 powershell.exe .\apply-ini.ps1 时,我收到以下错误:

该命令无法执行,错误:系统无法执行 找到指定的文件。

我已经尝试了所有可能找到的方法,但没有任何运气。希望有人能指出我正确的方向。

【问题讨论】:

    标签: .net windows shell scripting powershell


    【解决方案1】:

    实现一个好的调用提升命令有几个微妙之处。这是我们在PowerShell Community Extensions(2.1 Beta)中使用的Invoke-Elevated 的当前实现。您可能会发现它对您的工作很有用,或者您可以直接获取 PSCX 2.1 Beta。 :-)

    调用提升 以管理员身份打开一个新的 PowerShell 实例。 。例子 C:\PS> Invoke-Elevated Notepad C:\windows\system32\drivers\etc\hosts 打开使用主机文件提升的记事本,以便您可以保存对文件的更改。 。例子 C:\PS> 调用提升 {gci c:\windows\temp |导出-clixml tempdir.xml;出口} 在提升的 PowerShell 实例中执行脚本块。 。例子 C:\PS> 调用提升 {gci c:\windows\temp |导出-clixml tempdir.xml;退出} | %{$_.WaitForExit(5000)} | %{Import-Clixml tempdir.xml} 在提升的 PowerShell 实例中执行脚本块,等待提升的进程执行,然后 检索结果。 。笔记 别名:苏 作者:基思·希尔 #> 函数调用提升 { 写调试“`$MyInvocation:`n$($MyInvocation | Out-String)” $startProcessArgs = @{ FilePath = "PowerShell.exe" ArgumentList = "-NoExit", "-Command", "& {Set-Location $pwd}" 动词 = "runas" 直通 = $true 工作目录 = $pwd } $OFS = " " if ($args.Count -eq 0) { 写调试“在没有提供参数的情况下启动 Powershell” } elseif ($args[0] -is [脚本块]) { $script = $args[0] $cmdArgs = $args[1..$($args.Length)] $startProcessArgs['ArgumentList'] = "-NoExit", "-Command", "& {Set-Location $pwd; $script}" if ($cmdArgs.Count -gt 0) { $startProcessArgs['ArgumentList'] += $cmdArgs } 写入调试“使用脚本块启动 PowerShell:{$script} 和 args:$cmdArgs” } 别的 { $app = 获取命令 $args[0] |选择 - 前 1 | ? {$_.CommandType -eq '应用程序'} $cmdArgs = $args[1..$($args.Length)] 如果($app){ $startProcessArgs['FilePath'] = $app.Path if ($cmdArgs.Count -eq 0) { $startProcessArgs.Remove('ArgumentList') } 别的 { $startProcessArgs['ArgumentList'] = $cmdArgs } 写入调试“使用 args 启动应用程序 $app:$cmdArgs” } 别的 { $poshCmd = $args[0] $startProcessArgs['ArgumentList'] = "-NoExit", "-Command", "& {Set-Location $pwd; $poshCmd $cmdArgs}" 写入调试“使用 args 启动 PowerShell 命令 $poshCmd:$cmdArgs” } } 写入调试“使用 args 调用启动进程:$($startProcessArgs | Format-List | Out-String)” Microsoft.PowerShell.Management\Start-Process @startProcessArgs }

    【讨论】:

    • 哇,谢谢你的提示。但是,在使用 Invoke-Elevated 时,出现以下错误:
    • Set-Location : A positional parameter cannot be found that accepts argument 'Customizations'. At line:1 char:16 + &amp; {Set-Location &lt;&lt;&lt;&lt; C:\Users\Eric\Documents\Opera Customizations; cp .\standard_menu.ini C:\Pro gram Files (x86)\Opera\ui\standard_menu.ini} + CategoryInfo : InvalidArgument: (:) [Set-Location], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetLocatio nCommand
    • The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable progra m. Check the spelling of the name, or if a path was included, verify that the path is correct and t ry again. At line:1 char:107 + &amp; {Set-Location C:\Users\Eric\Documents\Opera Customizations; cp .\standard_menu.ini C:\Program F iles (x86 &lt;&lt;&lt;&lt; )\Opera\ui\standard_menu.ini} + CategoryInfo : ObjectNotFound: (x86:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
    • 我正在执行的代码如下:Invoke-Elevated cp '.\standard_menu.ini' 'C:\Program Files (x86)\Opera\ui\standard_menu.ini';
    • 将命令放在脚本块中,例如Invoked-Elevated {cp src target} 它会起作用。最后的 else 子句应该处理这个问题,但是目标路径周围的引号被去掉了。
    【解决方案2】:

    您正在调用 Start-Process,并使用 cmdlet(复制项)作为其文件路径参数,而不是可执行文件的文件路径。

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

    会成功的。

    【讨论】:

    • 你能说清楚一点吗?即使在 5 年后,powershell 中也没有 sudo 命令或别名。
    • @TanveerBadar sudo 已在问题中定义。
    • 糟糕,我错过了最后一行。
    猜你喜欢
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    • 2020-11-09
    • 1970-01-01
    相关资源
    最近更新 更多