-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExport-SCNetworkConfig.ps1
More file actions
121 lines (85 loc) · 4.52 KB
/
Export-SCNetworkConfig.ps1
File metadata and controls
121 lines (85 loc) · 4.52 KB
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
<#
.SYNOPSIS
This script is intended to export SCVMM network configuration.
.DESCRIPTION
The script lists every logical networks and dependencies such as vm networks and sites from a SCVMM server and exports the results as a .ps1 file so it can be replay
on a new SCVMM server. The script can be run from the powershell console of the SCVMM console or from a computer with the VirtualMachineManager already loaded and with
an access to the SCVMM server. Just use the Get-SCVMMServer beforehand with a user with sufficient permissions.
.EXAMPLE
"./Export-SCNetworkConfig.ps1" -Output "C:\Import-SCVNetworkConfig.ps1"
.INPUTS
Output : Filepath to the script which will be use to import resultas in the new scvmm server.
.COMPONENTS
Import-Module VirtualMachineManager
.NOTES
Written by : Lucas Bablon
Version : 1.0
Link : https://github.com/lbablon
#>
#required components
#requires -Module virtualmachinemanager
#params
param
(
[Parameter(Mandatory=$false, HelpMessage="Save the results in a .ps1 script")]
[string]$output=".\Import-SCNetworkConfig.ps1"
)
##
##SCRIPT
##
#initialize the output file
Write-Output "<#" | Out-File $output -Append
Write-Output "`n.SYNOPSIS" | Out-File $output -Append
Write-Output "`nThis script is intended to import network configuration on a SCVMM server." | Out-File $output -Append
Write-Output "`n.DESCRIPTION" | Out-File $output -Append
Write-Output "`nThis script was automatically generated by Export-SCVNetworkConfig.ps1. Run the import script from the SCVMM console of the server you want to import in." | Out-File $output -Append
Write-Output "`n#>" | Out-File $output -Append
Write-Output "`n#required components" | Out-File $output -Append
Write-Output "#requires -Module virtualmachinemanager" | Out-File $output -Append
Write-Output "`n#script" | Out-File $output -Append
#list all logical networks
$logicalNetworks = Get-SCLogicalNetwork | select -first 5
Write-Host "Exporting creation commands to $output..."
#foreach logical network found export sites and vm networks
$i=1
$logicalnetworks | % {
$logicalNetwork = $_
$logicalNetworkname = $logicalNetwork.Name
#progress bar
$completed=[math]::round(($i/$logicalNetworks.count)*100)
Write-Progress -Activity "Exporting configuration" -Status "$completed% complete" -PercentComplete $completed
Start-Sleep -Seconds 3
$i++
#Logical network creation commands are written in the output file
Write-Output "`n#Creates logical network $logicalNetworkname" | Out-File $output -Append
$print = 'New-SCLogicalNetwork -Name '+$logicalnetworkname+' -LogicalNetworkDefinitionIsolation $true -EnableNetworkVirtualization $false -UseGRE $false -IsPVLAN $false'
Write-Output $print | Out-File $output -Append
#foreach logical network found, list all sites and their configuration
$sites = Get-SCLogicalNetworkDefinition -LogicalNetwork $logicalNetwork
$sites | % {
$site = $_
$sitename = $site.name
$subnet = $site.SubnetVLans.Subnet
$vlan = $site.SubnetVLans.VlanID
$hostgroup = $site.HostGroups.Name
#Network sites creation commands
Write-Output "`n#Creates network site $sitename for logical network $logicalNetworkname" | Out-File $output -Append
$print = '$allSubnetVlan = @()'
write-output $print | Out-File $output -Append
$print = '$allSubnetVlan += New-SCSubnetVLan -Subnet '+$subnet+' -VLanID '+$vlan
write-output $print | Out-File $output -Append
$print = 'New-SCLogicalNetworkDefinition -Name '+$sitename+' -LogicalNetwork '+$logicalNetwork+' -VMHostGroup '+$hostgroup+' -SubnetVLan $allSubnetVlan -RunAsynchronously'
Write-output $print | Out-File $output -Append
#VM Networks creation commands
Write-Output "`n#Creates VM Network for site $sitename" | Out-File $output -Append
$print = '$vmNetwork = New-SCVMNetwork -Name '+$sitename+' -LogicalNetwork '+$logicalNetwork+' -IsolationType VLANNetwork'
write-output $print | Out-File $output -Append
$subnetVLANs = @()
$subnetVLANv4 = New-SCSubnetVLan -Subnet $subnet -VLanID $vlan
$subnetVLANs += $subnetVLANv4
$print = '$logicalNetworkDefinition = Get-SCLogicalNetworkDefinition -name '+$sitename
write-output $print | Out-File $output -Append
$print = 'New-SCVMSubnet -Name '+$sitename+' -LogicalNetworkDefinition '+$logicalNetworkDefinition+' -SubnetVLan $subnetVLANs -VMNetwork '+$vmNetwork
write-output $print | Out-File $output -Append
}
}