Jef*_*f B 10 windows-7 powershell batch
我正在处理我的 dotfiles,我想创建一个批处理脚本,它会在切换到新计算机时为我做一些初始设置,比如使用psget安装模块等......我也喜欢它继续将我的 ExecutionPolicy 更改为可用的内容。
我创建了一个批处理文件,它只是在BypassExecutionPolicy下触发一个 powershell 脚本:
powershell -ExecutionPolicy ByPass
-NoLogo -NoProfile -NoExit
-File .\set-policy.ps1
Run Code Online (Sandbox Code Playgroud)
set-policy.ps1 尝试以管理员身份运行 powershell 以更改 ExecutionPolicy:
Start-Process powershell -verb runas
-ArgumentList "-No Exit -Command { Set-ExecutionPolicy Restricted }"
Run Code Online (Sandbox Code Playgroud)
不幸的是,这似乎并没有做到这一点(下面的输出)。不确定是什么问题。
Set-ExecutionPolicy Restricted
PS C:\windows\system32> Get-ExecutionPolicy
RemoteSigned
Run Code Online (Sandbox Code Playgroud)
有关如何使用批处理文件 + powershell 脚本更改执行策略的任何提示?
dea*_*dog 14
问题在于您如何调用新的 PowerShell 进程;它似乎在 PowerShell 提示准备好之前执行命令,所以它们只是打印到控制台;我不知道为什么。无论如何,这里是修复。
这是您的 set-policy.ps1 文件的外观:
Start-Process PowerShell -ArgumentList "Set-ExecutionPolicy Restricted -Force" -Verb RunAs
Run Code Online (Sandbox Code Playgroud)
或者您可以在一行中从批处理文件中完成整个操作,如下所示:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList 'Set-ExecutionPolicy Restricted -Force' -Verb RunAs}"
Run Code Online (Sandbox Code Playgroud)
我提供了更多有关从批处理文件调用 PowerShell 脚本的信息,以及您为什么要在我的博客文章中执行此操作。
| 归档时间: |
|
| 查看次数: |
46148 次 |
| 最近记录: |