9

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 }}}
auth private
  • 1,318
  • 1
  • 9
  • 22
MattMoo
  • 192
  • 2
  • 3
  • 11

2 Answers2

5

try is for catching exceptions. You're using the -Quiet switch so Test-Connection returns $true or $false, and doesn't throw an exception when the connection fails.

As an alternative you can do:

if (Test-Connection -computername $computer -Quiet -Count 1) {
    # succeeded do stuff
} else {
    # failed, log or whatever
}
briantist
  • 45,546
  • 6
  • 82
  • 127
  • Yes that worked. Would you know why it only pipes in the last computer name in the my text file? I'm trying get-content "\\test\c$\powershell\computers.txt" | get-computernameChange – MattMoo Sep 17 '15 at 20:17
  • @user3620584 how do you know it's only piping in the last one? – briantist Sep 17 '15 at 20:37
  • As a test I put in 3 computer names that don't exist and it only list the last one in the log file. – MattMoo Sep 17 '15 at 20:58
  • @user3620584 `Out-File` is overwriting the file each time. Use the `-Append` parameter to add onto the end. – briantist Sep 17 '15 at 22:00
2

The Try/Catch block is the better way to go, especially if you plan to use a script in production. The OP's code works, we just need to remove the -Quiet parameter from Test-Connection and trap the error specified. I tested on Win10 in PowerShell 5.1 and it works well.

    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. The last error is $error[0], start by piping it to Get-Member and drill in with dot notation from there.

Don Jones and Jeffery Hicks have a great set of books available that cover everything from the basics to advanced topics like DSC. Reading through these books has given me new direction in my function development efforts.

ChrisS
  • 1,919
  • 2
  • 11
  • 6