简体   繁体   English

在PowerShell脚本中以管理员身份运行Command。 UAC

[英]Run Command as administrator in PowerShell script. UAC

OK here is my issue: 好的,这是我的问题:

I am trying to run a script remotely on a server. 我试图在服务器上远程运行脚本。

I am an administrator on both boxes, firewall exceptions are in place, remote admin is enabled, and everything else looks good that i can see. 我是两个盒子的管理员,防火墙例外已经到位,启用了远程管理,其他一切看起来都很好,我可以看到。

invoke-command -ComputerName $ComputerName -ScriptBlock `
{
    cd C:\Windows\System32\inetsrv\; 
    ./appcmd.exe ADD vdir /app.name:<SiteName>/ /path:/<VDir Name> /physicalPath:<Path to files>
}

I keep getting the following error in return 我一直收到以下错误

ERROR ( hresult:80070005, message:Failed to commit configuration changes. Access is denied.

The server it is trying to run on is a server 2k8 R2 box and I am thinking the issue is a UAC problem. 它试图运行的服务器是服务器2k8 R2盒子,我认为这个问题是UAC问题。 Is there anyway to get this to run as administrator without having to click yes on a UAC box? 无论如何要让它以管理员身份运行而不必在UAC盒子上单击是吗?

This piece of code will eventually become a script that will have to be completely automated. 这段代码最终将成为一个必须完全自动化的脚本。

Any help would be greatly appreciated. 任何帮助将不胜感激。

OK. 好。 After some research and testing I figured out the issue. 经过一些研究和测试,我发现了这个问题。 After disabling UAC and the firewall and the script still not working I dug a little deeper and discovered that the main issue was the way invoke-command runs the commands. 在禁用UAC和防火墙并且脚本仍无法正常工作之后,我深入挖掘并发现主要问题是invoke-command运行命令的方式。 it uses the credentials of the person running the script to authenticate to the server then tries to use another account to run the permissions or lowers the privileges of the user so that certain commands cannot be run. 它使用运行脚本的人员的凭据对服务器进行身份验证,然后尝试使用其他帐户来运行权限或降低用户的权限,以便无法运行某些命令。

I added the -Credentials switch to the invoke command and everything is working great now. 我将-Credentials开关添加到了invoke命令,现在一切正常。 Corrected code sample below: 更正了以下代码示例:

$user = New-Object Management.Automation.PSCredential("$UserName", $securePassword)
invoke-command -ComputerName $ComputerName -Credential $user -ScriptBlock ` 
{ 
    cd C:\Windows\System32\inetsrv\;  
    ./appcmd.exe ADD vdir /app.name:<SiteName>/ /path:/<VDir Name> /physicalPath:<Path to files> 
} 

This seems to indicate that you need to ensure you are a local admin on the remote machine (although admittedly this is for WMI specifically). 似乎表明您需要确保您是远程计算机上的本地管理员(尽管这是专门针对WMI的)。 According to this you can change a registry key to stop UAC applying to remote logons for administrators (search for LocalAccountTokenFilterPolicy). 根据此,您可以更改注册表项以停止UAC应用于管理员的远程登录(搜索LocalAccountTokenFilterPolicy)。 That shouldn't disable UAC just not filter the token if you use powershell/WMI remotely with an administrator account. 如果您使用管理员帐户远程使用PowerShell / WMI,则不应禁用UAC而不过滤令牌。

Set the option "EnableLUA" (DWORD value) found in HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System to 0 and reboot. 将HKLM \\ SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ Policies \\ System中的“EnableLUA”选项(DWORD值)设置为0并重新启动。

This will disable UAC without a problem, I would do it for all your users, whether with or without permission is up to you, because the vista UAC is so horrid that I do believe the less people that have it on the better (at least in vista ) they are. 这将禁用UAC没有问题,我会为所有用户执行此操作,无论是否有权限取决于您,因为vista UAC非常可怕,我相信拥有它的人越少越好(至少在远景中他们是。 Thsi trick also works in Win7 too. Thsi技巧也适用于Win7。

Have fun with my registry trick :) 玩我的注册表技巧:)

PS: As it turns out SO is censoring comments that show how to disable UAC as far as my post/thread with the above answer is concerned (a diligent answer was removed). PS:事实证明SO是审查评论,显示如何禁用UAC,就我的帖子/帖子与上述答案而言(一个勤奋的答案被删除)。

Is there anyway to get this to run as administrator without having to click yes on a UAC box? 无论如何要让它以管理员身份运行而不必在UAC盒子上单击是吗?

If this were possible it would entirely defeat the point of UAC. 如果这是可能的话,那将完全打败UAC的观点。

Thus, it would appear your only real solution is to disable UAC on the box. 因此,看起来您唯一真正的解决方案是在盒子上禁用UAC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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