開始使用 Configuration Manager Cmdlet

適用於:Configuration Manager (目前的分支)

使用Windows PowerShell來管理Configuration Manager階層。 您可以使用 PowerShell 腳本來自動化或擴充Configuration Manager類似于使用 WMI 和 C# 的其他記載方法。 如需詳細資訊,請參閱 Configuration Manager SDK

從 Configuration Manager 主控台或Windows PowerShell會話,在 PowerShell 中執行 Configuration Manager Cmdlet 和腳本。 當您使用 Configuration Manager 主控台執行 Configuration Manager Cmdlet 時,會話會自動在網站的內容中執行。

注意事項

所有目前支援的 Configuration Manager 最新分支版本都支援 Windows PowerShell 5.1 版。 如果您已安裝 PowerShell 第 7 版,您仍然可以使用 PowerShell 5.1 版。 如需詳細資訊,請參閱搭配使用 PowerShell 7 與 Windows PowerShell 5.1

Configuration Manager PowerShell Cmdlet 程式庫支援 PowerShell 7。 如需詳細資訊,請參閱 PowerShell 第 7 版的支援

從 2103 版開始,ConfigurationManager PowerShell 模組需要 Microsoft .NET 4.7.2 版或更新版本。

來自 Configuration Manager 主控台的 PowerShell

開啟 PowerShell 的最簡單方法是直接從 Configuration Manager 主控台。

  1. 啟動 Configuration Manager 主控台。 左上角有一個藍色矩形。 選取藍色矩形中的白色箭號,然後選擇 [透過Windows PowerShell連線]

  2. 載入Windows PowerShell之後,您會看到包含月臺碼的提示。 例如,如果月臺碼是 「ABC」,提示如下所示: PS ABC:\>

  3. 若要確認其運作正常,請使用 Get-CMSite Cmdlet。 此 Cmdlet 會傳回您目前連線的Configuration Manager網站和任何子月臺的相關資訊。 例如,月臺伺服器名稱、安裝目錄、月臺名稱和版本。

注意事項

當您從 Configuration Manager 主控台啟動 PowerShell 或 PowerShell ISE 時,它會針對進程範圍使用AllSigned執行原則。 如果此預設安全性群組態對您的環境而言太多,有兩個選項可解決此問題:

匯入 Configuration Manager PowerShell 模組

手動載入Configuration Manager模組,以從現有的Windows PowerShell會話連線到Configuration Manager。

  1. 從 [開始] 功能表開啟Windows PowerShell會話。

  2. 使用Import-Module Cmdlet 匯入Configuration Manager模組。 指定Configuration Manager模組的路徑,或變更為包含模組的目錄。 根據預設,模組位於下列路徑: C:\Program Files (x86)\Microsoft Endpoint Manager\AdminConsole\bin\ConfigurationManager.psd1

    從 2111 版開始,當您安裝 Configuration Manager 主控台時,模組的路徑現在會新增至系統內容變數PSModulePath。 如需詳細資訊,請 參閱 about_PSModulePath。 透過這項變更,您可以直接依其名稱匯入模組: Import-Module ConfigurationManager

    重要事項

    請確定您未匯入可能存在於另一個資料夾中的舊版模組。 匯入模組之後,請使用下列命令來檢查模組版本和路徑:

    (Get-Module -Name ConfigurationManager).Version
    (Get-Module -Name ConfigurationManager).Path
    

    下列範例會變更模組的目錄,然後將其匯入:

    Set-Location 'C:\Program Files (x86)\Microsoft Endpoint Manager\AdminConsole\bin'
    Import-Module .\ConfigurationManager.psd1
    

    提示

    您也可以使用 SMS_ADMIN_UI_PATH 環境變數。 例如:

    Set-Location "$env:SMS_ADMIN_UI_PATH\..\"
    

    此外,您可以使用 cd 別名來變更目錄,而不是 Set-Location Cmdlet。

  3. 如果這是第一次在此電腦上匯入Configuration Manager模組,您可能需要建立月臺磁片磁碟機。 例如:

    New-PSDrive -Name "ABC" -PSProvider "CMSite" -Root "siteserver.contoso.com" -Description "Primary site"
    

    提示

    當您從主控台啟動 PowerShell 時,它會自動建立 PSDrive,以方便目前連線的網站使用。 如果您在階層中,請使用 New-PSDrive 為每個月臺建立磁片磁碟機。

  4. 若要執行Configuration Manager Cmdlet,您必須將路徑切換至Configuration Manager網站。 在下列範例中,月臺碼為 ABC

    Set-Location ABC:
    
  5. 使用Get-CMSite Cmdlet 確認 PowerShell 已正確載入Configuration Manager模組。

