18

情况。在 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 模块。

4

7 回答 7

11

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

例如,Get-FileHash cmdlet 是 PowerShell 4.0 中的单行代码,但要在 2.0 中复制,我们必须使用 .NET。

PowerShell v4

Get-FileHash -Algorithm SHA1 "C:\Windows\explorer.exe"

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()
于 2014-01-21T11:31:16.997 回答
11

至少可以将 Test-NetConnection 移植回 Windows 7。只需从具有相同 PowerShell 版本(Windows 8.1、Windows 10 等)的受支持 Windows 计算机复制文件夹 NetTCPIP、DnsClient 和 NetSecurity。文件夹 - 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

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

于 2017-04-27T18:17:32.237 回答
5

添加到Anton 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"
于 2017-06-21T01:37:13.363 回答
0

据我所知,Windows Server 2008 R2/Windows 7 根本没有 .NET 方法用来实现 get-netstuff 的计数器。

一个新的 PowerShell 版本可以实现哈希比较等,因为这与任何东西都无关,只是一段代码。但是,如果您想使用,例如 Get-NetTCPConnection,则没有什么可显示的。

于 2018-09-25T09:47:43.677 回答
0

我看到几个断言可移植性的响应,我的测试证实了他们的断言:

从文件或通过 PSSession 将所有必需的模块导入到具有所需模块的主机。您运行的 PowerShell 控制台(x86 或 x64)的体系结构将决定您导入的模块体系结构。

对于那些:

  • 仍然无法完成这项工作并且
  • 确实需要可靠的 TCP 测试,但可能不需要 Test-NetConnection 和
  • 即使在 PowerShell v2.0 上也需要全部工作

你不妨试试这个。

# Create a TCP Client using .Net & attempt connection to target
$TCPClient = New-Object .net.sockets.tcpclient("[IP address or hostname]",[port])
# At the above point you may see familiar-looking Windows error messages in
# red text. (You may want to use "try/catch" if you intend to loop).
# Otherwise, check your new object's status to see if it worked
$TCPClient.Connected
# The response is either "True" or False"
# Now to avoid leaving idle connections, run:
$TCPClient.Close()

当然,应该可以创建一个循环来测试多个连接,并通过选择 $TCPClient 的属性来输出结果。我的初始测试表明您希望选择这些属性

你测试的地址

$TCPClient.Client.RemoteEndPoint.Address.IPAddressToString

您测试的端口

$TCPClient.Client.RemoteEndPoint.Port

结果

$TCPClient.Connected

HIH

于 2020-08-20T07:48:57.113 回答
-1

虽然 PowerShell 4.0 在 Windows 7 上可用,但正如 Knuckle-Dragger 所说,某些功能依赖于较新的操作系统功能。不幸的是,如文档中所述, Test-NetConnection 在 Windows 7 中不可用。

存在的测试连接基本上是 ping。Test- NetConnection提供了更多功能,允许选择诸如 TCP 端口、协议、路由跟踪和信息级别等内容。

TechNet 库中的 ScriptCenter 提供了一个 Send-Ping 脚本,但我认为这只有在您因某种原因被困在 PowerShell 3.0 上时才真正有用。

于 2014-06-09T15:13:56.830 回答
-11

我只能假设您安装了错误的软件包。确保从这里下载正确的包。

下面您将看到使用Test-ConnectionGet-FileHash运行带有 PowerShell 4 的 Windows 7 Service Pack 1 :

在此处输入图像描述

于 2014-05-30T18:28:27.153 回答