Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # first create a CSV with headers
- # hostname;ip;dateadded;
- # and add your first entries
- # google.com;173.194.69.103;2017-06-05
- if ($my_error){
- Clear-Variable my_error
- }
- $hosts_csv_path = "\\PathToCsv\hostAndIp.csv"
- $hosts_csv = Import-Csv $hosts_csv_path -Delimiter ";" -ErrorVariable +my_error
- $host_list = $hosts_csv | Group-Object -Property hostname -ErrorVariable +my_error
- $results = @()
- foreach ($host_single in $host_list){
- $hostname = $host_single.Name
- $host_ips = [System.Net.Dns]::GetHostAddresses("$hostname")
- if ($?){
- $host_ips.ipaddresstostring
- foreach ($Host_ip in $Host_ips){
- if (!($host_single.group.ip -contains $Host_ip.ipaddresstostring)){
- $line = "" | select hostname, ip, dateadded
- $line.hostname = $hostname
- $line.ip = $Host_ip.ipaddresstostring
- $line.dateadded = Get-Date -Format ("yyyy-MM-dd")
- $line
- $results += $line
- }
- }
- }
- }
- if ($results){
- Send-MailMessage -SmtpServer SmtpServer -To email -From email -Subject "Hostname ip change" -Body $(($results | foreach {"$($_.ip) $($_.hostname)"}) -join "`n")
- $results | Export-Csv -Append $hosts_csv_path -NoTypeInformation -Encoding utf8 -Delimiter ";" -ErrorVariable +my_error
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement