【问题标题】:Getting Write-Verbose messages output to show up in PowerShell module script code获取 Write-Verbose 消息输出以显示在 PowerShell 模块脚本代码中
【发布时间】:2018-10-16 05:22:55
【问题描述】:

我用 Plaster 制作了一个 powershell 模块。我有写详细的功能。那些工作很棒。例如:

function Get-Foo {
    [CmdletBinding()]
    [OutputType([string])]
    param()
    Write-Verbose "Writing foo"
    "foo"
}
Get-Foo -Verbose
VERBOSE: Writing foo
foo

但是,当我执行 Import-Module -Verbose -Force 时,我有一些带有 Write-Verbose 的启动代码不会打印到屏幕上

[string] $TenantId = $null

try {
    $tenantInfo = Get-AzureADTenantDetail
    $TenantId = $tenantInfo.ObjectId
    Write-Verbose "Found existing connection AzureAd connection to tenant $($TenantId) ($($tenantInfo.DisplayName))"
} catch  [Microsoft.Open.Azure.AD.CommonLibrary.AadNeedAuthenticationException] {
    Write-Verbose "No Existing Azure Ad connection found"
}

>Import-Module .\Foo.psd1 -Verbose -Force
VERBOSE: Loading module from path 'C:\Users\zippy\Source\Repos\psfoo\Foo.psd1'.
VERBOSE: Removing the imported "Get-AccessToken" function.
VERBOSE: Loading 'Assembly' from path 'C:\Users\zippy\Source\Repos\psfoo\Microsoft.Open.Azure.AD.CommonLibrary'.
VERBOSE: Loading 'Assembly' from path 'C:\Users\zippy\Source\Repos\psfoo\Microsoft.Open.Azure.AD.CommonLibrary'.
VERBOSE: Loading module from path 'C:\Users\zippy\Source\Repos\psfoo\Foo.psm1'.
VERBOSE: Importing function 'Get-AccessToken'.

我什至尝试将[CmdletBinding()]param() 添加到 psm1 的顶部,这不会破坏我的脚本,但也不会使详细消息可见。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    因此,在混合的某个地方,Import-Module 正在改变您的 VerbosePreference。如果您执行此类操作,您应该会看到您的消息。

    $VerbosePreference = "Continue"
    Write-Verbose "Your Verbose Messages Here"
    $VerbosePreference = "SilentlyContinue"
    

    您可以将其设置为顶部的继续,并在 psm1 文件的底部将其静音。

    【讨论】:

      猜你喜欢
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2021-12-14
      • 2021-08-16
      • 1970-01-01
      相关资源
      最近更新 更多