共用方式為


Test-ModuleManifest

驗證模組資訊清單檔案可精確地說明模組的內容。

Syntax

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

Description

Test-ModuleManifest Cmdlet 會驗證模組資訊清單 (.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 相同,但它會傳回布林值。 如果資訊清單通過測試,函式會傳回 $True,否則會傳回 $False。

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

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

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

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

參數

-Path

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

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

輸入

String

您可以使用管線將模組資訊清單的路徑傳送至此 Cmdlet。

輸出

PSModuleInfo

此 Cmdlet 會傳回代表模組的 PSModuleInfo 物件。 即使資訊清單發生錯誤,它還是會傳回這個物件。