0

我正在嘗試關注通過PowerShell部署服務結構應用的this文章,但運行Connect-ServiceFabricCluster cmdlet時遇到問題。我得到如下:使用PowerShell時找不到Connect-ServiceFabricCluster cmdlet

Connect-ServiceFabricCluster : The term 'Connect-ServiceFabricCluster' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, 
or if a path was included, verify that the path is correct and try again. 
At line:1 char:2 
+ Connect-ServiceFabricCluster 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (Connect-ServiceFabricCluster:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

繼互聯網上的其他文章中,我已經試過進口下列事情:

Import-Module "$ENV:ProgramW6432\Microsoft SDKs\Service Fabric\Tools\PSModule\ServiceFabricSDK\ServiceFabricSDK.psm1"

Import-Module "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ServiceFabric"

我也看到某處嘗試和設置導入模塊之前執行的政策,所以我嘗試這樣做:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope CurrentUser

在PowerShell ISE中的模塊部分,我看到ServiceFabricSDK模塊,但我沒有看到此cmdlet。

如何訪問這些cmdlet?

謝謝你的幫助。

當前版本:

運行$ PSVersionTable.PSVersion,我得到

Major Minor Build Revision 
----- ----- ----- -------- 
4  0  -1  -1 

服務織物SDK版本爲2.5.216

+1

在做其他事情之前,請確保您運行的是PowerShell x64版本,而不是x86版本。服務結構和相關的cmdlet *僅*可在64位環境中使用。 – yoape

回答

0

首先,我會設置你的政策,以繞過。這不能從腳本本身完成,因爲,這就是需要使用此策略運行的內容。你可以考慮設置你的powershell ise配置文件來爲你做這個。

Set-ExecutionPolicy Bypass 

對你的問題。並非所有模塊都可以使用導入模塊功能。例如,technet.microsoft.com站點中的模塊有時必須手動安裝和解壓縮。我在下面包含一個腳本來自動執行此操作。

#https://www.petri.com/manage-windows-updates-with-powershell-module\ 
    $url = "https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc/file/41459/47/PSWindowsUpdate.zip" 
    $module = "PSWindowsUpdate" 
    $zipped = "$($PSScriptRoot)\$($module).zip" 
    $unzipped = "C:\Windows\System32\WindowsPowerShell\v1.0\Modules" 
    #$unzipped = "$PSScriptRoot\$($module)" 

    if (Get-Module -Name $($module)) { 
      Write-Host "Module exists $($module)" 
     } else { 
      Write-Host "Getting Module $($module)" 

    if(!(Test-Path $zipped)){ 
     (New-Object System.Net.WebClient).DownloadFile($url, $zipped) 
     if($?){Write-Output "Downloaded zip $($zipped)"} 
    }else{ 
     Write-Output "Zip found $($zipped)" 
    } 

    if(!(test-path "$($unzipped)\$($module)")){ 
    Add-Type -assembly 「system.io.compression.filesystem」 
    [io.compression.zipfile]::ExtractToDirectory($zipped, $unzipped) 
    if($?){Write-Output "Unzipped to $($unzipped)"} 
} 
    Unblock-File -Path "$($unzipped)\$($module)" -Confirm 
    if($?){Write-Output "Unblocked file $($unzipped)"} 

    Import-Module $unzipped\*\$($module).psd1 -Verbose 
    if($?){Write-Output "Imported module $($unzipped)"}   
} 
+0

注意:「Unblock-File cmdlet允許您打開從Internet下載的文件,它可以解除從互聯網下載的Windows PowerShell腳本文件,以便您可以運行它們,即使Windows PowerShell執行策略是RemoteSigned。 ,這些文件被阻止以保護計算機免受不可信文件的攻擊。「 https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/unblock-file – primohacker

0

我在我的第一個回答中太倉促了。 (這很奇怪,因爲它需要一段時間才能輸入...)無論如何。看起來安裝過程實際上是爲你解開psm1的。

  1. 確保您以管理員身份運行,請使用它來檢查。

    ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity] :: GetCurrent())。IsInRole(` [Security.Principal.WindowsBuiltInRole]「管理員」)

  2. 確保文件名在第6步

  3. 當您運行導入模塊命令則步驟3場比賽的路徑,遵循了$?這會告訴你它是否正確導入。您也可以使用這些命令來查看它是否工作。

    get-command -name「Cluster」; get-module

+0

3.應該輸出什麼內容?我在列表中看到ServiceFabricSDK,它的類型爲Script,版本爲0.0,我看到一些導出的命令,但不是所有預期的命令。 – snaits

1

您是否運行x86版本的Powershell ISE?我也遇到了這個錯誤,但是當我切換到另一個ISE時,cmdlet又可用。

相關問題