3

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 ?

1 Answer 1

9

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;
Sign up to request clarification or add additional context in comments.

4 Comments

And if you don't like using [void], you can add | out-null to those lines instead.
Isn't there a more simple way to stop output, such as using a semicolon after a command?
There are three ways: [void]..., ... | Out-Null and ... > $null. That's it.
Any sample for three ways in your answer it would be great, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.