Advertisement
amnich

check-hostnames_and_ip_change

Jun 5th, 2017
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # first create a CSV with headers
  2. # hostname;ip;dateadded;
  3. # and add your first entries
  4. # google.com;173.194.69.103;2017-06-05
  5.  
  6. if ($my_error){
  7.     Clear-Variable my_error
  8. }
  9.  
  10. $hosts_csv_path = "\\PathToCsv\hostAndIp.csv"
  11. $hosts_csv = Import-Csv $hosts_csv_path -Delimiter ";" -ErrorVariable +my_error
  12. $host_list = $hosts_csv | Group-Object -Property hostname -ErrorVariable +my_error
  13.  
  14. $results = @()
  15. foreach ($host_single in $host_list){  
  16.     $hostname = $host_single.Name  
  17.     $host_ips = [System.Net.Dns]::GetHostAddresses("$hostname")
  18.     if ($?){
  19.         $host_ips.ipaddresstostring
  20.         foreach ($Host_ip in $Host_ips){
  21.             if (!($host_single.group.ip -contains $Host_ip.ipaddresstostring)){
  22.                 $line = "" | select hostname, ip, dateadded
  23.                 $line.hostname = $hostname
  24.                 $line.ip = $Host_ip.ipaddresstostring
  25.                 $line.dateadded = Get-Date -Format ("yyyy-MM-dd")
  26.                 $line
  27.                 $results += $line
  28.             }          
  29.         }
  30.     }
  31. }
  32. if ($results){
  33.     Send-MailMessage -SmtpServer SmtpServer -To email -From email -Subject "Hostname ip change" -Body $(($results | foreach {"$($_.ip) $($_.hostname)"}) -join "`n")
  34.     $results | Export-Csv -Append $hosts_csv_path -NoTypeInformation -Encoding utf8 -Delimiter ";" -ErrorVariable +my_error
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement