How to Scan multiple Hostnames to resolve Ip addresses Powershell script - powershell

I am looking for some help with a script that will read a textfile full of server names and resolve the IP address and export this into a csv file. I am using powershell for this and the Test-Connection command. Please see below code - i am getting the error - cmdlet ForEach-Object at command pipeline position 1 Supply values for the following parameters:Process[0]:
(removed my username and swapped with ***)
$array=#()
$computer=Get-Content -Path c:\Users\****\Desktop\ping_IP_host\computers.txt
ForEach ($server in $computer){
if(Test-Connection $server -Quiet)
{
try {
$IP=
[System.net.dns]::GetHostEntry($server).AddressList | %
{$_.IpAddressTostring}
}
catch {"Invalid HostName" - $server}
$obj = New-Object PSObject -Property #{
Hostname=$server
IP=$IP
}
$array += $obj
else {
$IP="Invalid Host"
}
$obj = New-Object PSObject -Property #{
Hostname=$server
IP=$IP
}
$array += $obj
}
}
$array | Export-Csv C:\Users\****\Desktop\ping_IP_host\results.csv

There is a misplace curly bracket, duplicated code, logic error and unwrapped objects.
Indenting properly will help you to find errors and duplicated code.
The custom object you build does contain both IPv4 and IPv6 addresses, so you may want to join the 2 addresses OR just select the IPv4 address - I do not know your requirements.
Edit:
The try..catch block is not needed as it depends on GetHostEntry() getting a result for the DNS lookup of $server, which is already verified by the test-connection. In short, your code defaults to the else clause "Invalid Host" when the host does not exist OR the host does not answer to ICMP.
the try..catch block around $IP = ([System.net.dns]::GetHostEntry($server).AddressList should be located before the Test-Connection because an error means the host name is invalid so no point in trying a ping.
Note: The code below does not take into account the above edit.
Working code:
$array=#()
$computer=Get-Content -Path $env:userprofile\Desktop\ping_IP_host\computers.txt
ForEach ($server in $computer) {
if (Test-Connection $server -Quiet -count 1) {
try {
$IP = ([System.net.dns]::GetHostEntry($server).AddressList | ForEach-Object {$_.IPAddressToString} ) -join ";"
}
catch {"Invalid HostName" - $server}
}
else {
$IP="Invalid Host"
}
$obj = New-Object PSObject -Property #{
Hostname = $server
IP = $IP
}
$array += $obj
}
$array | Export-Csv -NoTypeInformation $env:userprofile\Desktop\ping_IP_host\results.csv
Output:
"IP","Hostname"
"216.58.206.68;2a00:1450:4002:803::2004","www.google.com"
"Invalid Host","jupiter"
"Invalid host","microsoft.com"
Select IPv4 address only
$array=#()
$computer=Get-Content -Path $env:userprofile\computers.txt
ForEach ($server in $computer) {
if (Test-Connection $server -Quiet -count 1) {
try {
$IP = [System.net.dns]::GetHostEntry($server).AddressList | Where-Object {$_.AddressFamily -eq 'InterNetwork'} | ForEach-Object {$_.IPAddressToString}
}
catch {"Invalid HostName" - $server}
}
else {
$IP="Invalid Host"
}
$obj = New-Object PSObject -Property #{
Hostname = $server
IP = $IP
}
$array += $obj
}
$array | Export-Csv -NoTypeInformation $env:userprofile\Desktop\ping_IP_host\results.csv
Output:
"IP","Hostname"
"216.58.206.68","www.google.com"
"Invalid Host","jupiter"
"Invalid host","microsoft.com"

Related

PowerShell create a new object and add the values to an array

What I am trying to achieve here is add the servers and the updates that are not installed on the server to an array and create a new object that is going to display the names of the servers in one column and the missing updates on another column, but at the end I am getting an empty Grid-View table.
The values for the servers and updates are read from a file.
Write-Host
#Read the password from stdin and store it in a variable
$password = Read-Host -AsSecureString -Prompt "Enter your password"
Write-Host
#Get credentials and password for later user
$cred = New-Object System.Management.Automation.PSCredential ("Administrator#testing.local", $password )
#Get the list of available servers to test
$servers = Get-Content -Path $HOME\Desktop\servers.txt
#Get the list of available updates that need to be installed on the server
$available_updates = Get-Content $HOME\Desktop\update.txt
$add_updates = #()
$add_updates_and_servers = #()
#Get each server name from the list and execute the following commands
foreach ($server in $servers) {
#Test if the server is reponding
$ping = Test-Connection $server -Count 1 -Quiet
#If the above command returns True continue
if ($ping -eq "True") {
#Write a message saying Testing server_name
Write-Host "Testing $server"
foreach ($update in $available_updates) {
#Check if update is installed
$updates_from_os = Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock { Get-HotFix | Select-Object -Property HotFixID | Where-Object -Property HotFixID -EQ $Using:update } -HideComputerName | Select-Object -ExpandProperty HotFixID
if (!$updates_from_os) {
$add_updates += $update
}
}
New-Object -TypeName PSObject -Property $updates -OutVariable final
$updates = #{
"Server" = $server
"Updates" = $add_updates
}
}
$add_updates_and_servers += $final
}
$add_updates_and_servers | Out-GridView
For what is probably happening with your script:
I suspect that each time you calling the statement New-Object -TypeName PSObject -Property $updates -OutVariable final You overwriting any previous created $final object which references to the same objects as your $add_updates_and_servers collection.
Anyways, try to avoid using the increase assignment operator (+=) to create a collection, instead stream the results to a variable (or even better, directly to next/final cmdlet: ... }| Out-GridView).
Something like:
$add_updates_and_servers = foreach ($server in $servers) {
$ping = Test-Connection $server -Count 1 -Quiet
if ($ping -eq "True") {
Write-Host "Testing $server"
$add_updates = #(
foreach ($update in $available_updates) {
$updates_from_os = Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock { Get-HotFix | Select-Object -Property HotFixID | Where-Object -Property HotFixID -EQ $Using:update } -HideComputerName | Select-Object -ExpandProperty HotFixID
if (!$updates_from_os) { $update }
}
)
[PSCustomObject]#{
"Server" = $server
"Updates" = $add_updates
}
}
}
Note: in case you want each $update in a separate column, also have a look at: Not all properties displayed

Powershell IIS Audit Scripting

I'm in the early stages of learning powershell, and I'm trying to put together a script that remotely gathers information from our IIS servers, but I'm encountering several issues.
The first one is that the IP Address and OU columns remain empty in the output file.
The second one is that I'm not able to format the Administrator group column to have 1 group per line, or delimited by commas.
This is the current version of the code:
$computers = Get-Content "C:\servers.txt"
#Running the invoke-command on remote machine to run the iisreset
$output = foreach ($computer in $computers)
{
Write-Host "Details from server $computer..."
try{
Invoke-command -ComputerName $computer -ErrorAction Stop -ScriptBlock{
# Ensure to import the WebAdministration and AD module
Import-Module WebAdministration
Import-Module ActiveDirectory
$webapps = Get-WebApplication
$list = #()
foreach ($webapp in get-childitem IIS:\AppPools\)
{
$name = "IIS:\AppPools\" + $webapp.name
$item = #{}
$item.server = $env:computername
$item.WebAppName = $webapp.name
$item.Version = (Get-ItemProperty $name managedRuntimeVersion).Value
$item.State = (Get-WebAppPoolState -Name $webapp.name).Value
$item.UserIdentityType = $webapp.processModel.identityType
$item.Username = $webapp.processModel.userName
$item.Password = $webapp.processModel.password
$item.OU = (Get-ADComputer $_ | select DistinguishedNAme)
$item.IPAddress = (Get-NetIPAddress -AddressFamily IPv4)
$item.Administrators = (Get-LocalGroupMember -Group "Administrators")
$obj = New-Object PSObject -Property $item
$list += $obj
}
$list | select -Property "Server","WebAppName", "Version", "State", "UserIdentityType", "Username", "Password", "OU", "Ip Address", "Administrators"
}
} catch {
Add-Content .\failedservers.txt -Force -Value $computer
}
}
$output | Export-Csv -Path .\output.csv
#Stop-Transcript
I'd really appreciate any input on how to get it to work properly or improve on the code itself.
Thanks in advance!
For the OU property, you'll want to reference $env:COMPUTERNAME instead of $_:
$item.OU = (Get-ADComputer $env:COMPUTERNAME |Select -Expand DistinguishedName)
For the IPAddress and Administrators fields you'll want to use -join to create comma-separated lists of the relevant values:
$item.IPAddress = (Get-NetIPAddress -AddressFamily IPv4).IPAddress -join ','
$item.Administrators = (Get-LocalGroupMember -Group "Administrators").Name -join ','

