Powershell启动作业新窗口以管理员身份运行

发布于 11-15 16:29 字数 693 浏览 10 评论 0原文

我有一个脚本,它将一些命令写入另一个脚本,并使用 Start-Job 命令在新窗口中启动它们(我真的需要它)。 $cred 是本地管理员的凭据。主脚本从打开“以管理员身份运行”(作为机器管理员)的powershell窗口启动。命令需要使用计算机管理员帐户打开(例如使用“以管理员身份运行”打开新窗口)。但新窗口只能以本地管理员权限打开。我在互联网上搜索信息来解决这个问题,但失败了。可能有人知道我如何使用机器管理员的启动作业命令运行新窗口(“以管理员身份运行”)?

$ScriptFile = "$env:TEMP\ScriptBlock.ps1"
Write-Output "`$ProfileApp = <COMMAND> | Out-File $ScriptFile -Width 300
$PJob = Start-Job -Name PJob -Credential $cred -FilePath $ScriptFile -Verbose -InitializationScript {Add-PsSnapin Microsoft.SharePoint.PowerShell} | Wait-Job
Receive-Job -Name PJob -Verbose
Remove-Job -Name PJob -ErrorAction SilentlyContinue -ErrorVariable err
Remove-Item -Path "$env:TEMP\ScriptBlock.ps1"

I have script which write some COMMAND to another script and start them in new window with Start-Job command (I really need that). $cred is credential of local administrator. Main script started from powershell windows which opened "run as administrator" (as machine administrator). COMMAND need to open with machine administrator account (like new window open with "run as administrator"). BUT new window open only with local administrator rights. I search in the internet for information to round this issue, but failed. May be someone know how i can run new window with start-job command with machine administrator ("run as administrator")?

$ScriptFile = "$env:TEMP\ScriptBlock.ps1"
Write-Output "`$ProfileApp = <COMMAND> | Out-File $ScriptFile -Width 300
$PJob = Start-Job -Name PJob -Credential $cred -FilePath $ScriptFile -Verbose -InitializationScript {Add-PsSnapin Microsoft.SharePoint.PowerShell} | Wait-Job
Receive-Job -Name PJob -Verbose
Remove-Job -Name PJob -ErrorAction SilentlyContinue -ErrorVariable err
Remove-Item -Path "$env:TEMP\ScriptBlock.ps1"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见2024-11-22 16:29:52

我相信这可能就是您正在寻找的:
自提升 PowerShell 脚本

I believe this might be what you are looking for:
A self elevating PowerShell script

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文