繁体   English   中英

我如何在脚本中使用提升的凭据运行 powershell,然后在其位置执行文件

[英]How do i Run powershell with elevated credential in the script then execute files in their locations

我想在单击 .ps1 时执行 3 个不同的文件。 我希望它已经拥有提升的凭据

$credential = New-Object System.Management.Automation.PsCredential(".administrator", (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force))

Start-Process CMD.exe -Verb runAs -PassThru 

Get-Item -FilePath '\\I:\Mo Khalifa\eBillingHub\utils\addper.bat'

Get-Item -FilePath '\\I:\Mo Khalifa\eBillingHub\utils\AddToTrustedSites.reg' 

Get-Item -FilePath '\\I:\Mo Khalifa\eBillingHub\utils\DotNetPermissions.reg'

我不知道它是否在做什么

尝试对您的 bat 文件使用Start-Process而不是Get-Item

Start-Process c:\path\to\file.bat

您还可以使用问题的答案来使用您的 reg 文件:

reg import .\path\to\reg.reg

您可以使用-Credentials参数传递您的凭据。 它将模拟用户运行命令。

因此,在您的 .ps1 脚本中,您可以在您调用的 PS Cmdlet 中添加-Credentials $credential

-Credential <PSCredential>
        Specifies a user account that has permission to perform this action. Type a user name, such as User01 or
        Domain01\User01, or enter a PSCredential object, such as one from the Get-Credential cmdlet. By default, the
        cmdlet uses the credentials of the current user.```

暂无
暂无

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

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