Powershell get IPs from hosts programmatically

I have a list of hosts from my LAN (over 1000) that was extracted in Excel format from previous guy that worked here before I started my employment. I need to know the IP's of each host.
I would need some automatic extract (I hope there should be a smarter method than to ping manually all hosts in order to get the IP). I came across SO and read this question. Using PowerShell means a good step toward automation of what I need.
However, at what I am stuck is how to automate this command (in PowerShell) in order to use the excel file as input (or text) and provide an output file that contains the relevant IPs?
Update: a code I have in mind:
$servers = get-content "C:\scr\Servers.txt"
foreach ($Server in $Servers)
{
$Addresses = $null
try {
$Addresses = [System.Net.Dns]::GetHostAddresses("$Server").IPAddressToString
}
catch {
$Addresses = "Server IP cannot resolve."
}
foreach($Address in $addresses) {
write-host $Server, $Address
}
}
The Excel file used as input contains line by line the hostnames in a single column. This Excel file could be easily converted to text format (which is what I already done by now, resulting the Servers.txt file).
The problem with this code is that it displays the output only in the Powershell window, no file.
$output = #()
$servers = get-content "C:\scr\Servers.txt"
foreach ($Server in $Servers)
{
$Addresses = $null
try {
$Addresses = ((Test-Connection $Server | select -skip 3).IPV4Address).IPAddressToString
}
catch {
$Addresses = "Server IP cannot resolve."
}
$output += New-object psobject -property #{
servername = $server
Addresses = $Addresses
}
}
$output | select servername, Addresses | export-csv ".\Addresslist.csv" -NoTypeInformation
Finally, this code has worked successfuly, after tweakening (and also displays in the Powershell windows its outcome - those IPs - during script's execution). At the end, the results are exported in the Addresslist.csv file.
$output = #()
$servers = get-content "C:\scr\Servers.txt"
foreach ($Server in $Servers)
{
$Addresses = $null
try {
$Addresses = [System.Net.Dns]::GetHostAddresses("$Server")
}
catch {
$Addresses = "Server IP cannot resolve."
}
$output += New-object psobject -property #{
servername = $server
Addresses = $Address
}
foreach($Address in $addresses) {
write-host $Server, $Address
}
}
$output | select servername, Addresses | export-csv ".\Addresslist.csv" -NoTypeInformation

hashtable filter / select

