简体   繁体   English

Powershell试用/捕获测试连接

[英]Powershell try/catch with test-connection

I'm trying to have offline computers recorded in a text file so that I can run them again at a later time. 我正在尝试将离线计算机记录在文本文件中,以便我可以在以后再次运行它们。 Doesn't seem that it is being recorded or caught in catch. 似乎没有记录或陷入捕获。

function Get-ComputerNameChange {

    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
    [string[]]$computername,
    [string]$logfile = 'C:\PowerShell\offline.txt'
    )




    PROCESS {

        Foreach($computer in $computername) {
        $continue = $true
        try { Test-Connection -computername $computer -Quiet -Count 1 -ErrorAction stop
        } catch [System.Net.NetworkInformation.PingException]
        {
            $continue = $false

            $computer | Out-File $logfile
        }
        }

        if($continue){
        Get-EventLog -LogName System -ComputerName $computer | Where-Object {$_.EventID -eq 6011} | 
        select machinename, Time, EventID, Message }}}

try is for catch ing exceptions. try是为了catch异常。 You're using the -Quiet switch so Test-Connection returns $true or $false , and doesn't throw an exception when the connection fails. 您正在使用-Quiet开关,因此Test-Connection返回$true$false ,并且在连接失败时不会throw异常。

Just do: 做就是了:

if (Test-Connection -computername $computer -Quiet -Count 1) {
    # succeeded do stuff
} else {
    # failed, log or whatever
}

The Try/Catch block is the better way to go, especially if you plan to use a script in production. Try / Catch块是更好的方法,特别是如果您计划在生产中使用脚本。 The OP's code works, we just need to remove the -Quiet parameter from Test-Connection and trap the error specified. OP的代码有效,我们只需要从Test-Connection中删除-Quiet参数并捕获指定的错误。 I tested on Win10 in PowerShell 5.1 and it works well. 我在PowerShell 5.1中测试了Win10,效果很好。

    try {
        Write-Verbose "Testing that $computer is online"
        Test-Connection -ComputerName $computer -Count 1 -ErrorAction Stop | Out-Null
        # any other code steps follow
    catch [System.Net.NetworkInformation.PingException] {
        Write-Warning "The computer $(($computer).ToUpper()) could not be contacted"
    } # try/catch computer online?

I've struggled through these situations in the past. 我过去一直在努力解决这些问题。 If you want to be sure you catch the right error when you need to process for it, inspect the error information that will be held in the $error variable. 如果您想确保在需要处理时遇到正确的错误,请检查将在$ error变量中保存的错误信息。 The last error is $error[0], start by piping it to Get-Member and drill in with dot notation from there. 最后一个错误是$ error [0],首先将它传递给Get-Member并从那里用点符号钻取。

Don Jones and Jeffery Hicks have a great set of books available that cover everything from the basics to advanced topics like DSC. Don Jones和Jeffery Hicks提供了很多书籍,涵盖从基础知识到DSC等高级主题的所有内容。 Reading through these books has given me new direction in my function development efforts. 阅读这些书籍给了我功能开发工作的新方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM