3

我使用 PS Remoting,Powershell 2.0。

我需要调用返回布尔值($true 或 $false)的函数。

我的功能:

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;
    }   
}

注意:WriteTrace 函数只做 Write-Host。

我用这个:

    $ok = TryDisableClientForCredSSP;
    WriteTrace   "[TryDisableClientForCredSSP]. $ok"
    if ($ok -eq $true)
    {
        WriteTrace "[TryDisableClientForCredSSP]. OK true"
    }
    else
    {
        WriteTrace "[TryDisableClientForCredSSP]. KO false"
    }

我得到这个输出:

[TryDisableClientForCredSSP]. Auth     Basic = true     Digest = true     Kerberos = true     Negotiate = true     Certificate = true     CredSSP = false  True
[TryDisableClientForCredSSP]. OK true

我希望这一行输出“[TryDisableClientForCredSSP].True”

 WriteTrace   "[TryDisableClientForCredSSP]. $ok"

有什么建议么 ?

4

1 回答 1

9

函数将每个命令吐出的任何内容返回到输出流。尝试像这样消除该输出:

    [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;
于 2012-09-13T13:46:12.683 回答