启动
PowerShell
无法提供用户访问控制(User Access Control)。
❯❯ 🏠🏚 ◷ 10:40
👀 gsv -name XLServicePlatform | Stop-Service
Stop-Service : 由于以下错误无法停止服务“迅雷下载基础服务(用于快速申请磁盘空间及接管浏览器下载请求) (XLServicePlatform)”: 无法打
开计算机“.”上的 XLServicePlatform 服务。
所在位置 行:1 字符: 31
+ gsv -name XLServicePlatform | Stop-Service
+ ~~~~~~~~~~~~
+ CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Stop-Service],ServiceCommand
Exception
+ FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.StopServiceCommand
❯❯ 🏠🏚 ◷ 10:40
👀
需要使用管理员权限打开
❯❯ etc ◷ 10:33
👀 gsv -name XLServicePlatform | Stop-Service
❯❯ etc ◷ 10:46
👀
获取版本
❯❯ 🏠 ◷ 10:55
👀 $PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.752
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.752
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
❯❯ 🏠 ◷ 10:59
👀
PowerShell中有许多自动变量来存储状态信息。这些变量之一是 $PSVersionTable
,它包含一个哈希表,可用于显示相关的PowerShell版本。
Windows PowerShell的较新版本作为Windows管理框架(WMF)的一部分分发。
执行策略
与流行的看法相反,PowerShell中的执行策略不是安全边界。它旨在防止用户在不知不觉中运行脚本。确定的用户可以轻松绕过PowerShell中的执行策略。下表展示了不同的版本其默认的执行策略:
❯❯ 🏠 ◷ 11:19
👀 Get-ExecutionPolicy
Unrestricted
❯❯ 🏠 ◷ 11:19
👀 Set-ExecutionPolicy RemoteSigned
❯❯ 🏠 ◷ 11:19
👀 Get-ExecutionPolicy
RemoteSigned
❯❯ 🏠 ◷ 11:19
👀
无论执行策略设置如何,任何PowerShell命令都可以交互运行。执行策略仅影响在脚本中运行命令。建议是使用RemoteSigned
策略,该策略要求下载的脚本由受信任的发布者签名才能运行。
当执行策略设置为Restricted
时,PowerShell脚本将完全无法运行 。这是所有Windows客户端操作系统上的默认设置。若要演示此问题,请将以下代码另存为名为Stop-TimeService.ps1的.ps1文件。
Get-ExecutionPolicy
现将运行策略设置为“受限”(Set-ExecutionPolicy Restricted
),然后尝试运行该脚本:
❯❯ 🏠 ◷ 11:27
👀 .\getExecPolicy.ps1
.\getExecPolicy.ps1 : 无法加载文件 C:\Users\Administrator\getExecPolicy.ps1,因为在此系统上禁止运行脚本。有关详细信息,
请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ .\getExecPolicy.ps1
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
❯❯ 🏠 ◷ 11:28
👀
❯❯ 🏠 ◷ 11:28
👀 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
❯❯ 🏠 ◷ 11:31
👀 .\getExecPolicy.ps1
RemoteSigned
❯❯ 🏠 ◷ 11:31
👀
总结
- 学习了如何查找和启动PowerShell
- 以及如何创建以管理员身份启动PowerShell的快捷方式。
- 还了解了默认执行策略及其更改方法。