-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathBackupIPConfig.ps1
201 lines (189 loc) · 9.52 KB
/
BackupIPConfig.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
###########################################################################################################
# #
# File: BackupIPConfig.ps1 #
# #
# Purpose: Script for managing ip configuration on remote hosts via wmi #
# - read configuration from system and export to a csv or xml file #
# - import from a csv or xml file and write configuration to system #
# #
# Author: Sven Sperner <[email protected]> #
# #
# Last edited: 31.05.2012 #
# #
# Requirements: Microsoft Windows PowerShell 2.0 + enabled WMI on remote host #
# #
# Usage: #
# PowerShell -command C:\{Path2Script}\BackupIPConfig.ps1 #
# #
# Parameter: #
# -command {read|write} read & export OR import & write #
# -hosts {<list>|<listFile>} comma separated list or a file with a host in each line #
# -backupFile <filename> filename to save the settings in, without file extension #
# -fileType {csv|xml} file type and extension for backupFile #
# #
# Usage examples: #
# PowerShell -command ".\BackupIPConfig.ps1 read -hosts hostList.txt -backupFile Saved2012 -fileType csv"#
# --> read hosts from hostList.txt and save settings to Saved2012.csv #
# PowerShell -command ".\BackupIPConfig.ps1 read -hosts serverXY,horst0815,reschna007 -fileType xml" #
# --> read settings from the three hosts and save to <stdname>.xml (IPConfigList.xml) #
# PowerShell -command ".\BackupIPConfig.ps1 write -backupFile Saved2012" #
# --> import from "Saved2012.csv" (csv is std) and write to every host where settings are present for #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
# #
###########################################################################################################
PARAM( $command="read",
$hosts="localhost",
$backupFile="IPConfigList",
$fileType="csv",
[Switch]$help )
If( ($help) -OR (($fileType -ne "csv") -AND ($fileType -ne "xml")) )
{
Write-Host "usage: $($MyInvocation.MYCommand) <read|write> <hostslist/-file> <backupfile> <csv|xml> [-help]"
exit
}
# Resolve hostname <=> ip address
Function resolve( [String]$machine="localhost" )
{
[System.Net.IPAddress]$IPobj = $null
If( [System.Net.IPAddress]::tryparse($machine,[ref]$IPobj) -and $machine -eq $IPobj.tostring() )
{
$serv,$null,$null,$name,$null = nslookup $machine 2>$null
$name = $name -replace "Name:\s+",""
return $name
}
Else
{
$null,$serv,$null,$null,$ipadr = nslookup $machine 2>$null
$ipadr = $ipadr -replace "Address:\s+",""
return $ipadr
}
}
# Read ip configuration from host ...
If( $command -eq "read" )
{
If( Test-Path $hosts 2>&1 >$null )
{ $hosts = Get-Content $hosts }
$confList = @()
ForEach( $hostname in $hosts )
{
If( $hostname -eq "localhost" )
{ $hostname = $(hostname) }
Write-Host "Getting ip configuration of $hostname"
$netAdapters = Get-wmiobject -cl "Win32_NetworkAdapter" -comp $hostname -filter `
"AdapterType = 'Ethernet 802.3' AND NetConnectionStatus = 2" #> 0"
ForEach( $adapter in $netAdapters )
{
$adpConf = @($adapter.GetRelated('Win32_NetworkAdapterConfiguration'))[0]
If( ($adpConf.IPEnabled) -and ($adpConf.IPAddress -ne "0.0.0.0") )
{
$confRow = "" | Select Hostname,Resolved,ConfType,AdpId,IPAddress,SubnetMask,Gateway,`
Domain,DNS1,DNS2,DHCP,WINS1,WINS2
$confRow.Hostname = $hostname
[System.Net.IPAddress]$IPobj = $null
If( [System.Net.IPAddress]::tryparse($hostname,[ref]$IPobj) -and $hostname -eq $IPobj.tostring() )
{ $confRow.Resolved = [String]$(resolve("$hostname")) }
Else
{ $confRow.Resolved = [String]$(resolve("$hostname.$($adpConf.DNSDomain)")) }
If( $adpConf.DHCPEnabled )
{ $confRow.ConfType = "dynamic" }
Else
{ $confRow.ConfType = "static" }
$confRow.AdpId = [String]$adapter.DeviceId
$confRow.IPAddress = [String]$adpConf.IPAddress
$confRow.SubnetMask = [String]$adpConf.IPSubnet
$confRow.Gateway = [String]$adpConf.DefaultIPGateway
$confRow.Domain = [String]$adpConf.DNSDomain
If( $adpConf.DNSServerSearchOrder[0] )
{ $confRow.DNS1 = [String]$adpConf.DNSServerSearchOrder[0] }
Else
{ $confRow.DNS1 = [String]$adpConf.DNSServerSearchOrder }
If( $adpConf.DNSServerSearchOrder[1] )
{ $confRow.DNS2 = [String]$adpConf.DNSServerSearchOrder[1] }
Else
{ $confRow.DNS2 = "" }
$confRow.DHCP = [String]$adpConf.DHCPServer
$confRow.WINS1 = [String]$adpConf.WINSPrimaryServer
$confRow.WINS2 = [String]$adpConf.WINSSecondaryServer
$confList += $confRow
}
}
}
# ... and export to csv file
If( $confList.Length -gt 0 )
{
try{
If( $fileType -eq "csv" )
{ $confList | Export-Csv "$backupFile.$fileType" -noTypeInformation -Delimiter ";" }
ElseIf( $fileType -eq "xml" )
{ $confList | Export-Clixml "$backupFile.$fileType" }
Write-Host "IP Configuration List exported to $backupFile.$fileType"
}
catch{
Write-Error "$_`nCannot write to file $backupFile.$fileType"
exit -1
}
}
}
# Import from csv file ...
ElseIf( $command -eq "write" )
{
try{
If( $fileType -eq "csv" )
{ $confList = Import-Csv "$backupFile.$fileType" -Delimiter ";" }
ElseIf( $fileType -eq "xml" )
{ $confList = Import-Clixml "$backupFile.$fileType" }
Write-Host "IP Configuration List imported from $backupFile.$fileType"
}
catch{
Write-Error "$_`nCannot read from file $backupFile.$fileType"
exit -1
}
# ... and write configuration to host
ForEach( $confRow in $confList )
{
Write-Host "Setting ip configuration of $($confRow.Hostname)"
$netAdapters = Get-wmiobject -cl "Win32_NetworkAdapter" -comp $confRow.Hostname -filter `
"AdapterType = 'Ethernet 802.3' AND NetConnectionStatus = 2" #> 0"
ForEach( $adapter in $netAdapters )
{
If( $adapter.DeviceId -eq $confRow.AdpId )
{
$adpConf = @($adapter.GetRelated('Win32_NetworkAdapterConfiguration'))[0]
If( $confRow.ConfType -eq "static" )
{
try{ $adpConf.EnableStatic( $confRow.IPAddress, $confRow.SubnetMask ) > $null }
catch{ Write-Host "Could not write IP Address + Subnet Mask for $($confRow.Hostname)" }
try{ $adpConf.SetGateways( $confRow.Gateway ) > $null }
catch{ Write-Host "Could not write standard Gateway for $($confRow.Hostname)" }
try{ $adpConf.SetDNSDomain( $confRow.Domain ) > $null }
catch{ Write-Host "Could not write DNS Domain for $($confRow.Hostname)" }
try{ $adpConf.SetDNSServerSearchOrder( ($confRow.DNS1,$confRow.DNS2) ) > $null }
catch{ Write-Host "Could not write DNS Server(s) for $($confRow.Hostname)" }
try{ $adpConf.SetWINSServer( $confRow.WINS1, $confRow.WINS2 ) > $null }
catch{ Write-Host "Could not write WINS Server(s) for $($confRow.Hostname)" }
}
ElseIf( $confRow.ConfType -eq "dynamic" )
{
try{ $adpConf.SetWINSServer( $confRow.WINS1, $confRow.WINS2 ) > $null }
catch{ Write-Host "Could not write WINS Server(s) for $($confRow.Hostname)" }
try{ $adpConf.EnableDHCP( ) > $null }
catch{ Write-Host "Could not enable DHCP for $($confRow.Hostname)" }
try{ $adpConf.SetDNSServerSearchOrder( ) > $null }
catch{ Write-Host "Could not write DNS Server(s) for $($confRow.Hostname)" }
}
Else
{
Write-Host "$($confRow.Hostname) is configured $($confRow.ConfType)"
}
}
}
}
}
Else
{
Write-Error "unknown command: $command"
exit -1
}