当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Powershell 远程签名 |了解 Remotesigned 策略功能

作者:精品下载站 日期:2024-12-14 05:00:12 浏览:14 分类:玩电脑

Powershell 远程签名 |了解 Remotesigned 策略功能


[玩转系统] Powershell 远程签名 |了解 Remotesigned 策略功能

PowerShell 远程签名简介

PowerShell 有一些策略来确定 PowerShell 可以运行配置文件和脚本的情况。这可以防止 PowerShell 运行或执行恶意代码或软件。 Windows 机器中的执行策略可以为用户、机器或会话设置。还可以为一组特定的用户创建组策略。系统和当前用户的执行策略都保存在注册表中。会话的执行策略保存在内存中,会话关闭后就会丢失。可用的执行策略有“全部签名”、“绕过”、“默认”、“远程签名”、“受限”、“未定义”和“无限制”。默认策略是远程签名。本文将详细介绍 PowerShell 中的远程签名执行策略。在本主题中,我们将了解 Powershell Remotesigned。

远程签名执行策略功能

远程签名执行策略是默认策略。在此执行策略下,可以运行脚本。它需要数字签名才能运行从互联网下载的脚本和配置文件。它不需要数字签名来运行在本地计算机上开发的文件。要运行没有数字签名的文件,必须使用unlock-File cmdlet。运行没有数字签名的脚本可能是致命的。

执行策略是使用 Set-ExecutionPolicy cmdlet 设置的。

语法:

NAME
Set-ExecutionPolicy
SYNTAX
Set-ExecutionPolicy [-ExecutionPolicy] {Unrestricted | RemoteSigned | AllSigned | Restricted | Default | Bypass | Undefined} [[-Scope] {Process | CurrentUser |
LocalMachine | UserPolicy | MachinePolicy}] [-Force] [-WhatIf] [-Confirm]  [<CommonParameters>]
ALIASES
None
To set the policy as remote signed, the following cmdlet is used



Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

To see the execution policy settings the following cmdlet can be used. Scope ExecutionPolicy ----- --------------- MachinePolicy Undefined UserPolicy Undefined Process Undefined CurrentUser Undefined LocalMachine Unrestricted

1.执行政策范围

可以针对一定范围制定政策。范围的允许值为计算机策略、用户策略、进程、本地计算机和当前用户。默认执行策略是本地机器。

2.机器政策

它用于将策略应用于系统的所有用户。

3.用户政策

它为当前用户创建组策略。

4.流程

这仅适用于当前会话,其值存储在环境变量中而不是注册表中。 PowerShell 会话关闭后,该变量和相应的值将被删除。

5.当前用户

这仅适用于当前用户。它位于注册表的 HKEY_CURRENT_USER 项中。

6.本地机器:

它用于为系统中的所有用户设置策略。 它位于注册表的 HKEY_LOCAL_MACHINE 中。

为一个会话设置策略

可以为会话及其子会话设置执行策略。为此,可以使用 pwsh.exe。这存储在环境变量“$env: PSExecutionPolicyPreference”中。会话关闭后,该值将被删除。在会话内部,该执行策略的优先级高于注册表中设置的优先级。它永远不能优先于组执行策略。

示例:

pwsh.exe -ExecutionPolicy LocalMachine

输入:

Write-Host "Example of remote signed policy"
Write-Host "Setting the policy to remote signed"
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Write-Host "Policy changed to remote signed"
write-host "Setting local computers from a remote"
Invoke-Command -ComputerName testserver1 -ScriptBlock { Get-ExecutionPolicy } | Set-ExecutionPolicy
Write-Host "Policy set to Local Machine"
Write-Host "Setting the scope for current user to remote signed"
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Write-Host "Policy set to remote signed for local user"
Write-Host "Removing the user policy"
Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser
Write-Host "Current users execution policy removed"
Write-Host "Setting the current sessions policy to remote signed"
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
Write-Host "policy set for current session"
Write-Host "Running a script without digital signature"
Unblock-File c:\vignesh\test.ps1
Write-Host "File executed successfully"
Write-Host "Setting the  policy to remote signed for local machine"
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Write-Host "policy set for local machine"
Write-Host "Setting the  policy to remote signed for user group"
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope UserPolicy
Write-Host "policy set for user group"
Write-Host "Setting the  policy to remote signed for all users"
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope MachinePolicy
Write-Host "policy set all users"

输出:

[玩转系统] Powershell 远程签名 |了解 Remotesigned 策略功能

其他 Powershell Remotesigned 执行策略功能

1.全部签名

这允许执行所有脚本,但需要对所有脚本(甚至是在本地计算机中开发的脚本)进行数字签名。运行脚本之前会出现提示。

2.旁路

执行脚本时不会显示任何警告或提示。

3.默认

Windows 客户端的默认策略是受限制的,而对于 Windows,服务器是远程签名的

4.受限

它只允许命令,不允许执行脚本。不允许使用任何配置文件、ps 文件或模块脚本。

5.无限制

非 Windows 计算机的策略无法更改。所有未签名的脚本都可以运行。

组策略功能

如果禁用启动脚本执行,则脚本将不会运行。这就像限制执行策略。要启用执行策略,必须启用打开脚本执行组策略。要允许所有脚本,则应将策略设置为不受限制。要允许执行具有数字签名的脚本,则必须对其进行远程签名,如果仅要执行签名的脚本,则必须对策略进行全部签名。

这些策略按以下顺序执行。机器策略的优先级最高,然后是用户策略,然后是进程,然后是当前用户,最后是本地机器。

结论 - Powershell Remotesigned

因此,本文详细介绍了 PowerShell 中的远程签名执行策略。它解释了远程签名策略的功能以及语法。本文展示了如何将策略设置为不同范围的远程签名的各种示例。它还简要介绍了其他可用的执行策略。要了解更多详细信息,建议编写示例程序并进行练习。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