Functions Returns Boolean in PowerShell
Functions return whatever each command spits out to the output stream. Try eliminating that output like so:
[void]WriteTrace "[TryDisableClientForCredSSP]. Disable-WSManCredSSP -Role Client "
$script=Convert-StringToScriptBlock("Disable-WSManCredSSP -Role Client ")
[void]Caller($script)
[void]WriteTrace "[TryDisableClientForCredSSP]. winrm get winrm/config/client/auth [($env:COMPUTERNAME)]"
$script=Convert-StringToScriptBlock("winrm get winrm/config/client/auth")
[void]Caller($script);
return $true;
Kiquenet
Should "Hi", "Thanks" and taglines and salutations be removed from posts? http://meta.stackexchange.com/questions/2950/should-hi-thanks-and-taglines-and-salutations-be-removed-from-posts What have you tried? http://meta.stackexchange.com/questions/122986/is-it-ok-to-leave-what-have-you-tried-comments Asking http://stackoverflow.com/help/asking Answer http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers http://www.enriquepradosvaliente.com http://kiquenet.wordpress.com ◣◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤ ◥◢◤◢◤◣◥◢◤◢◤◣◥◢◤◢ .NET developer and fan of continuous self-improvement and good patterns and practices. Stuff I am interested in: .NET technology stack in general, C#, Powershell and Javascript in particular as languages Test driven development, DI, IoC and mocking frameworks Data access with ORMs and SQL ASP.NET javascript, jQuery and related frontend frameworks Open source projects
Updated on September 14, 2020Comments
-
Kiquenet over 3 years
I use PS Remoting , Powershell 2.0.
I need call to Functions that returns boolean value ($true or $false).
My function:
Function TryDisableClientForCredSSP() { try { WriteTrace "[TryDisableClientForCredSSP]. Disable-WSManCredSSP -Role Client " $script=Convert-StringToScriptBlock("Disable-WSManCredSSP -Role Client ") Caller($script) WriteTrace "[TryDisableClientForCredSSP]. winrm get winrm/config/client/auth [($env:COMPUTERNAME)]" $script=Convert-StringToScriptBlock("winrm get winrm/config/client/auth") Caller($script); return $true; } catch { Write-Verbose "[TryDisableClientForCredSSP] Error " Write-Verbose $_ Write-Host $_.Exception.Message`r`n return $false; } }
note: WriteTrace function just only do Write-Host.
I use it:
$ok = TryDisableClientForCredSSP; WriteTrace "[TryDisableClientForCredSSP]. $ok" if ($ok -eq $true) { WriteTrace "[TryDisableClientForCredSSP]. OK true" } else { WriteTrace "[TryDisableClientForCredSSP]. KO false" }
I get this output:
[TryDisableClientForCredSSP]. Auth Basic = true Digest = true Kerberos = true Negotiate = true Certificate = true CredSSP = false True [TryDisableClientForCredSSP]. OK true
I want that this line outputs "[TryDisableClientForCredSSP]. True"
WriteTrace "[TryDisableClientForCredSSP]. $ok"
Any suggestions ?
-
Mike Shepard over 11 yearsAnd if you don't like using [void], you can add
| out-null
to those lines instead. -
Victor Zakharov over 11 yearsIsn't there a more simple way to stop output, such as using a semicolon after a command?
-
Keith Hill over 11 yearsThere are three ways: [void]..., ... | Out-Null and ... > $null. That's it.
-
Kiquenet over 10 yearsAny sample for three ways in your answer it would be great, I think.