- List all server in sheet and save it by name "InputServerIPs.csv". And name column by name "ServerIPs".
example:
ServerIPs
server1
server2
.
.
.
server
- Copy below code and paste in notepad and save by name GetServerIPs.ps1
- Call the script according to environment. ex:
- .\GetServerIPs.ps1 -env env1,
- .\GetServerIPs.ps1 -env env2,
- .\GetServerIPs.ps1 -env production
- Get output in "OutputServerIPs.csv" in location where your "InputServerIPs.csv" is located.
param
(
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)] [System.String] $env
)
$data = Get-Content "C:\Users\sp_farm\InputServerIPs.csv"
$startDate =Get-Date
Write-host $startDate -ForegroundColor Red
$env1Domain = ".env1.net"
$env2Domain = ".env2.net"
$productionDomain = ".production.net"
if($env -eq "env1")
{
foreach($item in $data)
{
if($item -ne "ServerIPs")
{
try
{
$item
$server = $item + $env1Domain
$serverIP = ping $server
$getServerIP =$serverIP.GetValue(2)
$SpiltbyColon =$getServerIP.Split(":")
$splitbyfrom = $SpiltbyColon[0].Split("m")
$result = $item + ":" + $splitbyfrom[1]
}
catch
{
$result = $item + ":" + "Not Found"
}
Write-Host $result -ForegroundColor Blue
write-output $result >> "C:\Users\sp_farm\OutputServerIPs.csv"
}
}
}
elseif($env -eq "env2")
{
foreach($item in $data)
{
if($item -ne "ServerIPs")
{
try
{
$item
$server = $item + $env2Domain
$serverIP = ping $server
$getServerIP =$serverIP.GetValue(2)
$SpiltbyColon =$getServerIP.Split(":")
$splitbyfrom = $SpiltbyColon[0].Split("m")
$result = $item + ":" + $splitbyfrom[1]
}
catch
{
$result = $item + ":" + "Not Found"
}
Write-Host $result -ForegroundColor Blue
write-output $result >> "C:\Users\sp_farm\OutputServerIPs.csv"
}
}
}
else
{
foreach($item in $data)
{
if($item -ne "ServerIPs")
{
try
{
$item
$server = $item + $productionDomain
$serverIP = ping $server
$getServerIP =$serverIP.GetValue(2)
$SpiltbyColon =$getServerIP.Split(":")
$splitbyfrom = $SpiltbyColon[0].Split("m")
$result = $item + ":" + $splitbyfrom[1]
}
catch
{
$result = $item + ":" + "Not Found"
}
Write-Host $result -ForegroundColor Blue
write-output $result >> "C:\Users\sp_farm\OutputServerIPs.csv"
}
}
}
$endDate = Get-Date
Write-host $endDate -ForegroundColor Green