PowerShell 7 新功能列表

PowerShell的下一个主要版本已经发布,它带来了一些重大变化。第七版包括并行执行、远程执行导入模块、新运算符等功能。在这篇文章中,我们将研究PowerShell 7.0上的所有(PowerShell 7.0)(New)功能。

PowerShell 7 功能

PowerShell 目录更改

当您安装 PowerShell 7(install PowerShell 7)时,它将安装到一个新目录,并与PowerShell 5.1一起运行。如果您是从PowerShell Core 6.x升级,那么它将覆盖PowerShell 6目录,并删除所有不必要的文件。这是您应该知道的目录列表:

PowerShell 7 安装到 -

%programfiles%\PowerShell\7

% programfiles  %programfiles%\PowerShell\7文件夹被添加到 $env:PATH

PowerShell 7安装程序包升级(PowerShell 7)PowerShell Core 6.x的早期版本:

Windows 上的 PowerShell Core 6.x:(PowerShell Core 6.x on Windows:)

%programfiles%\PowerShell\6

被替换为 

%programfiles%\PowerShell\7

Linux:

/opt/microsoft/powershell/6

被替换为 

/opt/microsoft/powershell/7

macOS: 

/usr/local/microsoft/powershell/6

被替换为

/usr/local/microsoft/powershell/7

PowerShell 7 中的新功能

我已尝试简要解释这些功能中的每一个以及它将如何帮助PowerShell 用户(users)。但是,请务必在Microsoft官方页面上阅读更多详细信息。

  1. 管道并行化
  2. 新运营商
  3. ConciseView 和Get-Error cmdlet
  4. 自动新版本通知
  5. (Invoke DSC)直接从PowerShell 7调用 DSC资源
  6. 兼容层

在测试实际代码之前,请务必检查环境变量。(Environment Variable)

1]管道并行化

您现在可以在使用 ForEach-Object -Parallel 时并行执行或处理对象,而不是使用序列方法。在我们关于卸载WIM的帖子中,此方法可以在不到 10 秒的时间内卸载三个WIM映像,而不是按顺序运行时几乎需要 25 秒。(WIM)这是它的示例代码:

Get-WindowsImage -Mounted | foreach -Parallel {Measure-Command {Dismount-WindowsImage -Discard -Path $_.Path}}

2] 新运营商

新增了三个运算符Ternary operator: a ? b : c, Pipeline chain operators: || and && and Null conditional operators: ?? and ??=.这些行为类似于简化的 if-else 语句。(behaves like a simplified if-else statement.)它们使编写代码变得容易,而不是一直使用 If-else 循环。

3] ConciseView和 Get-Error cmdlet

获取错误命令让 PowerShell 7

ConciseView是一个用户可选择的视图,它作为(ConciseView)PowerShell 7的默认视图启用。如果错误不是来自脚本,您将收到单行错误。但是,如果它来自脚本或存在解析错误,您将收到多行错误消息,以及显示错误发生在哪一行的指针。

然后,您将拥有一个新的 cmdlet Get-Error,它可以帮助您在需要时获得错误的详细视图。它可以显示最后发生的错误的完整详细信息,包括内部异常。

$Error | Get-Error

Get-Error -Newest 3 # Displays the last three errors that occurred in the session

4 ]自动(] Automatic)新版本通知

PowerShell 7开始,系统将每天检查一次更新,并在可用时通知新版本。该信息仅在后续会话开始时显示。PowerShell 更新(PowerShell Update)提供了三个标志

  • 默认(Default )GA、预览版和 RC 版本
  • Off  关闭更新通知功能
  • LTS仅通知长期服务 ( LTS ) GA 版本的更新

如果要在PowerShell 7(PowerShell 7)中关闭更新通知,请在PowerShell窗口中执行此命令。

$Env:POWERSHELL_UPDATECHECK = 'Off'

5]直接从PowerShell 7调用 DSC资源(Invoke DSC)

Invoke-DscResource cmdlet 运行指定PowerShell 所需状态配置(PowerShell Desired State Configuration)( DSC ) 资源的方法。这是一个实验性功能。

Using this cmdlet, configuration management products can manage Windows or Linux by using DSC resources. This cmdlet also enables debugging of resources when the DSC engine is running with debugging enabled.

6] 兼容层

它允许PowerShell用户在隐式Windows PowerShell会话中导入模块。使用它,您将能够将真正的命令作为远程计算机上的会话并将结果返回到本地会话。现在它支持导入模块,您可以在远程计算机上运行这些模块。

PowerShell也是(PowerShell)Linux和 macOS上可用的开源软件。很高兴看到PowerShell 7中有这么多新功能,我们希望它继续发展。您可以在 Microsoft 上阅读有关它的(about it on Microsoft)更多信息。



About the author

拥有超过 10 年经验的发烧友工程师和音频产品专家。我专注于从头到尾创造优质的音乐扬声器和耳机。我是解决音频问题以及设计新扬声器和耳机系统的专家。我的经验不仅仅是制造好产品。我也热衷于帮助他人成为最好的自己,无论是通过教育还是社区服务。



Related posts