Test-ModuleManifest
確認模組資訊清單檔案是否正確描述模組的內容。
語法
Test-ModuleManifest [-Path] <string> [<CommonParameters>]
描述
Test-ModuleManifest Cmdlet 會確認模組資訊清單 (.psd1) 檔案所列出的檔案確實存在指定的路徑中。
此 Cmdlet 是專為協助模組作者測試其資訊清單檔案而設計。模組使用者也可以在指令碼和命令中使用這個 Cmdlet,在執行依存於模組的指令碼之前偵測錯誤。
Test-ModuleManifest Cmdlet 會傳回表示模組的物件 (與 Get-Module 傳回的物件屬於相同類型)。如果有檔案不在資訊清單所指定的位置中,此 Cmdlet 也會針對每個遺失的檔案產生一項錯誤。
參數
-Path <string>
指定模組資訊清單檔案的路徑。請輸入副檔名為 .psd1 之模組資訊清單檔案的路徑 (選擇項) 和名稱。預設位置為目前目錄。此參數為必要項。參數名稱 ("Path") 為選擇項。您也可以經由管道將路徑輸出至 Test-ModuleManifest。
必要? |
true |
位置? |
1 |
預設值 |
無 |
接受管線輸入? |
true (ByValue, ByPropertyName) |
接受萬用字元? |
false |
<CommonParameters>
這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.
輸入和輸出
輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。
輸入 |
System.String 您可以經由管道將模組資訊清單的路徑輸出至 Test-ModuleManifest。 |
輸出 |
System.Management.Automation.PSModuleInfo Test-ModuleManifest 會傳回表示模組的 PSModuleInfo 物件。即使資訊清單發生錯誤,它還是會傳回這個物件。 |
範例 1
C:\PS>test-ModuleManifest -path $pshome\Modules\TestModule.psd1
描述
-----------
這個命令會測試 TestModule.psd1 模組資訊清單。
範例 2
C:\PS>"$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 pro
cessed because the file was not found. Please correct the path and try aga
in.
At line:1 char:34
+ "$pshome\Modules\TestModule.psd1" | test-modulemanifest <<<<
+ CategoryInfo : ResourceUnavailable: (C:\Windows\System32\Win
dowsPowerShell\v1.0\Modules\TestModule\TestTypes.ps1xml:String) [Test-Modul
eManifest], FileNotFoundException
+ FullyQualifiedErrorId : Modules_TypeDataFileNotFound,Microsoft.
PowerShell.Commands.TestModuleManifestCommandName
Name : TestModule
Path : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Test
Module\TestModule.psd1
Description :
Guid : 6f0f1387-cd25-4902-b7b4-22cff6aefa7b
Version : 1.0
ModuleBase : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Test
Module
ModuleType : Manifest
PrivateData :
AccessMode : ReadWrite
ExportedAliases : {}
ExportedCmdlets : {}
ExportedFunctions : {}
ExportedVariables : {}
NestedModules : {}
描述
-----------
這個命令會使用管線運算子 (|),將路徑字串傳送到 Test-ModuleManifest。
命令輸出顯示測試失敗,原因是找不到資訊清單中列出的 TestTypes.ps1xml 檔案。
範例 3
C:\PS>function Bool-ModuleManifest ($path)
{$a = dir $path | test-modulemanifest -erroraction SilentlyContinue; $?}
描述
-----------
這個函數與 Test-ModuleManifest 類似,但是前者會傳回布林值,其中 "True" 表示資訊清單通過測試,"False" 則表示未通過。
這個函數會使用 Get-ChildItem Cmdlet (別名為 dir) 取得 $path 變數所指定的模組資訊清單。它會使用管線運算子 (|),將檔案物件傳遞給 Test-ModuleManifest Cmdlet。
Test-ModuleManifest 命令會使用值為 SilentlyContinue 的 ErrorAction 一般參數,隱藏此命令產生的任何錯誤。它也會將 Test-ModuleManifest 傳回的 PSModuleInfo 物件儲存在 $a 變數中,所以這個物件並不會顯示。
接著,在另一個命令中 (分號 [;] 是命令分隔符號),它會顯示 $? 自動變數的值,如果先前的命令沒有產生任何錯誤便會傳回 "True",否則會傳回 "False"。
您可以在條件陳述式中使用這個函數,例如可能放在 Import-Module 命令或使用模組之命令前面的條件陳述式。
請參閱
概念
Import-Module
Get-Module
New-Module
Remove-Module
Export-ModuleMember
New-ModuleManifest
about_Modules