Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How do I write out that a server I'm scanning is not available - Fails my Test Connection Cmdlet

$
0
0

I have this script that reads in a list of computers.  Works great so far...but I want to add an Else statement to my Test-Connection If Statement so if a Computer is not online or I don't have access to it my else statement will write out that the Computer is not available. 

Here is what I have for my script with the Else statement I've added.  Not working quite right though...

#param (
    $inputFile = "C:\Apps\Powershell_Scripts\IP_Mac\Servers.txt"
    $csvFile = "C:\Apps\Powershell_Scripts\IP_Mac\Servers_results.csv"
#    ) 


$report = @()
foreach($Computer in (gc -Path $inputFile)){
get-wmiobject -computer $Computer win32_logicaldisk -filter "drivetype=3" | ForEach-Object {
$device = $_.deviceid
$freespce = ($_.freespace/1GB).tostring("0.00")+"GB"
$Totalspce = ($_.size/1GB).tostring("0.00")+"GB"
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
   $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
   foreach ($Network in $Networks) {
    #$IPAddress  = $Network.IpAddress[0]
    $IPAddress  = $Network.IpAddress
    $SubnetMask  = $Network.IPSubnet[0]
    $DefaultGateway = $Network.DefaultIPGateway
    $DNSServers  = $Network.DNSServerSearchOrder
    }
    $OST = (Get-WmiObject -ComputerName $Computer Win32_OperatingSystem).caption
    $MACAddress  = $Network.MACAddress
    $OutputObj  = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name OS -Value  $OST
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value ($IPAddress -join ",")
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value ($DefaultGateway -join “,”)
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value ($DNSServers -join ",")
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name device -Value $device
    $OutputObj | Add-Member -MemberType NoteProperty -Name freespce -Value $freespce
    $OutputObj | Add-Member -MemberType NoteProperty -Name Totalspce -Value $Totalspce
    #$OutputObj
    $report += $OutputObj
                 }
Else {
    $OutputObj = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name OS -Value  "Not Available"
    $report += $OutputObj }
          }
 }

      Write-Output $report
#}  

$report | Select-Object -Property $properties | Export-Csv -Path $csvFile -NoTypeInformation

How can I adjust this If/Else statement to work properly?

Thank you.


Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>