【问题标题】:Run a PowerShell script with Administrator privileges through NAnt通过 NAnt 以管理员权限运行 PowerShell 脚本
【发布时间】:2011-07-24 12:01:38
【问题描述】:

我目前正在构建一个自动化的 NAnt 脚本,它将部署 SharePoint Web 部件。这个过程的NAnt代码是:-

<echo message="Deploying Solutions" />
<exec failonerror="true" program="${powershell.exe}">
    <arg value="-noprofile" />
    <arg value="-nologo" />
    <arg value="-noninteractive" />
    <arg value="-command" />
    <arg value=" &quot;&amp; &apos;${deploysolutions.ps1}&apos; &apos;${solutionconfiguration.xml}&apos; &quot; " />
</exec>

其中 ${deploysolutions.ps1} 是要运行的脚本,${solutionconfiguration.xml} 是要传递给脚本的参数。

这在我的本地机器上运行时效果很好。当我尝试通过我们的 SharePoint 开发服务器上的巡航控制运行它时,我遇到了权限错误。我已验证所使用的凭据具有必要的权限。我在构建日志中看到的错误是:-

     [exec] Deploy-Solution : Unable to add solution MyWebParts.wsp

这是我在 PowerShell 脚本中抛出的错误

try
{
    Write-Host "Adding solution $name"
    Add-SPSolution -LiteralPath $path -ErrorAction Stop
    Start-Sleep -Seconds 60
    # as above
}
catch 
{
    Write-Error "Unable to add solution $name"
    throw "Could not add solution $name"
}

但是,当以管理员身份运行时,通过 PowerShell 控制台运行相同的命令时,凭据很好。

有没有办法让 NAnt 调用以管理员身份运行 PowerShell?我尝试了以下方法但没有成功:-

1. <arg value="Start-Process powershell -verb runas -FilePath &quot; -file ${deploysolutions.ps1} &quot; -ArgumentList &apos;${solutionconfiguration.xml}&apos; " />

2. <arg value=" &quot; start-process powershell -verb runas &amp; &apos;${deploysolutions.ps1}&apos; &apos;${solutionconfiguration.xml}&apos; &quot; " />

3. <arg value=" &apos; start-process powershell -verb runas &apos; " />
<arg value=" &quot;&amp; &apos;${deploysolutions.ps1}&apos; &apos;${solutionconfiguration.xml}&apos; &quot; " />

我也尝试通过 PowerShell 脚本执行管理员权限,但也没有成功。有人有什么想法吗?

【问题讨论】:

    标签: powershell nant


    【解决方案1】:

    您不能将对 powershell 的调用封装在命令 runas 中吗? 例如runas /user:adminuser powershell.exe

    在你的 exec 语句中?

    【讨论】:

    • 不,很遗憾没有。这是一个自动脚本,按照您的建议执行会导致提示输入密码。此外,用户是管理员。问题是需要在“以管理员身份运行”模式下运行 PowerShell。
    • 我发誓它认为你可以在脚本中提供密码,看起来我错了。
    猜你喜欢
    • 2019-07-03
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 2016-09-28
    • 2010-10-26
    • 1970-01-01
    相关资源
    最近更新 更多