Test-ModuleManifest
驗證模組資訊清單檔案可精確地說明模組的內容。
Syntax
Test-ModuleManifest
[-Path] <String>
[<CommonParameters>]
Description
Cmdlet Test-ModuleManifest
會確認模組指令清單 .psd1
中所列的檔案 () 檔案實際上是在指定的路徑中。
這個 Cmdlet 是設計來協助模組作者測試其資訊清單檔案。 模組使用者也可以在執行相依於模組的指令碼之前,於指令碼和命令中使用這個 Cmdlet 來偵測錯誤。
Test-ModuleManifest
會傳回代表模組的物件。 這是傳回的相同物件 Get-Module
類型。 若有檔案不在資訊清單中指定的位置,此 Cmdlet 也會針對每個遺失的檔案產生錯誤。
範例
範例 1:測試資訊清單
Test-ModuleManifest -Path "$pshome\Modules\TestModule.psd1"
此命令會測試 TestModule.psd1
模組指令清單。
範例 2︰使用管線測試資訊清單
"$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:撰寫函式以測試模組資訊清單
function Test-ManifestBool ($path)
{$a = dir $path | Test-ModuleManifest -ErrorAction SilentlyContinue; $?}
此函式類似 Test-ModuleManifest
,但會傳回布爾值。 如果指令清單通過測試$False
,則函式會傳回 ,否則會傳回 $True
。
函式會使用 Get-ChildItem Cmdlet 別名 = dir 來取得變數所 $path
指定的模組指令清單。 這個指令會使用管線運算子 (|
) 將檔案物件傳遞至 Test-ModuleManifest
。
Test-ModuleManifest
會使用 ErrorAction 一般參數,其值為 SilentlyContinue,以隱藏命令產生的任何錯誤。 它也會儲存在 $a 變數中傳回的 PSModuleInfo 物件 Test-ModuleManifest
。 因此該物件不會顯示。
然後,在個別的命令中,函式會顯示自動變數的值 $?
。 如果上一個指令不會產生任何錯誤,命令會顯示 ,$False
否則為 $True
。
您可以在條件語句中使用此函式,例如可能位於命令前面的 Import-Module
函式或使用模組的命令。
參數
-Path
指定資訊清單檔案的路徑和檔案名稱。 輸入具有 .psd1
擴展名之模組指令清單檔的選擇性路徑和名稱。 預設位置是目前的目錄。 支援使用萬用字元,但必須解析為單一模組資訊清單檔案。
此為必要參數。 您也可以使用管線會路徑傳送至 Test-ModuleManifest
。
Type: | String |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | True |
輸入
您可以使用管線將模組資訊清單的路徑傳送至此 Cmdlet。
輸出
此 Cmdlet 會傳回代表模組的 PSModuleInfo 物件。 即使資訊清單發生錯誤,它還是會傳回這個物件。