forked from MikeFal/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGather-IPs.ps1
60 lines (49 loc) · 2 KB
/
Gather-IPs.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<#
.SYNOPSIS
Uses a text list of machine names and gathers IP v4 addresses for each, outputting the gathered values to a .csv file.
.DESCRIPTION
This script consumes a basic text list (one machine per line) and uses Test-Connection (PoSH equivalent to ping) to
query the machine's IP address. If the machine exists, the IP is recorded, otherwise a "not found" string is recorded.
The total output is spooled to a .csv document either in the user's My Documents (default) or a specified output directory.
Output file will be Environment_IP_List_<datetimestamp>.csv
Mike Fal (htp://www.mikefal.net) 2013-01-10
.PARAMETER <paramName>
-ServerList (mandatory) - Full path of server list text file
-OutputPath (optional) - Full path where .csv will be outputted. NOTE: Do not declare a filename, filename will be created by script.
.EXAMPLE
.\Gather-IPs.ps1 -ServerList foo.txt
.\Gather-IPs.ps1 -ServerList foo.txt -OutputPath "C:\bar"
#>
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$ServerList,
[Parameter(Mandatory=$false)]
[string]
$OutputPath=([Environment]::GetFolderPath("MyDocuments")))
if((Test-Path $OutputPath) -eq $false)
{
throw "Invalid -OutputPath, please use a valid file path"
}
if(Test-Path $ServerList){
$output=@()
$machines=Get-Content $ServerList
$filename=(Join-Path -Path $OutputPath -ChildPath("Environement_IP_List_"+(Get-Date -Format yyyyMMddHHmm)+".csv"))
foreach($machine in $machines){
$return=New-Object System.Object
if(Test-Connection $machine -Quiet -Count 1){
$ip = (Test-Connection $machine -count 1).IPV4Address.ToString()
}
else{
$ip = "Machine not available"
}
$return | Add-Member -type NoteProperty -Name "Host" -Value $machine
$return | Add-Member -type NoteProperty -Name "IP" -Value $ip
$output+=$return
}
$output | Export-CSV -Path $filename -NoTypeInformation
}
else{
throw "Invalid -ServerList, please use a valid file path"
}