-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvDiagram.ps1
More file actions
164 lines (138 loc) · 4 KB
/
vDiagram.ps1
File metadata and controls
164 lines (138 loc) · 4 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
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
Param ($VIServer=$FALSE, $Cluster=$FALSE)
$SaveFile = [system.Environment]::GetFolderPath('MyDocuments') + "\My_vDrawing.vsd"
if ($VIServer -eq $FALSE) { $VIServer = Read-Host "Please enter a Virtual Center name or ESX Host to diagram:" }
$shpFile = "\My-VI-Shapes.vss"
function connect-visioobject ($firstObj, $secondObj)
{
$shpConn = $pagObj.Drop($pagObj.Application.ConnectorToolDataObject, 0, 0)
#// Connect its Begin to the 'From' shape:
$connectBegin = $shpConn.CellsU("BeginX").GlueTo($firstObj.CellsU("PinX"))
#// Connect its End to the 'To' shape:
$connectEnd = $shpConn.CellsU("EndX").GlueTo($secondObj.CellsU("PinX"))
}
function add-visioobject ($mastObj, $item)
{
Write-Host "Adding $item"
# Drop the selected stencil on the active page, with the coordinates x, y
$shpObj = $pagObj.Drop($mastObj, $x, $y)
# Enter text for the object
$shpObj.Text = $item
#Return the visioobject to be used
return $shpObj
}
# Create an instance of Visio and create a document based on the Basic Diagram template.
$AppVisio = New-Object -ComObject Visio.Application
$docsObj = $AppVisio.Documents
$DocObj = $docsObj.Add("Basic Diagram.vst")
# Set the active page of the document to page 1
$pagsObj = $AppVisio.ActiveDocument.Pages
$pagObj = $pagsObj.Item(1)
# Connect to the VI Server
Write-Host "Connecting to $VIServer"
$VIServer = Connect-VIServer $VIServer
# Load a set of stencils and select one to drop
$stnPath = [system.Environment]::GetFolderPath('MyDocuments') + "\My Shapes"
$stnObj = $AppVisio.Documents.Add($stnPath + $shpFile)
$VCObj = $stnObj.Masters.Item("Virtual Center Management Console")
$HostObj = $stnObj.Masters.Item("ESX Host")
$MSObj = $stnObj.Masters.Item("Microsoft Server")
$LXObj = $stnObj.Masters.Item("Linux Server")
$OtherObj = $stnObj.Masters.Item("Other Server")
$CluShp = $stnObj.Masters.Item("Cluster")
If ((Get-Cluster) -ne $Null){
If ($Cluster -eq $FALSE){
$DrawItems = get-cluster
}Else {
$DrawItems = (Get-Cluster $Cluster)
}
$x = 0
$VCLocation = $DrawItems | Get-VMHost
$y = $VCLocation.Length * 1.50 / 2
$VCObject = add-visioobject $VCObj $VIServer
$x = 1.50
$y = 1.50
ForEach ($Cluster in $DrawItems)
{
$CluVisObj = add-visioobject $CluShp $Cluster
connect-visioobject $VCObject $CluVisObj
$x=3.00
ForEach ($VMHost in (Get-Cluster $Cluster | Get-VMHost))
{
$Object1 = add-visioobject $HostObj $VMHost
connect-visioobject $CluVisObj $Object1
ForEach ($VM in (Get-vmhost $VMHost | get-vm))
{
$x += 1.50
If ($vm.Guest.OSFUllName -eq $Null)
{
$Object2 = add-visioobject $OtherObj $VM
}
Else
{
If ($vm.Guest.OSFUllName.contains("Microsoft") -eq $True)
{
$Object2 = add-visioobject $MSObj $VM
}
else
{
$Object2 = add-visioobject $LXObj $VM
}
}
connect-visioobject $Object1 $Object2
$Object1 = $Object2
}
$x = 3.00
$y += 1.50
}
$x = 1.50
}
}
Else
{
$DrawItems = Get-VMHost
$x = 0
$y = $DrawItems.Length * 1.50 / 2
$VCObject = add-visioobject $VCObj $VIServer
$x = 1.50
$y = 1.50
ForEach ($VMHost in $DrawItems)
{
$Object1 = add-visioobject $HostObj $VMHost
connect-visioobject $VCObject $Object1
ForEach ($VM in (Get-vmhost $VMHost | get-vm))
{
$x += 1.50
If ($vm.Guest.OSFUllName -eq $Null)
{
$Object2 = add-visioobject $OtherObj $VM
}
Else
{
If ($vm.Guest.OSFUllName.contains("Microsoft") -eq $True)
{
$Object2 = add-visioobject $MSObj $VM
}
else
{
$Object2 = add-visioobject $LXObj $VM
}
}
connect-visioobject $Object1 $Object2
$Object1 = $Object2
}
$x = 1.50
$y += 1.50
}
$x = 1.50
}
# Resize to fit page
$pagObj.ResizeToFitContents()
# Zoom to 50% of the drawing - Not working yet
#$Application.ActiveWindow.Page = $pagObj.NameU
#$AppVisio.ActiveWindow.zoom = [double].5
# Save the diagram
$DocObj.SaveAs("$Savefile")
# Quit Visio
#$AppVisio.Quit()
Write-Output "Document saved as $savefile"
Disconnect-VIServer -Server $VIServer -Confirm:$false