更新說明

若要取得 Configuration Manager PowerShell 模組的最新資訊,請使用Update-Help Cmdlet。 此內容與 Microsoft Learn 上針對 ConfigurationManager 模組發佈的內容相同。

重要事項

由於可更新內容在 2103 版發行時的結構和發佈方式有所變更,因此請勿在 2010 版網站上使用 Update-Help 。 將網站更新為 2103 版或更新版本,然後更新本機說明內容。

如需詳細資訊,請參閱 PowerShell 2103 版版本資訊

您執行此 Cmdlet 的電腦需要網際網路存取,特別是 pshelpprod.blob.core.windows.net 。 然後從提升許可權的 PowerShell 會話執行下列命令:

Update-Help -Module ConfigurationManager

更新Configuration Manager Cmdlet 說明之後,您可以使用Get-Help Cmdlet 來取得 Cmdlet 的相關說明。 例如:

Get-Help Get-CMDevice
Get-Help Get-CMDevice -Examples
Get-Help Get-CMDevice -Parameter *

如需詳細資訊,請參閱下列 PowerShell 部落格文章: 您已取得說明!

通用參數

所有Configuration Manager Cmdlet 都支援常見的 PowerShell 參數:

  • 偵錯
  • ErrorAction
  • ErrorVariable
  • InformationAction
  • InformationVariable
  • OutVariable
  • OutBuffer
  • PipelineVariable
  • 詳細資訊
  • WarningAction
  • WarningVariable

如需詳細資訊,請參閱 about_CommonParameters

支援 PowerShell 第 7 版

Configuration Manager PowerShell Cmdlet 程式庫支援 PowerShell 第 7 版。 如需 PowerShell 7 的詳細資訊,包括如何下載和安裝它的指示,請參閱 在 Windows 上安裝 PowerShell

提示

PowerShell 7 會以 執行。 pwsh.exe 舊版 PowerShell 會以 執行。 powershell.exe

不支援 PowerShell 第 7 版的 Cmdlet

下列 Cmdlet 不支援 PowerShell 7:

  • Import-CMPackage
  • Import-CMDriverPackage
  • Import-CMTaskSequence
  • Export-CMPackage
  • Export-CMDriverPackage
  • Export-CMTaskSequence
  • Add-CMReportingServicePoint
  • Get-CMReportingServicePoint
  • Remove-CMReportingServicePoint
  • Set-CMReportingServicePoint

它們需要.NET Framework,而不是與 PowerShell 第 7 版搭配使用的 .NET Core。

從 2103 版開始,如果您嘗試在 PowerShell 第 7 版會話中使用這些 Cmdlet,它們會失敗並出現下列錯誤: This cmdlet only supports the ".NET Framework" runtime.

PowerShell 第 7 版的已知問題

  • 您無法直接從 Configuration Manager 主控台啟動 PowerShell 7。 手動啟動 PowerShell 7,然後匯入Configuration Manager模組

  • 目前僅支援 Configuration Manager Cmdlet。 依賴 PowerShell 的其他Configuration Manager功能可能不支援第 7 版。 例如,執行 腳本CMPivot或執行 PowerShell 腳本 工作順序步驟。

PowerShell 的意見反應

如果您對 Configuration Manager PowerShell Cmdlet 有意見反應,請在 Configuration Manager 主控台中使用相同的選項來傳送意見反應。 如需詳細資訊,請參閱 產品意見反應

當您傳送苦臉時,請包含下列 PowerShell 特定的其他資訊:

  • 您用來讓 Microsoft 嘗試重現問題的確切腳本或命令語法。

  • 相較于實際行為,您預期的行為。

  • 當您使用 Verbose 通用參數執行時的完整輸出。

  • ConfigurationManager模組的版本和路徑。 例如,包含下列命令的輸出:

    (Get-Module -Name ConfigurationManager).Version
    (Get-Module -Name ConfigurationManager).Path
    
  • 如果 Cmdlet 傳回錯誤,請使用下列命令來取得例外狀況詳細資料:

    $Error[0].Exception | Format-List * -Force
    

預覽版本資訊

核心文件庫中的 Technical Preview 功能文章包含 PowerShell 的版本資訊。 例如,請參閱 Technical Preview 2202 版

後續步驟

如需最新版Configuration Manager變更的詳細資訊,請從目錄中選取最新的版本資訊

如需個別 Cmdlet 的詳細資訊,請參閱 Configuration Manager Cmdlet 參考

如需有關學習及開始使用Windows PowerShell的詳細資訊,請參閱PowerShell 101