繁体   English   中英

如何从发布模块中捕获异常?

[英]How can I catch an exception from Publish-Module?

我正在使用Publish-Module,其中一个模块的psd1文件错误。 PowerShell发生了预期的异常。 对Publish-Module的调用位于try块内,但是catch块中的错误处理代码从未运行过。 似乎未捕获此错误。

在存储库中已经存在我正在发布的模块的同一PowerShell代码中,还会发生另一个错误。 发生该错误时,catch块中的代码将运行并处理异常。 第一个异常是否会导致catch块被绕过而有所不同?

程式码片段:

    try {

        Publish-Module -Path .\$moduleName -Repository MyRepo -NuGetApiKey ghehdue

        "Module $moduleName published."

    }
    catch {

        if ($_.Exception.Message -ilike "*cannot be published as the current version*is already available in the repository*") {

            "The latest version of module $moduleName already exists in the repository."

        }
        else {
            $exitCode += 1
            Write-Error $_

        }
    }
}

未捕获的错误

Microsoft.PowerShell.Core\Test-ModuleManifest : The module manifest 'J:\Builds\
Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Build\MyCompany.B
uild.psd1' could not be processed because it is not a valid Windows PowerShell 
restricted language file. Remove the elements that are not permitted by the 
restricted language:
At J:\Builds\Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Bui
ld\MyCompany.Build.psd1:13 char:9
+ GUID = 'ccaa548f-8194-4cfa-a659-260f6ddc556b'
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'ccaa548f-8194-4cfa-a659-260f6ddc556b'
# Author of this module
Author = 'MyCompany'
# Company or vendor of this module
CompanyName = 'MyCompany' in expression or statement.
At J:\Builds\Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Bui
ld\MyCompany.Build.psd1:13 char:9
+ GUID = 'ccaa548f-8194-4cfa-a659-260f6ddc556b'
+         ~
The hash literal was incomplete.
At J:\Builds\Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Bui
ld\MyCompany.Build.psd1:19 char:25
+ CompanyName = 'MyCompany, Inc.'
+                         ~
Missing argument in parameter list.
At J:\Builds\Jenkins\PROJECT_2456764.0\Applications\ALM\PSModules\MyCompany.Bui
ld\MyCompany.Build.psd1:118 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
At C:\Program Files 
(x86)\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:989 char:27
+ ...   $module = Microsoft.PowerShell.Core\Test-ModuleManifest -Path $mani ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (J:\Builds\Jenki...Quip.Bui 
   ld.psd1:String) [Test-ModuleManifest], MissingMemberException
    + FullyQualifiedErrorId : Modules_InvalidManifest,Microsoft.PowerShell.Com 
   mands.TestModuleManifestCommand

发现的错误

publish-module : The module 'DqCryptography' with version '1.0.2' cannot be published as the current version '1.0.2' is already available in the repository 'http://usas26:8624/nuget/PROJECTPowerShell/'.
At line:1 char:1
+ publish-module -Path DqCryptography  -Repository PROJECTPowerShell - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Publish-Module], InvalidOperationException
    + FullyQualifiedErrorId : ModuleVersionIsAlreadyAvailableInTheGallery,Publish-Module

在try块内,将错误操作添加到Publish-Module中

Publish-Module -Path .\$moduleName -Repository MyRepo -NuGetApiKey ghehdue -ErrorAction Stop

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM