获取Write-Verbose消息输出以显示在PowerShell模块脚本代码中

时间:2018-04-02 12:58:12

标签: powershell

我用石膏制作了一个PowerShell模块。我有Write verbose的函数。那些工作很棒。 e.g:

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

但是,我有一些Write-Verbose的启动代码,当我Import-Module -Verbose -Force

时,它不会打印到屏幕上
[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的顶部,这不会破坏我的脚本,但也不会使详细消息可见。

1 个答案:

答案 0 :(得分:0)

因此,在混合中的某个位置,导入模块正在更改您的VerbosePreference。如果您这样做,则应该会看到您的消息。

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

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