如何让 PowerShell 4 cmdlet(例如 Test-NetConnection)在 Windows 7 上运行? [英] How do I get PowerShell 4 cmdlets such as Test-NetConnection to work on Windows 7?
问题描述
情况.在 Windows 7 SP1 机器上,我已更新为 Windows6.1-KB2819745-x64-MultiPkg.msu.此外,在 PowerShell 中,$PSVersionTable 现在报告PSVersion 4.0".
The situation. On a Windows 7 SP1 machine, I have updated with Windows6.1-KB2819745-x64-MultiPkg.msu. Furthermore, in PowerShell $PSVersionTable now reports ‘PSVersion 4.0’.
目前,我的结论是,许多 PowerShell 4 cmdlet(例如 Test-NetConnection)只能在 Windows 8.1 上运行.但是,我想知道是否有一种解决方法可以在我的 Windows 7 机器上导入 PowerShell 4 模块.
At present, my conclusion is that many PowerShell 4 cmdlets such Test-NetConnection, will only work on Windows 8.1. However, I was wondering if there was a work-around whereby I could import PowerShell 4 modules on my Windows 7 machine.
推荐答案
你不能.它们依赖于较新操作系统(8.0 或 8.1)的底层功能,并且无法移植回 Windows 7 .另一种方法是编写您自己的函数/模块,以使用 .NET 框架方法复制新的 cmdlet.
You cannot. They rely on the underlying features of the newer OS (8.0 or 8.1) and cannot be ported back to Windows 7 . The alternative is to write your own functions / modules to replicate the new cmdlets using .NET framework methods.
例如,Get-FileHash cmdlet 在 PowerShell 4.0 中是单行的,但要在 2.0 中复制,我们必须使用 .NET.
For instance, the Get-FileHash cmdlet is a one-liner in PowerShell 4.0, but to replicate in 2.0 we have to use .NET.
PowerShell v4
PowerShell v4
Get-FileHash -Algorithm SHA1 "C:\Windows\explorer.exe"
PowerShell v2
PowerShell v2
$SHA1 = new-object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider
$file = [System.IO.File]::Open("C:\Windows\explorer.exe",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
[System.BitConverter]::ToString($SHA1.ComputeHash($file)) -replace "-",""
$file.Close()
这篇关于如何让 PowerShell 4 cmdlet(例如 Test-NetConnection)在 Windows 7 上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!