Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell 2: Unable to suppress warning message

I am making a cmdlet call to 'set-distributiongroup' in powershell 2. I am simply setting the value of the parameter 'hiddenFromAddressListsEnabled' to a pre-defined boolean value.

However, no matter what I try, it displays a warning message if the boolean assignment is not actually changing the current value of 'hiddenFromAddressListsEnabled'.

Here is the main command I'm invoking:

set-DistributionGroup -identity TestGroup                  `
                      -hiddenFromAddressListsEnabled=$true

Let's semantically define what I have above as 'command'.

Now, I've tried adding several different variants, all with proper line-continuation and syntax. Here are those variants:

command > $null
command 2> $null
command -ErrorAction:silentlycontinue
command -ErrorVariable $throwAway
command -WarningAction:silentlycontinue
command -WarningVariable $throwAway
$var = command

Regardless of various combinations of one or more of the above, I still get a yellow WARNING: message spit to output. Specifically, this:

WARNING: The command completed successfully but no settings of
'<xxxxxx>/Users/TestGroup' have been modified.

Any suggestions on a key concept I'm not understanding? I want the command to not produce this output, and I want it to silently continue if this occurs.

Thanks!!

like image 612
Larold Avatar asked Sep 12 '11 19:09

Larold


2 Answers

I've been trying to suppress the warning messages when stopping a service:

WARNING: Waiting for service 'Service Description' to finish stopping...

The following worked for me:

Stop-Service $svc.Name -WarningAction SilentlyContinue
like image 119
Ralph Willgoss Avatar answered Oct 14 '22 14:10

Ralph Willgoss


If it's just a warning that cause problem why don't you set in your script $WarningPreference variable ?

PS C:\> $WarningPreference='silentlycontinue'
PS C:\> Write-Warning "coucou"
PS C:\> $WarningPreference='continue'
PS C:\> Write-Warning "coucou"
AVERTISSEMENT : coucou
like image 37
JPBlanc Avatar answered Oct 14 '22 13:10

JPBlanc