I am trying to add the IP address to this script, it would be perfect if I could get this to work. Any help is greatly appreciated.
The input files has host names, and I like to get the IP address into the csv please.
$servers = Get-content "servers.txt"
$collection = $()
foreach ($server in $servers)
{
$status = #{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s);"IP" = what to put here? }
if (Test-Connection $server -Count 1 -ea 0 -Quiet)
{
$status["Results"] = "Up"
}
else
{
$status["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}
$collection | Export-Csv -LiteralPath .\ServerStatus.csv -NoTypeInformation
Using -Quiet would suppress the information you are looking for. Remove the quiet and instead capture the results in a variable you can then query for both success and the ipaddress.
$status = #{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s)}
$result = Test-Connection $server -Count 1 -ErrorAction SilentlyContinue
if ($result)
{
$status.Results = "Up"
$status.IP = ($result.IPV4Address).IPAddressToString
}
else
{
$status.Results = "Down"
$status.IP = "N/A"
}
I am unsure if this logic would produce misleading information but I am playing around with it just in case.
If you have already made the switch to IPv6 then you might be more interested in ($result.IPV6Address).IPAddressToString
Thank you, Matt, you helped me work through my issue.
I found that I wasn't able to use (.) in the new variables. Could be my own misunderstanding. I post this that it may help others with my same issue.
Based on your answer plus an example of how I had to adjust.
$status = #{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s)}
$result = Test-Connection $server -Count 1 -ErrorAction SilentlyContinue
if ($result)
{
$StatusResult = "Up"
$statusIP = ($result.IPV4Address).IPAddressToString
}
Related
I am trying to use the script below to test the trust relationship with the domain controller for every computer in AD. I am using powershell 2.0. When I test the script I get no output. It is based off a powershell 4.0 script that works.
$localCredential = Get-Credential
ForEach ($Name in Get-AdComputer -Filter *){
$output = { $Name = $_.Name }
if (-not (Test-Connection $Name $_.Name -Quiet -Count 1)) { $output.Status = 'Offline'
} else {
$trustStatus = Invoke-Command $Name $_.Name -ScriptBlock { Test-ComputerSecureChannel } -Credential $localCredential
$output.Status = $trustStatus
}
[pscustomobject]$output
}
Below is a powershell 4.0 script that I have tried to convert because the .ForEach syntax is not valid in Powershell 2.0.
Source: https://adamtheautomator.com/trust-relationship-between-this-workstation-and-the-primary-domain-failed/
here is the working script I tried to convert from:
$localCredential = Get-Credential
#(Get-AdComputer -Filter *).foreach({
$output = #{ ComputerName = $_.Name }
if (-not (Test-Connection -ComputerName $_.Name -Quiet -Count 1)) { $output.Status = 'Offline'
} else {
$trustStatus = Invoke-Command -ComputerName $_.Name -ScriptBlock { Test-ComputerSecureChannel } -Credential $localCredential
$output.Status = $trustStatus
}
[pscustomobject]$output
})
Does anyone know why I am not getting an output? Is there something clearly wrong with the first script I posted? Any help would be greatly appreciated.
Thank you very much,
Dave
In the foreach() statement you declare the iterator variable $Name, but inside the loop body you inconsistently use $_ as well.
You're also using [pscustomobject]#{}, a special object allocation syntax that was introduced in PowerShell 3.0 - you need to use New-Object psobject -Property in version 2.0.
Finally, your $output variable needs to be a dictionary and not a scriptblock (notice the # in front of { Name = ... }).
To fix it all:
ForEach ($Computer in Get-AdComputer -Filter *){
$output = #{ Name = $Computer.Name }
if (-not (Test-Connection $Computer.Name -Quiet -Count 1)) {
$output.Status = 'Offline'
} else {
$trustStatus = Invoke-Command -ComputerName $Computer.Name -ScriptBlock { Test-ComputerSecureChannel } -Credential $localCredential
$output.Status = $trustStatus
}
New-Object psobject -Property $output
}
Good Day!
I work with multiple clients and one of my clients have their own toll that displays live server ping stats and this is very helpful during patching activity because of followed reboots etc. I want to design a tool like this or any other tool or means where I can see live ping stats of servers in my Domain. Can I do it using Power Shell by any chance? Is there a way that I could refresh my Power Shell page or the output HTML page which updates the ping stats of respective servers please! I have no clue about programming languages but I want to do this as my pet project and I cannot ask my client about all these details. Help is very much appreciated!
Thanks guys, I have tried below two codes:
param(
[int]$waitseconds = 3
)
while($true) {
$servers = Get-Content .\servers.txt
$collection = $()
foreach ($server in $servers)
{
$status = #{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }
if (Test-Connection $server -Count 1 -ea 0 -Quiet)
{
$status["Results"] = "Up"
}
else
{
$status["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}
$collection | out-gridview; sleep $waitseconds
}
This gives me output in a grid view and it also successfully refreshes the output but the new refreshed output is being presented in a new grid view window. I cannot afford opening numerous windows. So I have tried below code:
param(
[int]$waitseconds = 3
)
while($true) {
$servers = Get-Content .\servers.txt
$collection = $()
foreach ($server in $servers)
{
$status = #{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }
if (Test-Connection $server -Count 1 -ea 0 -Quiet)
{
$status["Results"] = "Up"
}
else
{
$status["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}
Start-Process -FilePath powershell.exe -ArgumentList "-WindowStyle -NoNewWindow -Command &{ $collection | out-gridview; sleep $waitseconds; exit }" -Wait
}
This code is not at all giving gridview output, it simply displays ping stats in shell itself but able to refresh.
I have tried below code too with -NoNewWWindow at the end and this gives output one time and then closes:
param(
[int]$waitseconds = 3
)
while($true) {
$servers = Get-Content .\servers.txt
$collection = $()
foreach ($server in $servers)
{
$status = #{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }
if (Test-Connection $server -Count 1 -ea 0 -Quiet)
{
$status["Results"] = "Up"
}
else
{
$status["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}
Start-Process -FilePath powershell.exe -ArgumentList "-WindowStyle -Command &{ $collection | out-gridview; sleep $waitseconds; exit }" -Wait -NoNewWindow
}
Help me with any modifications to my 1st code so that the grid view does not open for every refresh please!
Have you tried something like this:
$collection | out-gridview
sleep $waitseconds
(Get-Process -id $PID).CloseMainWindow()
I was wondering how I could return the Resolve-DnsName output from my Test-Connection script and add it to the CSV I created.
I like to capture the Name, Type, TTL, Section from that please.
Only invoke the Resolve-DnsName when the ping is not successful.
$servers = Get-Content "servers.txt"
$collection = $()
foreach ($server in $servers)
{
$status = #{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }
$result = Test-Connection $server -Count 1 -ErrorAction SilentlyContinue
if ($result)
{
$status.Results = "Up"
$status.IP = ($result.IPV4Address).IPAddressToString
}
else
{
$status.Results = "Down"
$status.IP = "N/A"
$status.DNS = if (-not(Resolve-DnsName -Name $server -ErrorAction SilentlyContinue))
{
Write-Output -Verbose "$server -- Not Resolving"
}
else
{
"$server resolving"
}
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}
$collection | Export-Csv -LiteralPath .\ServerStatus3.csv -NoTypeInformation
but nothing new is added to the CSV.
You ran into a PowerShell gotcha. PowerShell determines the columns displayed in tabular/CSV output from the first object processed. If that object doesn't have a property DNS that column won't be shown in the output, even if other objects in the list do have it. If other objects don't have properties that were present in the first object they will be displayed as empty values.
Demonstration:
PS C:\> $a = (New-Object -Type PSObject -Property #{'a'=1; 'b'=2}),
>> (New-Object -Type PSObject -Property #{'a'=3; 'b'=4; 'c'=5}),
>> (New-Object -Type PSObject -Property #{'b'=6; 'c'=7})
>>
PS C:\> $a | Format-Table -AutoSize
a b
- -
1 2
3 4
6
PS C:\> $a[1..2] | Format-Table -AutoSize
c b a
- - -
5 4 3
7 6
If you want to generate tabular output always create your objects uniformly with the same set of properties. Choosing sensible defaults even allows you to reduce your total codebase.
$collection = foreach ($server in $servers) {
$status = New-Object -Type PSObject -Property #{
'ServerName' = $server
'TimeStamp' = Get-Date -f s
'Results' = 'Down'
'IP' = 'N/A'
'HasDNS' = [bool](Resolve-DnsName -Name $server -EA SilentlyContinue)
}
$result = Test-Connection $server -Count 1 -EA SilentlyContinue
if ($result) {
$status.Results = 'Up'
$status.IP = ($result.IPV4Address).IPAddressToString
}
$status
}
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)
...
..
.
Two issues,
I want a constaint ping to the two servers, and output to a .csv file.
The script below only runs twice and the output doesn't work. I'm a power newbie so please go easy.
$servers = "server1","server2"
$collection = $()
foreach ($server in $servers)
{
$status = #{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }
if (Test-Connection $server -Count 1 -ea 0 -Quiet)
{
$status["Results"] = "Up"
}
else
{
$status["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}
$collection | Export-Csv -LiteralPath .\ServerStatus.csv -NoTypeInformation
The output works fine. Just run the script and then Invoke-Item ServerStatus.csv
If you want it to run forever just wrap the whole thing in a while loop:
$servers = "server1","server2"
$collection = $()
while(1) {
foreach ($server in $servers)
{
...
}
$collection | Export-Csv -LiteralPath .\ServerStatus.csv -NoTypeInformation
}