简体   繁体   English

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

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

I want to execute 3 different files when I click on the .ps1.我想在单击 .ps1 时执行 3 个不同的文件。 I want it to already have the elevated credentials within我希望它已经拥有提升的凭据

$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'

I don't know if its doing anything我不知道它是否在做什么

Try using Start-Process instead of Get-Item for your bat file:尝试对您的 bat 文件使用Start-Process而不是Get-Item

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

You can also use the answer from this question to use your reg files:您还可以使用问题的答案来使用您的 reg 文件:

reg import .\path\to\reg.reg

You can pass your credentials with the -Credentials parameter.您可以使用-Credentials参数传递您的凭据。 It will impersonate the user to run the commands.它将模拟用户运行命令。

So in your .ps1 script, you could add the -Credentials $credential in the PS Cmdlets that you're calling.因此,在您的 .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.

相关问题 如何从 Windows 命令提示符调用提升的 PowerShell 以执行命令和 PowerShell 脚本文件? - How do I call an elevated PowerShell from Windows command prompt to execute commands and a PowerShell script file? 如何在PowerShell中远程执行ELEVATED远程脚本 - How to remote execute an ELEVATED remote script in PowerShell 使用提升的命令运行Powershell脚本 - Run powershell script with elevated command 如何运行同时使用本地和提升权限的 powershell 脚本? - How can I run powershell script that uses both local & elevated permissions? 如何使用 robocopy 运行目录名称中有空格的 PowerShell 提升副本? - How do I use robocopy to run a powershell elevated copy with spaces in the directory names? 运行提升的Powershell脚本并导入系统模块 - Run elevated Powershell Script and import system modules 如何使用Powershell远程以提升的权限启动批处理脚本 - How can I start a batch script with elevated permissions remotely with powershell 如何在 Powershell 中存储 MFA 凭据 - How do I store a MFA credential in Powershell 如何在powershell的提升模式下执行命令集 - how to execute set of commands in elevated mode of powershell 如何将 PowerShell 脚本作为服务运行? - How do I run a PowerShell script as a service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM