前言

上一篇博文,我介绍了一下powershell和cmd的对比。通过学习,我发现powershell的确比cmd更加power,也更加适应现在的使用场景。

那么本文将继续介绍一个powershell的另一个特性,执行策略。

execution policy描述

首先我们看看官网是怎么描述execution policy的:

PowerShell's execution policy is a safety feature that controls the conditions under which PowerShell loads configuration files and runs scripts. This feature helps prevent the execution of malicious scripts.

On a Windows computer you can set an execution policy for the local computer, for the current user, or for a particular session. You can also use a Group Policy setting to set execution policies for computers and users.

Execution policies for the local computer and current user are stored in the registry. You don't need to set execution policies in your PowerShell profile. The execution policy for a particular session is stored only in memory and is lost when the session is closed.

The execution policy isn't a security system that restricts user actions. For example, users can easily bypass a policy by typing the script contents at the command line when they cannot run a script. Instead, the execution policy helps users to set basic rules and prevents them from violating them unintentionally.

On non-Windows computers, the default execution policy is Unrestricted and cannot be changed. The Set-ExecutionPolicy cmdlet is available, but PowerShell displays a console message that it's not supported. While Get-ExecutionPolicy returns Unrestricted on non-Windows platforms, the behavior really matches Bypass because those platforms do not implement the Windows Security Zones.

我们总结一下,这段文字中提到了几个要点:

  1. powershell的执行策略是种安全特性,保证不会什么猫狗脚本都执行,对于操作系统是种保护。
  2. windows的执行策略执行粒度分3种:本机、当前用户、特定会话。本机和当前用户的策略会存进注册表,当前会话的执行策略只会进内存,会话一断就会消失。
  3. 执行策略并不会限制用户操作,只是给了用户一把安全锁,可用可不用。
  4. 对于非windows机器,不会有执行策略的设置,因此会建议windows机器来做设置。

常见的执行策略

明白了上面的几个要点,我们来看windows提供了哪些执行策略供我们使用。

  1. ByPass

    任何脚本都可以执行,且没有任何提示。

  2. Undefined

    没有设置脚本执行策略。

  3. Unrestricted

    允许运行未签名的脚本,但是会有安全性提示。

  4. Default

    默认策略,对客户端是Restricted,对服务端是RemoteSigned

  5. RemoteSigned

    Windows Server 2012 R2 之后的默认策略

    如果从网络下载的脚本会有限制,需要添加数字签名;如果是本地创建的脚本,则不需要数字签名可以运行。

但是问题来了,真的会有老哥从网络上下载到本地运行,而不是复制粘贴到本地,自己创建的文件里?

不会吧,不会吧?

  1. AllSigned

    本策略只允许运行具有数字签名的脚本。

  2. Restricted

    本策略允许运行命令,但无法运行脚本。

运行范围

上面也讲了,powershell有3种范围:本机、当前用户、当前会话。

可使用Scope选项进行设置。

PS C:\Windows\system32> Get-ExecutionPolicy -Scope LocalMachine
RemoteSigned
PS C:\Windows\system32> Get-ExecutionPolicy -Scope CurrentUser
Undefined
PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned -Scope CurrentUserc
执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“N”): A
PS C:\Windows\system32> Get-ExecutionPolicy -Scope CurrentUser
RemoteSigned

cmd调用powershell

由于powershell和cmd是不同的命令行,而且powershell有执行策略的限制,如果我们想使用cmd去运行powershell脚本,除了需要设置powershell的策略,还需要使用cmd设置ps1脚本默认的运行方式。

默认方式是打开/编辑,而不是运行。

设置成运行:

ftype Microsoft.PowerShellScript.1="%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" "%1"

设置成编辑:

ftype Microsoft.PowerShellScript.1="%SystemRoot%\system32\notepad.exe" "%1"

鸣谢

官网

powershell脚本执行策略

CMD命令行修改.ps1文件(powershell脚本)的默认打开方式

powershell 执行策略的更多相关文章

  1. 绕过PowerShell执行策略方法总结

    默认情况下,PowerShell配置为阻止Windows系统上执行PowerShell脚本.对于渗透测试人员,系统管理员和开发人员而言,这可能是一个障碍,但并非必须如此. 什么是PowerShell执 ...

  2. Win powershell执行策略配置

    参考连接:https://blog.csdn.net/jeffxu_lib/article/details/84710386 参考连接:http://www.cragsman.org/index.ph ...

  3. PowerShell~执行策略的介绍

    首先看一下无法加载ps1脚本的解决方法 事实上也是由于策略导致的  解决方法主是开启对应的策略 set-ExecutionPolicy RemoteSigned 执行策略更改 执行策略可以防止您执行不 ...

  4. 【原创】NuGet 出现“无法初始化 PowerShell 主机,如果将你的 PowerShell 执行策略设置设置为 AllSigned ,请先打开程序包管理控制台以初始化该主机” 错误的解决方法

    现象: 网上的设置 AllSigned 等方法都无效..后来考虑可能跟命令行版本兼容性有关系,然后在注册表命令行配置里发现一 ForceV2 设置项,抱着试一试的心态改了下,果然解决了! 解决方法:修 ...

  5. PowerShell 脚本执行策略

    为防止恶意脚本的执行,PowerShell 中设计了一个叫做执行策略(Execution Policy)的东西(我更倾向于把它叫做脚本执行策略).我们可以在不同的应用场景中设置不同的策略来防止恶意脚本 ...

  6. python虚拟环境virtualenv、virtualenv下运行IDLE、powershell 运行脚本由执行策略引起的问题

    一.为什么要创建虚拟环境: 应为在开发中会有同时对一个包不同版本的需求,创建多个开发环境就能解决这个问题.或许也会有对python不同版本的需求,这就需要使用程序来管理不同的版本,virtualenv ...

  7. Powershell ExecutionPolicy 执行策略

    简单说明 powershell对于脚本的执行有着严格的安全限制 Get-ExecutionPolicy -List #查看当前的执行策略 Set-ExecutionPolicy -Scope Curr ...

  8. Powershell 执行外部命令

    Powershell 执行外部命令 724 11月, 2011  在 Powershell  tagged Powershell教程 / 程序 by Mooser Lee本文索引[隐藏]1通过nets ...

  9. PowerShell执行脚本时“系统上禁止运行脚本”问题解决

    PowerShell执行脚本策略错误 错误信息:PowerShell运行脚本错误--"系统上禁止运行脚本" 原因:默认执行策略为Restricted 解决:执行Set-Execut ...

随机推荐

  1. Flask框架实现登录注册功能(mysql数据库)

    前言: 本例使用Flask框架完成登录和注册操作,包括前端(index.html,regist.html)和后端(app.py)两部分,前端页面不过多介绍,直接进入后端部分: 逻辑思路: 登录部分:运 ...

  2. ES6 Promise 的链式调用

    1.什么是Promise Promise 对象代表了未来将要发生的事件,用来传递异步操作的消息. 2.对象的状态不受外界影响.Promise 对象代表一个异步操作,有三种状态: pending: 初始 ...

  3. js 定时器 Timer

    1 /* Timer 定时器 2 3 parameter: 4 func: Function; //定时器运行时的回调; 默认 null 5 speed: Number; //延迟多少毫秒执行一次 f ...

  4. java继承中关于super关键字和this关键字的使用

    1.super关键字 由于子类不能继承父类的构造方法,因此,如果要调用父类的构造方法,可以使用 super 关键字.super 可以用来访问父类的构造方法.普通方法和属性.super 关键字的功能: ...

  5. Nastran的应变方向

    问题 近日使用Nastran做一个算例,在计算频响时发现:位移场是连续的,而应变场不连续.以某一频率处应变场为例,其上表面X.Y方向应变场分布如下图.此处关闭了云图的插值,所显示的为单元的应变,因此云 ...

  6. DS18B20数字温度计 (三) 1-WIRE总线 ROM搜索算法和实际测试

    目录 DS18B20数字温度计 (一) 电气特性, 寄生供电模式和远距离接线 DS18B20数字温度计 (二) 测温, ROM和CRC算法 DS18B20数字温度计 (三) 1-WIRE总线 ROM搜 ...

  7. Linux 装完后没有声音的解决办法

    备注:1)Ubuntu Desktop版本:16.042)Linux工作用户:root1. 临时方法在终端中执行命令:pulseaudio --start --log-target=syslog2. ...

  8. 南京大学 静态软件分析(static program analyzes)-- introduction 学习笔记

    一.Programming Languages体系 静态程序分析是编程语言中应用层面下的一个细分领域,它是一个非常重要的核心内容. 在理论部分,考虑的是如何设计一个语言的语法和语义,如何设计语言的类型 ...

  9. 倾斜摄影3D模型|手工建模|BIM模型 轻量化处理

    一.什么是大场景? 顾名思义,大场景就是能够从一个鸟瞰的角度看到一个大型场景的全貌,比如一个园区.一座城市.一个国家甚至是整个地球.但过去都以图片记录下大场景,如今我们可以通过建造3D模型来还原大场景 ...

  10. 简单ELK配置实现生产级别的日志采集和查询实践

    概述 生产问题 集群规模如何规划? 集群中节点角色如何规划? 集群之脑裂问题如何避免? 索引分片如何规划? 分片副本如何规划? 集群规划 准备条件 先估算当前系统的数据量和数据增长趋势情况. 现有服务 ...