如何让PowerShell 4 cmdlet(如Test-NetConnection)在Windows 7上运行?

Guy*_*mas 17 powershell windows-7 powershell-4.0

情况.在Windows 7 SP1计算机上,我已使用Windows6.1-KB2819745-x64-MultiPkg.msu进行了更新.此外,在PowerShell $ PSVersionTable现在报告'PSVersion 4.0'.

目前,我的结论是许多PowerShell 4 cmdlet如Test-NetConnection,只能在Windows 8.1上运行.但是,我想知道是否有可以在我的Windows 7机器上导入PowerShell 4模块的解决方法.

Knu*_*ger 12

你不能,他们依赖于较新操作系统(8.0或8.1)的底层功能,无法移植回W7.另一种方法是编写自己的函数/模块,以使用.NET框架方法复制新的cmdlet.

例如,Get-FileHash cmdlet是Powershell 4.0中的一个单行程序,但要在2.0中复制,我们必须使用.NET.

Powershell v4

Get-FileHash -Algorithm SHA1 "C:\Windows\explorer.exe"
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

  • 问题不是'你能在Windows 7上安装PowerShell 4',但是你可以在Windows 7上安装Test-NetConnection.关键词是'Net'Test-NetConnection,而不是Plain Test-Connection. (4认同)
  • **这个答案是错误的.你绝对可以在Windows 7上拥有powershell 4命令.** (3认同)
  • @Keltari给我展示一台装有Net模块cmdlet的Windows 7计算机,我将收回我的赞誉。 (2认同)

Ant*_*lov 8

至少可以将Test-NetConnection移植回Windows 7.只需从支持的Windows机器上复制文件夹NetTCPIP,DnsClient,NetSecurity,使用相同的PS版本(win8.1,Win10等).文件夹 - C:\ Windows\System32\WindowsPowerShell\v1.0\Modules.然后Import-Module -Name C:\Windows\System32\WindowsPowerShell\v1.0\Modules\NetTCPIP -Verbose

或者,您可以从远程计算机导入模块(例如win2012):

$rsession = New-PSSession -ComputerName win2012
Import-Module NetTCPIP -PSSession $rsession
Run Code Online (Sandbox Code Playgroud)

我在Windows 7 x64上遇到了同样的问题,从PowerShell 5.1开始,这两个解决方案都适用于我.

  • 由于清单问题,我无法导入这些内容:“无法导入模块,因为其清单包含一个或多个无效成员......” (2认同)

Ada*_*sha 5

添加到安东Krouglov的答案。PowerShell模块是跨平台兼容的。因此,可以将从Windows Server 2012 R2 x64复制的模块导入Windows 7 x86,即使您以标准用户身份运行而没有将其复制到的权限,C:\Windows\System32\WindowsPowerShell\v1.0\Modules也可以将其复制到任何本地文件夹中并运行。

假设您从Windows Server 2012或更高版本的计算机复制了NetTCPIPDnsClientNetSecurity模块,并将其保存到文件夹中,则可以使用

Get-ChildItem -Directory .\psmodules | foreach { Import-Module -Name $_.FullName -Verbose}
Test-NetConnection -InformationLevel "Detailed"
Run Code Online (Sandbox Code Playgroud)