PowerShell script to find network info for all NICs on a list of Windows servers

### PowerShell script to find for all NICs on a Windows server : IP address, Subnet Mask, Gateway, if DHCP is enabled, DNS servers, WINS servers
# This works on a given list of servers, and collects info for all NICs of server.
###############################################################################

$outputfile = “D:\temp\Networkinfo-4.csv”
$servers = get-content “D:\temp\test2.txt”
$report = @()

foreach ($Computer in $servers)
{
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0)
{
$Networks = $null
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -ea silentlycontinue | ? {$_.IPEnabled}

if($Networks)
{
foreach ($Network in $Networks)
{
$IPAddress = $null
$SubnetMask = $null
$DefaultGateway= $null
$DNSServers = $null
$WINSPrimaryserver = $null
$WINSSecondaryserver = $null
$IsDHCPEnabled = $null

$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway -join ‘,’
$DNSServers = $Network.DNSServerSearchOrder -join ‘,’
$WINSPrimaryserver = $Networks.WINSPrimaryServer
$WINSSecondaryserver = $Networks.WINSSecondaryserver
$IsDHCPEnabled = $false

If($network.DHCPEnabled) { $IsDHCPEnabled = $true }

$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name WINSPrimaryserver -Value $WINSPrimaryserver
$OutputObj | Add-Member -MemberType NoteProperty -Name WINSSecondaryserver -Value $WINSSecondaryserver

$OutputObj
$report += $OutputObj

} # foreach ends here
} # if Networks ends here
else { Write-Warning “Could not find any info on networks/NICs on server $Computer” }

} #if ping ends here
else { Write-Warning “Unable to access/ping $Computer” }

}

$report | Export-Csv $outputfile -NoClobber -NoTypeInformation

###############################################################################

Advertisements

PowerShell script to find all IP addresses assigned on a list of Windows Servers

### PowerShell script to find all IP addresses assigned on a list of Windows Servers.
### List all IP addresses assigned in each server, includes IPv6
###############################################################################

$servers = Get-Content “D:\temp\test2.txt”
$reg=””
foreach ($server in $servers)
{
$reg+=$server+ “`t”+([System.Net.Dns]::GetHostAddresses(“$server”) | foreach {echo $_.IPAddressToString})+”`n”
}
$reg > “D:\temp\ping-IPs-1.csv”

PowerShell script to find FQDN and get ping status of Windows Servers

### PowerShell script to find FQDN and get ping status for given list of windows servers
# Servers which we failed to connect to get any info are collected in to a txt file.
### Writing credits : VM ###
###########################################################################

$servers = Get-Content “D:\temp\test2.txt”

@(
foreach ($name in $servers)
{
if ( Test-Connection -ComputerName “$name.domain.com” -Count 1 -ErrorAction SilentlyContinue ) { Write-output “$name.domain.com is up”}
elseif ( Test-Connection -ComputerName “$name.a.domain.com” -Count 1 -ErrorAction SilentlyContinue ) { Write-output “$name.a.domain.com is up”}
elseif ( Test-Connection -ComputerName “$name.b.domain.com” -Count 1 -ErrorAction SilentlyContinue ) { Write-output “$name.b.domain.com is up”}
elseif ( Test-Connection -ComputerName “$name.c.domain.com” -Count 1 -ErrorAction SilentlyContinue ) { Write-output “$name.c.domain.com is up”}
elseif ( Test-Connection -ComputerName “$name.newdomain.com” -Count 1 -ErrorAction SilentlyContinue ) { Write-output “$name.newdomain.com is up”}
#elseif ( Test-Connection -ComputerName “$name” -Count 1 -ErrorAction SilentlyContinue ) { Write-output “$name is up”}
else
{
Write “$name FQDN not found/ server is not pinging” | out-file -Append “D:\temp\failed-ping-1.txt” -NoClobber
}
}
) | Out-file -FilePath “D:\temp\FQDN-1.csv”

PowerShell script to find a server is physical server or virtual machine

