PowerShell 2.0中sudo的限制 [英] Powershell 2.0 sudo limitations
问题描述
很长一段时间的读者,但新的海报。 我一直在摔跤这个问题了整整一天,它的驾驶我坚果。淘本网站和谷歌之后,我仍然坚持。基本上,我不知道如何与复制 - 项目cmdlet的作品在PowerShell中实现一个命令。这是我的code:
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:
# 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 \适用-ini.ps1 ,我得到以下错误:
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.
推荐答案
有一些细微之处,以获得实现了良好的调用提升的命令。这是当前实现调用提升的,我们在 PowerShell的社区扩展(2.1测试版)使用。您可能会发现它有用gettings你的工作或你可以只抓PSCX 2.1 Beta版。 : - )
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
}
这篇关于PowerShell 2.0中sudo的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!