extract ip address from string powershell pattern
$Results = @()
$Hosts = @()
$Server = "Server01"
$LogPath = "C:\logs\$Server\logs\server.log"
#Checking log file
$Lines = Get-Content $LogPath | Where-Object {$_ -match "AUDIT "}
#Remotely
#$Lines = icm -cn $Server {param($LogPath)Get-Content $LogPath | Where-Object {$_ -match "AUDIT "}} -ArgumentList $LogPath
#Getting IP Addresses
Foreach ($Line in $Lines) {
$IP = $Object1 = $null
$IP = ($Line | Select-String -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches).Matches.Value
IF($IP -notmatch "0.0.0.0"){
$Object1 = New-Object PSObject -Property @{
IPAddress = $IP
}
$Results += $Object1
}
}
#Selecting unique IPs
$IPUnique = $Results | Select-Object IPAddress -Unique
#Checking hostname
Foreach ($Item in $IPUnique) {
$HostName = $Object2 = $null
$HostName = (Resolve-DnsName $Item.IPAddress -ErrorAction SilentlyContinue).NAMEHOST
If(!$HostName){$Hostname = "None"}
$Object2 = New-Object PSObject -Property @{
IPAddress = $item.ipaddress
NameHost = $HostName
}
$Hosts += $Object2
}
$Hosts | Out-GridView -Title "Hostnames"
Are there any code examples left?
New code examples in category Shell/Bash
-
Shell/Bash 2022-03-27 22:30:33 wise highlight ubuntu
-
Shell/Bash 2022-03-27 22:25:19 how to send a pull request in git
-
Shell/Bash 2022-03-27 21:55:06 how to open terminal in sublime text ubuntu
-
Shell/Bash 2022-03-27 21:25:19 how to switch branch
-
Shell/Bash 2022-03-27 21:20:25 python
-
Shell/Bash 2022-03-27 21:20:04 set user as admin gitlab
-
Shell/Bash 2022-03-27 21:10:09 apache2 mod_jk proxy to tomcat connector
-
Shell/Bash 2022-03-27 20:35:12 chown
-
Shell/Bash 2022-03-27 20:15:08 awk last match