I was working tonight to re-write an existing server health check script to store its values in a hashtable, and that part is working fine. However, I want the results to go to a CSV file, and that file only to be populated with servers where I've tagged them as requiring action. Currently those are generating event ID 7011, or failing a ping test by Test-Connection.
Here's the code:
$CheckServer = #{}
$Servers = (Get-Content $Dir\Test.txt)
foreach ($Server in $Servers) {
$CheckServer.EventID7011 = Get-Eventlog -LogName System -ComputerName $Server -Newest 1 |
Where-Object {$_.EventId -eq 7011} | select Message
if ($CheckServer.EventID -ne $Null) {
$CheckServer.Server = "$Server"
$CheckServer.ActionReq = "Yes"
}
$CheckServer.Ping = Test-Connection -ComputerName $Server -Count 1 -Quiet
if (! $CheckServer.Ping) {
$CheckServer.Server = "$Server"
$CheckServer.ActionReq ="Yes"
$CheckServer.Ping = "Offline"
} else {
$CheckServer.Server = "$Server"
$CheckServer.ActionReq = "No"
$CheckServer.Ping = "Online"
}
New-Object -TypeName PSObject -Property $CheckServer |
Export-Csv "ScanResults.csv" -NoTypeInformation -Append
}
I need the correct code at the end, as it stands, the script works fine for collecting/storing the data in the hashtable array $CheckServer, but I'd like to only select those servers that require action. So, if I'm scanning 100 servers, and 2 of them are in a ping fail state, I want only those selected and sent to Export-Csv.
If you want only servers that don't respond to Test-Connection in the output anyway it would be much simpler to just use a Where-Object filter on the server list:
Get-Content "$Dir\Test.txt" |
Where-Object { -not (Test-Connection -Computer $_ -Count 1 -Quiet) } |
Select-Object #{n='Server';e={$_}}, #{n='ActionReq';e={'Yes'}},
#{n='Ping';e={'Offline'}} |
Export-Csv 'ScanResults.csv' -NoType -Append
You need to store the objects into a list before you can filter and export them. See the lines with comments in your code:
$CheckServer = #{}
$serverObjects = #() # create a list of server objects
$Servers = (get-content $Dir\Test.txt)
ForEach ($Server in $Servers) {
$CheckServer.EventID7011 = get-eventlog -LogName System -ComputerName
$Server -newest 1 | where-object {$_.eventID -eq 7011} |select message
If ($CheckServer.EventID -ne $Null) {
$CheckServer.Server="$Server"
$CheckServer.ActionReq = "Yes"}
$CheckServer.Ping = Test-Connection -ComputerName $Server -count 1 -quiet
if (! $CheckServer.Ping) {
$CheckServer.Server="$Server"
$CheckServer.ActionReq ="Yes"
$CheckServer.Ping= "Offline"}
Else {
$CheckServer.Server="$Server"
$CheckServer.ActionReq ="No"
$CheckServer.Ping= "Online"}
# Add the server object to the list
$serverObjects += New-Object -TypeName PSObject -Property $CheckServer
}
}
# now filter it:
$serverObjects | where ActionReq -eq "Yes" | Export-Csv -Path "...."

test-connection output to csv file with hostname and IPaddress

Currently i have the below script to import from a csv file with a few columns. the ones i am interested in are Hostname and Ipaddress.
Ipaddress = ip address of servers
Hostname - hostnames of servers
basically what i am aiming to do is an audit of currently active servers in our CMDB (lot of old junk that hasn't been properly removed)
$servers = import-csv 'config-network devicetest.csv'
$collection = $()
foreach ( $IPAddress in $servers)
{
$status = #{ "ServerName" = $Hostname; "TimeStamp" = (Get-Date -f s) }
if (Test-Connection -IPAddress $IPAddress -Count 2 -ea 0 -quiet)
{
$status["Results"] = "Active"
}
else
{
$status["Results"] = "Inactive"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}
$collection | Export-Csv .\ServerStatus.csv -NoTypeInformation
My goal is to run test-connection on each Ip address and output to a file that has timestamp/results/servername (being Hostname).
currently i am always getting the result "inactive" and i think this is due to me not calling the proper fields from the csv.
i'm fairly new to importing from csv so any help with how to properly import and export specific columns would be very helpful.
I've done a bit of a search on the web but nothing that really explains it clearly or in this context.
Thank you in advance.
Try to change these lines:
$status = #{ "ServerName" = $IPAddress.Hostname; "TimeStamp" = (Get-Date -f s); "Results"="" }
...
....
(Test-Connection -IPAddress $IPAddress.ipaddress -Count 2 -ea 0 -quiet)
...
..
.