The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Leone (1) [Avatar] Offline
#1
Sorry for my disturb smilie((

I'm a little doubt: it's possibile to discard an error messages generated from a CmdLet.

For example in this situation:

if (!(get-variable -Scope Global MiTrovoNelloScope)) {
"Variable `$MiTrovoNelloScope does not exist in the Global Scope !"
"Creating..."
sleep 3
new-variable -Scope Global -name MiTrovoNelloScope -Value "Scope_Global"
}

it's possible to discard the message generated from PowerShell:

Get-Variable : Cannot find a variable with name 'MiTrovoNelloScope'.
At C:leoneMieCorsi2009LaboratorioConWPSEsempio11-Scope-Variabili.ps1:9 char:19
+ if (!(get-variable <<<< -Scope Global MiTrovoNelloScope)) {
+ CategoryInfo : ObjectNotFound: (MiTrovoNelloScope:String) [Get-Variable], ItemNotFoundException
+ FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand

Thank You for All in advance.

Leone
franck (17) [Avatar] Offline
#2
Re: Discard error messages from script
Hi Leone

Is not a problem, you have to set the common parameter '-ea' (Error Action) to silentlycontinue.
After that, you can check with $? if the command was succesfully.

get-variable guguss -ea silentlycontinue
if($?)
{
write-host "the variable guguss exists"
}
else
{
write-host "the variable guguss doesn't exists"
}

Regards
Franck Marchionni