質問

I'm importing Carbon into my PowerShell script; however when running my script with -Verbose, Carbon also outputs a lot of VERBOSE statements.

Is it possible to Import-Module silently such that I can ignore the verbose statements in the imported module and leave just my own?

役に立ちましたか?

解決

Try Import-Module Carbon -Verbose:$false

他のヒント

I could not get the solutions above to work with all modules (I'm using Powershell 4.0). This is the solution I ended up using and so far it has worked with every module I've used:

At the top of my script file I have this, to make the -Verbose work for the script (the script has no parameters):

[CmdletBinding()]
Param()

Then when I'm ready to import the modules, I do this:

$SaveVerbosePreference = $global:VerbosePreference;
$global:VerbosePreference = 'SilentlyContinue';

Import-module "Whatever";

$global:VerbosePreference = $SaveVerbosePreference;

Then I just call the script like so:

PowerShell -file something.ps1 -Verbose

Import-Module Carbon -Verbose:$false | Out-Null

I think a better solution than the one which is marked here is to redirect the verbose output to a different stream. This way you can print the output if you need it and it doesn't get swollen for ever:

Import-Module Carbon 4>&5

This redirects the verbose stream (4) to the debug stream (5). When you run your script with the Verbose switch, it will not output the verbose lines from Import-Module, but you can bring it back by running your script with the -Debug switch.

As Carbon seems to be a script module, can you try to set the $script:VerbosePreference (or just $VerbosePreference) to 'SilentlyContinue' inside the module itself carbon.psm1. The module scope should do the trick.

First contribution, I hope this helps.


ipmo $dir\$i 3>$null

ipmo: Short-hand/alias for Import-Module

3>$null: Redirect warnings messages to null

Edit: I wanted to share a table I found at work while I was looking for the best way to help explain this... But I cant find it now. However, the quick an dirty is you may have already noticed that ipmo doesn't act like the rest of those PWshell cmdlet. It has a total of 4 streams in the bg.

I don't remember 1 and 2. 3 is warning. 4 though, 4 is not necessarily error, but it is error at the same time? I don't remember the exact wording. It is something that bypasses errors.

If I do find that award winning table again I'll be sure to share the blessing.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top