Forum Discussion
Problem with my script, dont execute
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?
Nervill_mcbo
What is the status when you run it through the task scheduler?
If I have to guess I would say it could be permissions issue as you are trying to write directly to C: other than that it depends on the environment.
Regards
- Erick A. Moreno R.Mar 06, 2020Iron ContributorHello, you will have the file for down hosts, if there is no file no action needs to be taken.
It's similar to the pop up avoiding the interaction from user. - Nervill_mcboMar 06, 2020Copper ContributorHi Erick,
I only have one doubt if it is me on my machine where this script is running under a scheduled task and there is a down of the services under the mail mode as I would receive it in case the internet goes down for example? There is no other solution similar or similar to popup alert?
Regards, - Erick A. Moreno R.Mar 05, 2020Iron Contributor
Nervill_mcbo Hello.
I've adapted the script to only check for the Network adapter "Ethernet 3" as you are sure that your VPN connection is using that one. that should fix that for you.
For the warning I've added a piece that will send an email whenever there is a host down, please update the script with your desired paths and email server/user data. remember that the first time you have to run the script and provide the credentials that will be saved to an XML file, after that you can use the task scheduler and the script will no ask for credentials as it already has the file.
#Start-Transcript -Path "C:\transcriptPing.txt" -Force #Add-Type -AssemblyName PresentationCore,PresentationFramework $names = @('192.168.50.1','192.168.50.2','8.8.8.8')#Get-content "C:\Servers.txt" # $subname = $null $chequea = "no" $dia = Get-date #$ButtonType = [System.Windows.MessageBoxButton]::OK #$MessageIcon = [System.Windows.MessageBoxImage]::Error #$MessageTitle = "Falla de Alcance por Ping" $ipsV4 = $(Get-NetAdapter -Physical | where{$_.Name -eq 'Ethernet 3'} | Get-NetIPAddress | where{$_.AddressFamily -eq 'IPv4'}).IPAddress Foreach($ipV4_tmp in $ipsV4) #added due that multiple Adapter could be up { $ipV4 = $null [System.Net.IPAddress]$ipV4 = $ipV4_tmp Write-Host "Local Adapter ip" $ipV4.IPAddressToString -ForegroundColor Cyan 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") { $Unreachable = @() $Output += "$dia : " foreach ($name in $names) { if(Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue) { $Output+= "$name ====>> UP || " Write-Host "$name ====>> UP " } else { $Output+= "$name ====>> DOWN || " Write-Host "$name ====>> DOWN " $Unreachable += $name #region Conditions 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 " } #endregion #$MessageBody = "Conexion Perdida con el IP: $name, $subname" #$oReturn=[System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon) }#If }#foreach $Output | Out-file "C:\PingResult.txt" -Append }#If $WorkingDirectory = "C:\" $credPath = Join-Path -path ($WorkingDirectory) -ChildPath Mail.cred $mailCreds = If(Test-Path $credPath) { Import-Clixml $credPath } else #Creates Creds File { $tmpCred = Get-Credential #Provide Mail Creds, you may require a service account $tmpCred | Export-CliXml -Path $credPath } If($Unreachable) { $SMTPServer = "smtp.office365.com" #Change To match your SMTP Server or Relay Connectors $SMTPPort = "587" #Change to match your service port usually 587 or 25 $Unreachable | Out-file "C:\UnreachableHosts.txt" -Force Start-Sleep -Seconds 2 $Attachment = @() $Body = @() $From = "emoreno@request-script.com" #Update with your data $To = @("emoreno@request-script.com","DistributionList@request-script.com") #add emails or Distribution list here $Attachment += "C:\UnreachableHosts.txt" $Subject = "Unreachable Hosts" $Body = "<b>Find Attached Report File</b><br>" #add any information that you consider relevant Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -BodyAsHtml -SmtpServer $SMTPServer ` -port $SMTPPort -UseSsl -Credential $mailCreds -Attachments $Attachment #–DeliveryNotificationOption OnSuccess # Send Email } #Stop-TranscriptRegards
Erick Moreno
- Nervill_mcboMar 05, 2020Copper ContributorHello, With the code that you send me I get the following answer: Local Adapter ip 192.168.0.135, but within my laptop I have a network that is valid to perform the check, I send you for example my ipconfig:
Windows IP Settings
Ethernet adapter Ethernet 2:
Media status. . . . . . . . . . . : disconnected media
DNS suffix specific for the connection. . :
Ethernet adapter Ethernet 3:
DNS suffix specific for the connection. . :
Link: local IPv6 address. . . : fe80 :: 60e9: c265: 2745: 5140% 19
IPv4 address. . . . . . . . . . . . . . : 10.212.134.200
Subnet mask . . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . . :
Ethernet Ethernet adapter:
Media status. . . . . . . . . . . : disconnected media
DNS suffix specific for the connection. . :
Wireless LAN adapter Local area connection * 1:
Media status. . . . . . . . . . . : disconnected media
DNS suffix specific for the connection. . :
Wireless LAN adapter Local area connection * 10:
Media status. . . . . . . . . . . : disconnected media
DNS suffix specific for the connection. . :
VMware Network Adapter VMnet1 Ethernet Adapter:
DNS suffix specific for the connection. . :
Link: local IPv6 address. . . : fe80 :: 1008: 6a5e: fda9: 67c3% 6
IPv4 address. . . . . . . . . . . . . . : 192.168.20.1
Subnet mask . . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . :
VMware Network Adapter VMnet8 Ethernet Adapter:
DNS suffix specific for the connection. . :
Link: local IPv6 address. . . : fe80 :: 1931: 2147: e0f8: dca7% 20
IPv4 address. . . . . . . . . . . . . . : 192.168.111.1
Subnet mask . . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . :
Wi-Fi wireless LAN adapter:
DNS suffix specific for the connection. . : fibertel.com.ar
IPv4 address. . . . . . . . . . . . . . : 192.168.0.135
Subnet mask . . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . : 192.168.0.1
Ethernet adapter Bluetooth network connection:
Media status. . . . . . . . . . . : disconnected media
DNS suffix specific for the connection. . :
VEthernet Ethernet Adapter (Default Switch):
DNS suffix specific for the connection. . :
Link: local IPv6 address. . . : fe80 :: 5508: 6a4: 984b: 899c% 35
IPv4 address. . . . . . . . . . . . . . : 172.17.109.17
Subnet mask . . . . . . . . . . . . : 255.255.255.240
Default Gateway . . . . . :
The following adapter has a valid network:
Ethernet adapter Ethernet 3:
DNS suffix specific for the connection. . :
Link: local IPv6 address. . . : fe80 :: 60e9: c265: 2745: 5140% 19
IPv4 address . . . . . . . . . . . . . : 10.212.134.200
Subnet mask . . . . . . . . . . . : 255.255.255.255
Default Gateway . . . . :
but the code continues seeing only and bringing the result of the network:
Wi-Fi wireless LAN adapter:
DNS suffix specific for the connection. . : fibertel.com.ar
IPv4 address . . . . . . . . . . . . . : 192.168.0.135
Subnet mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . : 192.168.0.1
I am actually connected using a Wi-Fi connection but I connected via vpn to the network that I want to verify, you think if there is a way.
And the other thing can then warn the script when a server is down, since I can't use pop-ups? - Erick A. Moreno R.Feb 21, 2020Iron Contributor
Nervill_mcbo For the error: You have to update the location of the server's file as it is looking for C:\servers.txt
The alternatives; Save a file containing all the IPs that did not respond or send an email notifying about it at the end.
I can help you with that later, just confirm that after modifying the server's file location the script is able to complete using the task scheduler after that we can add functionality to the script as needed.
- Nervill_mcboFeb 21, 2020Copper Contributor
I'm throwing this error with the code you send me
Get-content : No se encuentra la ruta de acceso 'C:\Servers.txt' porque no existe.
En C:\Users\LabsXD-1\Documents\DevOps\checkpingPowershell\CheckPing.ps1: 5 Carácter: 17
+ $names = Get-content "C:\Servers.txt" #@('192.168.50.1','192.1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Servers.txt:String) [Get-Content], ItemNot
FoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommandLocal Adapter ip 192.168.6.32
Out-file : Acceso denegado a la ruta de acceso 'C:\PingResult.txt'.
En C:\Users\LabsXD-1\Documents\DevOps\checkpingPowershell\CheckPing.ps1: 117 Carácter: 14
+ $Output | Out-file "C:\PingResult.txt" -Append
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], UnauthorizedAccessException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand - Nervill_mcboFeb 21, 2020Copper ContributorOk, but what solution could be applied to return an alert identifying me the IP and name of the service dropped in this case? since popus cause a problem with the task.
- Erick A. Moreno R.Feb 21, 2020Iron Contributor
Nervill_mcbo After testing on a different machine I found something that may help.
Please use the arguments -noninteractive -file "Pathtoscript.ps1" don't forget the quotes for your path as you can see in the attached ss. Also, I have removed the popup window as it was preventing the task from completing.#Start-Transcript -Path "C:\transcriptPing.txt" -Force #Add-Type -AssemblyName PresentationCore,PresentationFramework $names = Get-content "C:\Servers.txt" #@('192.168.50.1','192.168.50.2','8.8.8.8')# $subname = $null $chequea = "no" $dia = Get-date #$ButtonType = [System.Windows.MessageBoxButton]::OK #$MessageIcon = [System.Windows.MessageBoxImage]::Error #$MessageTitle = "Falla de Alcance por Ping" $ipsV4 = $(Get-NetAdapter -Physical | where status -eq 'up' | Get-NetIPAddress | where{$_.AddressFamily -eq 'IPv4'}).IPAddress Foreach($ipV4_tmp in $ipsV4) #added due that multiple Adapter could be up { $ipV4 = $null [System.Net.IPAddress]$ipV4 = $ipV4_tmp Write-Host "Local Adapter ip" $ipV4.IPAddressToString -ForegroundColor Cyan 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 " } else { $Output+= "$name ====>> DOWN || " Write-Host "$name ====>> DOWN " #region Conditions 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 " } #endregion #$MessageBody = "Conexion Perdida con el IP: $name, $subname" #$oReturn=[System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon) }#If }#foreach $Output | Out-file "C:\PingResult.txt" -Append }#If #Stop-Transcript - Nervill_mcboFeb 21, 2020Copper ContributorCopy your images and configure it, even though I think that the execution does not happen as I expect, when I check the result in the task it is always different from 0x0
- Erick A. Moreno R.Feb 21, 2020Iron Contributor
- Nervill_mcboFeb 21, 2020Copper ContributorHello, what I am trying to do with the script is to check which of the NICs is in an up state and also contains one of the 3 network segments configured to run so that when detecting some of the IPSs the script is executed
- Erick A. Moreno R.Feb 20, 2020Iron Contributor
Nervill_mcbo It is weird, I have created a task to test it and its working fine, even without the additional argument. I'm attaching my settings in the tasks in case we have something different that could be messing with yours, probably playing with the time when the task first runs can help to clear that error.
For your question about the VPN, If I understood your question correctly you can use the task scheduler option to run it only when a specific connection is available and there you can select the one that corresponds to your VPN. - Nervill_mcboFeb 20, 2020Copper ContributorIt remains the same as the following program: Powershell argument: -NoneInteractive C: \ Users \ LabsXD-1 \ Documents \ checkpingPowershell \ CheckPing.ps1
- Erick A. Moreno R.Feb 20, 2020Iron Contributor
Nervill_mcbo could you try to run it, with the argument -NonInteractive in the task scheduler? and see what is the result.
Regards
Erick
- Nervill_mcboFeb 20, 2020Copper ContributorI change the path to this C: \ Users \ LabsXD-1 \ Documents \ checkpingPowershell
the error in the scheduled task is 0x41303 and it stays in running state and never runs and never ends