gpt4 book ai didi

windows - 执行策略修改

转载 作者:可可西里 更新时间:2023-11-01 11:15:30 26 4
gpt4 key购买 nike

我需要在不同的服务器上运行几个脚本,这将反过来停止/启动许多其他服务器上的服务。

我创建了一个脚本来将策略更改为“绕过”并运行脚本,然后使更改恢复正常。

$LOGPATH = "output.txt"
$system_default_policy = Get-ExecutionPolicy
"Current execution policy $system_default_policy" | Out-File -FilePath $LOGPATH
if ($syste_default_policy -ne 'Bypass') {
"Changing the execution policy from $system_default_policy to Bypass" | Out-File -FilePath $LOGPATH -Append
Set-ExecutionPolicy Bypass -Force
"Successfully changed the execution policy to Bypass" | Out-File -FilePath $LOGPATH -Append
}

### executing the commands to stop/start the services

"Re-writing the changes to default policy" | Out-File -FilePath $LOGPATH -Append
Set-ExecutionPolicy $system_default_policy -Force
"Changed the policy to " + $(Get-ExecutionPolicy) | Out-File -FilePath $LOGPATH -Append

但是,在下面的情况下,这似乎是一个多余的过程。

  1. 如果执行策略已经是 Bypass,那么我只是在最后一行重新设置它。
  2. 我必须在同一台服务器上运行多个脚本,因此对于每个脚本,我都将其更改为“绕过”并将其设置回原始状态。

有没有其他方法可以让我在执行脚本之前只运行一次该脚本(以更改执行策略),然后在运行所有脚本后将其更改为原始值。

最佳答案

执行策略仅适用于脚本,因此不适用于在主机上调用或作为命令传递的代码。有多种方法可以做到这一点,其中一些是:

从远程计算机调用命令

Invoke-Command -ComputerName $Computer -ScriptBlock { 
# Code
}

powershell.exe -Command 本地

powershell.exe -Command "#code"

但是,通常在不更改配置的情况下运行脚本的最简单方法是

powershell.exe -ExecutionPolicy Bypass -File C:\yourscript.ps1

关于windows - 执行策略修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53690716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com