2014-01-21 113 views
13

這種情況。在Windows 7 SP1計算機上,我使用Windows6.1-KB2819745-x64-MultiPkg.msu進行了更新。此外,在PowerShell中,$ PSVersionTable現在報告'PSVersion 4.0'。如何讓PowerShell 4 cmdlet(如Test-NetConnection)在Windows 7上運行?

目前,我的結論是許多PowerShell 4 cmdlet如Test-NetConnection只能在Windows 8.1上運行。但是,我想知道是否可以在我的Windows 7機器上導入PowerShell 4模塊。

+0

您接受的答案是錯誤的。您絕對可以在Windows 7上安裝PowerShell 4命令。 – Keltari

回答

13

你不能,他們依賴較新的操作系統(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" 

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() 
+0

謝謝。我看到越來越多的PowerShell 4功能的示例僅適用於Windows 8.1和Server 2013. –

+2

**此答案爲錯誤。您絕對可以在Windows 7上安裝PowerShell 4命令。** – Keltari

+3

問題不在於「Windows 7上可以安裝PowerShell 4」,但您可以在Windows 7上安裝Test-NetConnection。關鍵詞是「Net」Test- NetConnection,而不是簡單的測試連接。 –

-9

我只能假設你安裝了錯誤的包。確保您從here下載適當的軟件包。

下面你將使用測試連接和Get-FileHash運行Windows 7 Service Pack 1的使用PowerShell 4參見:

enter image description here

+5

這個屏幕截圖顯示了簡單的Test-Connection。我詢問了另一個名爲Test NetConnection的cmdlet。 –

+0

hm ... well get-filehash is there – Keltari

+0

屏幕截圖問題放在一邊,這個答案仍然是錯誤的,因爲'Test-NetConnection'(在OP中被請求)根本不可用於Win7。 – Iszi

-1

雖然PowerShell的4.0適用於Windows 7,因爲肘牽引狀態某些功能依賴於較新的操作系統功能。不幸的是,Test-NetConnection在Windows 7中不可用,如documentation中所述。

Test-Connection,這是目前,基本上是平。測試 - NetConnection提供更多的功能,允許選擇諸如TCP端口,協議,路由追蹤和信息級別等。

在Technet圖庫中的ScriptCenter中有一個Send-Ping腳本,但是我認爲這僅僅因爲某些原因停留在Powershell 3.0上纔有用?

+0

Test-NetConnection [可以移植回Windows 7](http://srv-spb.ru/microsoft/windows/kak-proverit-port-cherez-powershell-na-windows-7.html) –

3

至少Test-NetConnection可以移植到Windows 7.只需從支持的Windows機器上覆制相同PS版本的文件夾NetTCPIP,DnsClient,NetSecurity(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 

我有同樣的問題,在我的Windows 7 x64和解決方案都爲我工作作爲PowerShell的5.1。

+0

它甚至可以跨越x86和x64版本。我能夠將TCPIP模塊從Server 2012 R2 x64導入我的x86筆記本電腦。 – Adarsha

+0

儘管導入看起來很成功,但是'Test-NetConnection'中有一個函數引用了一個不存在的函數'Resolve-DnsName'。反正至少在我的系統上。 – YetAnotherRandomUser

+0

由於存在明顯問題,我無法導入這些文件: 「模塊無法導入,因爲其清單包含一個或多個無效的成員...」 – fix

4

添加到@Anton Krouglov的回答。 PowerShell模塊是跨平臺兼容的。因此,從Windows Server 2012 R2 x64複製模塊可以導入到Windows 7 x86,即使您運行的是標準用戶無權獲得將它們複製到C:\Windows\System32\WindowsPowerShell\v1.0\Modules可以把它複製到任何本地文件夾,然後運行

假設你複製NetTCPIPDnsClientNetSecurity模塊,並將它們保存到可以使用導入它們的文件夾中

Get-ChildItem -Directory .\psmodules | foreach { Import-Module -Name $_.FullName -Verbose} 
Test-NetConnection -InformationLevel "Detailed" 
+0

在Windows 7(V6.1.7601)上使用PowerShell 5.0進行確認.10586.117。 要擺脫Resolve-DnsName錯誤,只需導入NetTCPIP和DnsClient。 –

相關問題