【问题标题】:How to hide output from net use in Powershell如何在 Powershell 中隐藏网络使用的输出
【发布时间】:2013-05-24 20:49:28
【问题描述】:

我正在使用这个 Powershell 脚本来连接网络路径:

$usrLogin = [Environment]::UserName + "@mydomain.net"
$cred = Get-Credential $usrLogin
$networkCred = $cred.GetNetworkCredential()
$usercred = $networkCred.UserName + '@' + $networkCred.Domain
$extractSource = "\\srv01\d$\"
net use $extractSource $networkCred.Password /USER:$usercred

我决定使用“net use”,因为稍后我将在脚本中打开一个直接打开网络路径的 FileDialog。

$openFileDialog1 = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog1.initialDirectory = "\\srv01\d$\"

现在我的问题:

如何隐藏“net use”命令的输出?我试过了,但没有用:

net use $extractSource $networkCred.Password /USER:$usercred | out-null

【问题讨论】:

    标签: networking powershell output net-use


    【解决方案1】:

    您可以将错误流重定向到null:

    net use $extractSource $networkCred.Password /USER:$usercred 2>null
    

    或者使用 2>&1 将错误流重定向到标准输出流并且都重定向到 null:

    net use $extractSource $networkCred.Password /USER:$usercred 2>&1>null
    

    【讨论】:

    • 非常感谢,这正是我想要的!我一直认为我需要使用管道而不是“>”:-)
    • 你能解释一下 2>&1>null 在做什么吗?重定向号码如何处理错误?
    • 抱歉回复晚了。这些数字代表控制台流,2 是错误流。您可以在 about_Redirection 帮助主题中找到更多信息
    【解决方案2】:

    当我运行上面的行时(管道到out-null)。如果它仍然不适合您,请尝试以下操作:

    (net use $extractSource $networkCred.Password /USER:$usercred) | out-null 
    

    或像这样存储在 $null 中:

    $null = net use $extractSource $networkCred.Password /USER:$usercred 
    

    【讨论】:

    • 不幸的是,这两个选项对我都不起作用......它只隐藏来自“net use”命令的成功消息,而不是错误警告。例如,当我两次运行该命令时,即使我通过管道将其输出为空值,我仍然会收到这样的错误:net.exe:发生系统错误 1219。在 line:133 char:16 + $null = net
    • 这是您应该在问题中指定的内容。要处理错误输出,请参阅 Shay 的回答。
    • 抱歉这个不确定的问题...谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2011-07-12
    • 1970-01-01
    相关资源
    最近更新 更多