### Powershell script gives Manufacturer and Server Model for a given list of servers, and we can find if its a VM or a physical server.
### eg: for a VM, manufacturer will be “VMware, Inc.”, and model will be “VMware Virtual Platform”
### eg: for a physical server, manufacturer will be “HP” (or others), and model will be “ProLiant BL460c G7”
### Servers which we failed to connect to get any info are collected in to a txt file.
### writing credits : VM ###
#######################################################################################

$servers = Get-Content “D:\temp\test2.txt”
$result = @()
$finalresult = @()

foreach ($computer in $servers)
{
$result = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer | select pscomputername, manufacturer, model
$finalresult+=$result
if (!$result) { Write “No info on server $computer” | out-file -Append “D:\temp\notfound-phy-vm.txt” -NoClobber }
$computer = $null
$result = $null
}

$finalresult | export-csv “D:\temp\VMphys-1.csv” -NoTypeInformation

PowerShell script to find OS name, Service Pack Version and OS-Architecture (32/64 bit)

### PowerShell script to find OS name, Service Pack Version and OS-Architecture (32/64 bit) for given list of servers
# servers which we failed to connect to get any info are collected in to a txt file
### writing credits : VM ###
#######################################################################################

$servers = Get-Content “D:\temp\test2.txt”
$result = @()
$finalresult = @()

foreach ($srv in $servers)
{
$result = Get-WMIObject Win32_OperatingSystem -ComputerName $srv | Select-Object csname, caption, ServicePackMajorVersion, OSArchitecture
$finalresult+=$result
if (!$result) { Write “No info on server $srv” | out-file -Append “D:\temp\notfound.txt” -NoClobber }
$srv = $null
$result = $null
}

$finalresult | export-csv “D:\temp\OS-outputs.csv” -NoTypeInformation

PowerShell script to change the Log mode of Eventvwr -> Security Log to Circular (i.e. OverwriteasNeeded)

### This PowerShell script will change the Log mode of Eventvwr -> Security Log to Circular (i.e. OverwriteasNeeded) for a given list of servers.
# This works for servers with OS 2008R2 and above.
# Servers which we failed to connect or unable to get info are collected in a txt file.
### writing credits : VM ###
############################################################################################################

$servers = Get-Content “D:\temp\test4.txt”
$beforeoutput = “D:\temp\Security-b4-chg.csv”
$outputfile = “D:\temp\Security-log-after-change.csv”
$b4result = @()
$result = @()
$finalresult = @()

foreach ($name in $servers)
{
$log = Get-WinEvent -ListLog Security -ComputerName $name
$b4result += $log | select LogMode, LogName , @{n=”ServerName”;e={$name }}
$log.logmode = “circular”
$log.SaveChanges()

$result = $log | select LogMode, LogName , @{n=”ServerName”;e={$name }}
$finalresult+=$result
if (!$result) { Write “No info from server $name” | out-file -Append “D:\temp\failed-servers.txt” -NoClobber }

$name = $null
$result = $null
$log = $null
}

$b4result | export-csv -NoTypeInformation $beforeoutput
$finalresult | export-csv -NoTypeInformation $outputfile

PowerShell script to find Log mode, and max Log file size of Security Log for each server

#This PowerShell script will give the Log mode and the max log file size of Eventvwr -> Security Log for each server with OS 2008R2 and above.

## Servers which we failed to connect or unable to get info are collected in a txt file.
### writing credits : VM ###
############################################################################################################

$servers = Get-Content “D:\temp\test4.txt”
$result = @()
$finalresult = @()

foreach ($name in $servers)
{
$result = Get-WinEvent -listlog Security -ComputerName $name | select LogMode, LogName , @{n=”ServerName”;e={$name }} # , MaximumSizeinBytes
$finalresult+=$result
if (!$result) { Write “No info on server $name” | out-file -Append “D:\temp\failed-security.txt” -NoClobber }
$srv = $null
$result = $null
}

$finalresult | export-csv “D:\temp\Security-2k8-12-Status.csv” -NoTypeInformation