Forum Discussion

Nervill_mcbo's avatar
Nervill_mcbo
Copper Contributor
Feb 16, 2020

Problem with my script, dont execute

Hello to everyone
I have a script that is not working for me and I do not understand why ?, my script tries to run a ping to selected machines as long as the computer is in a valid network to perform the ping and finally gives me a report in a file, in If a ping fails with a machine, I will display an alert window to take actions, but I really do not understand what happens with the script if you can guide me please, thank you very much.

 

+++ Powershell script++++

Add-Type -AssemblyName PresentationCore,PresentationFramework

$names = Get-content "C:\Servers.txt"
$subname=" "
$chequea="no"
$dia = Get-date
$ButtonType = [System.Windows.MessageBoxButton]::OK
$MessageIcon = [System.Windows.MessageBoxImage]::Error
$MessageTitle = "Falla de Alcance por Ping"

#$ipV4 = Test-Connection -ComputerName (hostname) -Count 1  | Select -ExpandProperty IPV4Address
$ipV4 = (Get-NetAdapter -Physical | where status -eq 'up' | Get-NetIPAddress).IPv4Address 
Write-Host $ipV4.IPAddressToString
#Write-Verbose $ipV4.IPAddressToString

if ($ipV4.GetAddressBytes()[0] -eq "192" -and $ipV4.GetAddressBytes()[1] -eq "168" -and $ipV4.GetAddressBytes()[2] -eq "5" ){
$chequea="si"
}

if ($ipV4.GetAddressBytes()[0] -eq "192" -and $ipV4.GetAddressBytes()[1] -eq "168" -and $ipV4.GetAddressBytes()[2] -eq "6" ){
$chequea="si"
}

if ($ipV4.GetAddressBytes()[0] -eq "10" -and $ipV4.GetAddressBytes()[1] -eq "212" -and $ipV4.GetAddressBytes()[2] -eq "134" ){
$chequea="si"
}

if($chequea -eq "si"){

$Output+="$dia : "
foreach ($name in $names){

 if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
   $Output+= "$name ====>> UP || "
   Write-Host "$name ====>> UP "
   #Write-Verbose "$name ====>> UP "
 }
 else{
    
   $Output+= "$name ====>> DOWN || "
   Write-Host "$name ====>> DOWN "
   #Write-Verbose "$name ====>> DOWN "

   if ($name -eq "8.8.8.8"){
    $subname="DNS Google"
}

if ($name -eq "www.google.com"){
    $subname="Dominio Google"
}

if ($name -eq "192.168.5.1"){
    $subname="Firewall"
}

if ($name -eq "192.168.5.2"){
    $subname="Server 10"
}

if ($name -eq "192.168.5.30"){
    $subname="Server 8"
}

if ($name -eq "192.168.5.29"){
    $subname="Server 9"
}

if ($name -eq "192.168.5.52"){
    $subname="Server 7"
}

if ($name -eq "192.168.6.16"){
    $subname="Server 1"
}

if ($name -eq "192.168.5.94"){
    $subname="Server 2"
}

if ($name -eq "192.168.5.102"){
    $subname="Server 3"
}

if ($name -eq "192.168.5.43"){
    $subname="Server 4"
}

if ($name -eq "192.168.5.124"){
    $subname="Server 5"
}

if ($name -eq "192.168.6.250"){
    $subname="Impresora Wifi"
}

if ($name -eq "192.168.5.250"){
    $subname="Impresora Xerox "
}
   $MessageBody = "Conexion Perdida con el IP: $name$subname"
   $oReturn=[System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
 }
}
$Output | Out-file "C:\PingResult.txt" -Append
}

 

Resources