【问题标题】:How do i Run powershell with elevated credential in the script then execute files in their locations我如何在脚本中使用提升的凭据运行 powershell,然后在其位置执行文件
【发布时间】:2019-08-20 23:43:47
【问题描述】:

单击 .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'

我不知道它是否在做任何事情

【问题讨论】:

  • 仅供参考,上面的脚本并不适用,我只需要帮助正确构建它

标签: powershell


【解决方案1】:

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

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

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

reg import .\path\to\reg.reg

【讨论】:

    【解决方案2】:

    您可以使用-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.```
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 2020-07-23
      • 2019-09-26
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      相关资源
      最近更新 更多