Test-ModuleManifest

確認模組指令清單檔案正確描述模組的內容。

語法

PowerShell
Test-ModuleManifest
    [-Path] <String>
    [<CommonParameters>]

Description

Test-ModuleManifest Cmdlet 會確認模組指令清單 (.psd1) 檔案中所列的檔案實際上位於指定的路徑中。

此 Cmdlet 旨在協助模組作者測試其指令清單檔案。 模組使用者也可以在腳本和命令中使用此 Cmdlet 來偵測錯誤,再執行相依於模組的腳本。

Test-ModuleManifest 會傳回代表模組的物件。 這是 Get-Module 傳回的相同物件類型。 如果指令清單中指定的位置中沒有任何檔案,Cmdlet 也會為每個遺漏的檔案產生錯誤。

範例

範例 1:測試指令清單

PowerShell
Test-ModuleManifest -Path "$pshome\Modules\TestModule.psd1"

此命令會測試 TestModule.psd1 模組指令清單。

範例 2:使用管線測試指令清單

PowerShell
"$pshome\Modules\TestModule.psd1" | test-modulemanifest

Test-ModuleManifest : The specified type data file 'C:\Windows\System32\Wi
ndowsPowerShell\v1.0\Modules\TestModule\TestTypes.ps1xml' could not be processed because the file was not found. Please correct the path and try again.
At line:1 char:34
+ "$pshome\Modules\TestModule.psd1" | test-modulemanifest <<<<
+ CategoryInfo          : ResourceUnavailable: (C:\Windows\System32\WindowsPowerShell\v1.0\Modules\TestModule\TestTypes.ps1xml:String) [Test-ModuleManifest], FileNotFoundException
+ FullyQualifiedErrorId : Modules_TypeDataFileNotFound,Microsoft.PowerShell.Commands.TestModuleManifestCommandName

Name              : TestModule
Path              : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\TestModule\TestModule.psd1
Description       :
Guid              : 6f0f1387-cd25-4902-b7b4-22cff6aefa7b
Version           : 1.0
ModuleBase        : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\TestModule
ModuleType        : Manifest
PrivateData       :
AccessMode        : ReadWrite
ExportedAliases   : {}
ExportedCmdlets   : {}
ExportedFunctions : {}
ExportedVariables : {}
NestedModules     : {}

這個指令會使用管線運算子 (|) 將路徑字串傳送至 Test-ModuleManifest

命令輸出顯示測試失敗,因為找不到指令清單中列出的TestTypes.ps1xml檔案。

範例 3:撰寫函式以測試模組指令清單

PowerShell
function Test-ManifestBool ($path)

{$a = dir $path | Test-ModuleManifest -ErrorAction SilentlyContinue; $?}

此函式就像 Test-ModuleManifest,但它會傳回布爾值。 如果指令清單通過測試,則函式會傳回 $True,否則會傳回 $False

函式會使用 Get-ChildItem Cmdlet 別名 = dir 來取得 $path 變數所指定的模組指令清單。 命令會使用管線運算子 (|) 將檔案物件傳遞至 Test-ModuleManifest

Test-ModuleManifest 使用 ErrorAction common 參數搭配 SilentlyContinue 值來隱藏命令所產生的任何錯誤。 它也會儲存 PSModuleInfo 物件,Test-ModuleManifest$a變數中傳回。 因此,不會顯示物件。

然後,在個別的命令中,函式會顯示自動變數 $? 的值。 如果上一個命令不會產生任何錯誤,則命令會顯示 $True,否則 $False

您可以在條件語句中使用此函式,例如可能位於 Import-Module 命令之前或使用模組的命令。

參數

-Path

指定指令清單檔的路徑和檔名。 輸入具有 .psd1 擴展名之模組指令清單檔的選擇性路徑和名稱。 預設位置是目前目錄。 支援通配符,但必須解析為單一模組指令清單檔案。 這是必要參數。 您也可以使用管線將路徑傳送至 Test-ModuleManifest

類型:String
Position:0
預設值:None
必要:True
接受管線輸入:True
接受萬用字元:True

輸入

String

您可以將路徑傳送至模組指令清單至此 Cmdlet。

輸出

PSModuleInfo

此 Cmdlet 會傳回代表模組 PSModuleInfo 物件。 即使指令清單發生錯誤,也會傳回這個物件。