Forum Discussion
Nervill_mcbo
Feb 16, 2020Copper Contributor
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 perfor...
Nervill_mcbo
Feb 19, 2020Copper Contributor
Not working Erick!
Erick A. Moreno R.
Feb 19, 2020Iron Contributor
Nervill_mcbo So, you mean no output at all? No error, no results, right? I'm going to try to run it in my system and brb.
Regards
- Nervill_mcboFeb 19, 2020Copper Contributorwhen I set the variable wanting to get the IP value of the active network, it stopped working completely
- Erick A. Moreno R.Feb 19, 2020Iron Contributor
Nervill_mcbo I've modified your script to fix the errors that it was throwing for the GetAddressBytes() and the way the array of adapters were listed.
It is working on my system, so please let me know how it goes now.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 $ipsV4 = $(Get-NetAdapter -Physical | where status -eq 'up' | Get-NetIPAddress | where{$_.AddressFamily -eq 'IPv4'}).IPAddress #Write-Verbose $ipV4.IPAddressToString Foreach($ipV4_tmp in $ipsV4) #added due that multiple Adapter could be up { $ipV4 = $null [System.Net.IPAddress]$ipV4 = $ipV4_tmp Write-Host $ipV4.IPAddressToString If($ipV4_tmp) { 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" } } Else { Write-Host "Adapter IP item was empty" } } 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) } }#foreach $Output | Out-file "C:\PingResult.txt" -Append }#If
- Nervill_mcboFeb 20, 2020Copper Contributor
Erick A. Moreno R.
It works perfect! I only have one doubt I try to have this script running on the scheduled tasks and it doesn't run, is it not the reason?