├── Example Shapes └── My-VI-Shapes.vss └── vDiagram.ps1 /Example Shapes/My-VI-Shapes.vss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alanrenouf/VisioPowerShell/c60d64b92e499a71fe5906e2fa6f6e2d59ee527a/Example Shapes/My-VI-Shapes.vss -------------------------------------------------------------------------------- /vDiagram.ps1: -------------------------------------------------------------------------------- 1 | Param ($VIServer=$FALSE, $Cluster=$FALSE) 2 | 3 | $SaveFile = [system.Environment]::GetFolderPath('MyDocuments') + "\My_vDrawing.vsd" 4 | if ($VIServer -eq $FALSE) { $VIServer = Read-Host "Please enter a Virtual Center name or ESX Host to diagram:" } 5 | 6 | $shpFile = "\My-VI-Shapes.vss" 7 | 8 | 9 | function connect-visioobject ($firstObj, $secondObj) 10 | { 11 | $shpConn = $pagObj.Drop($pagObj.Application.ConnectorToolDataObject, 0, 0) 12 | 13 | #// Connect its Begin to the 'From' shape: 14 | $connectBegin = $shpConn.CellsU("BeginX").GlueTo($firstObj.CellsU("PinX")) 15 | 16 | #// Connect its End to the 'To' shape: 17 | $connectEnd = $shpConn.CellsU("EndX").GlueTo($secondObj.CellsU("PinX")) 18 | } 19 | 20 | function add-visioobject ($mastObj, $item) 21 | { 22 | Write-Host "Adding $item" 23 | # Drop the selected stencil on the active page, with the coordinates x, y 24 | $shpObj = $pagObj.Drop($mastObj, $x, $y) 25 | # Enter text for the object 26 | $shpObj.Text = $item 27 | #Return the visioobject to be used 28 | return $shpObj 29 | } 30 | 31 | # Create an instance of Visio and create a document based on the Basic Diagram template. 32 | $AppVisio = New-Object -ComObject Visio.Application 33 | $docsObj = $AppVisio.Documents 34 | $DocObj = $docsObj.Add("Basic Diagram.vst") 35 | 36 | # Set the active page of the document to page 1 37 | $pagsObj = $AppVisio.ActiveDocument.Pages 38 | $pagObj = $pagsObj.Item(1) 39 | 40 | # Connect to the VI Server 41 | Write-Host "Connecting to $VIServer" 42 | $VIServer = Connect-VIServer $VIServer 43 | 44 | # Load a set of stencils and select one to drop 45 | $stnPath = [system.Environment]::GetFolderPath('MyDocuments') + "\My Shapes" 46 | $stnObj = $AppVisio.Documents.Add($stnPath + $shpFile) 47 | $VCObj = $stnObj.Masters.Item("Virtual Center Management Console") 48 | $HostObj = $stnObj.Masters.Item("ESX Host") 49 | $MSObj = $stnObj.Masters.Item("Microsoft Server") 50 | $LXObj = $stnObj.Masters.Item("Linux Server") 51 | $OtherObj = $stnObj.Masters.Item("Other Server") 52 | $CluShp = $stnObj.Masters.Item("Cluster") 53 | 54 | If ((Get-Cluster) -ne $Null){ 55 | 56 | If ($Cluster -eq $FALSE){ 57 | $DrawItems = get-cluster 58 | }Else { 59 | $DrawItems = (Get-Cluster $Cluster) 60 | } 61 | 62 | $x = 0 63 | $VCLocation = $DrawItems | Get-VMHost 64 | $y = $VCLocation.Length * 1.50 / 2 65 | 66 | $VCObject = add-visioobject $VCObj $VIServer 67 | 68 | $x = 1.50 69 | $y = 1.50 70 | 71 | ForEach ($Cluster in $DrawItems) 72 | { 73 | $CluVisObj = add-visioobject $CluShp $Cluster 74 | connect-visioobject $VCObject $CluVisObj 75 | 76 | $x=3.00 77 | ForEach ($VMHost in (Get-Cluster $Cluster | Get-VMHost)) 78 | { 79 | $Object1 = add-visioobject $HostObj $VMHost 80 | connect-visioobject $CluVisObj $Object1 81 | ForEach ($VM in (Get-vmhost $VMHost | get-vm)) 82 | { 83 | $x += 1.50 84 | If ($vm.Guest.OSFUllName -eq $Null) 85 | { 86 | $Object2 = add-visioobject $OtherObj $VM 87 | } 88 | Else 89 | { 90 | If ($vm.Guest.OSFUllName.contains("Microsoft") -eq $True) 91 | { 92 | $Object2 = add-visioobject $MSObj $VM 93 | } 94 | else 95 | { 96 | $Object2 = add-visioobject $LXObj $VM 97 | } 98 | } 99 | connect-visioobject $Object1 $Object2 100 | $Object1 = $Object2 101 | } 102 | $x = 3.00 103 | $y += 1.50 104 | } 105 | $x = 1.50 106 | } 107 | } 108 | Else 109 | { 110 | $DrawItems = Get-VMHost 111 | 112 | $x = 0 113 | $y = $DrawItems.Length * 1.50 / 2 114 | 115 | $VCObject = add-visioobject $VCObj $VIServer 116 | 117 | $x = 1.50 118 | $y = 1.50 119 | 120 | ForEach ($VMHost in $DrawItems) 121 | { 122 | $Object1 = add-visioobject $HostObj $VMHost 123 | connect-visioobject $VCObject $Object1 124 | ForEach ($VM in (Get-vmhost $VMHost | get-vm)) 125 | { 126 | $x += 1.50 127 | If ($vm.Guest.OSFUllName -eq $Null) 128 | { 129 | $Object2 = add-visioobject $OtherObj $VM 130 | } 131 | Else 132 | { 133 | If ($vm.Guest.OSFUllName.contains("Microsoft") -eq $True) 134 | { 135 | $Object2 = add-visioobject $MSObj $VM 136 | } 137 | else 138 | { 139 | $Object2 = add-visioobject $LXObj $VM 140 | } 141 | } 142 | connect-visioobject $Object1 $Object2 143 | $Object1 = $Object2 144 | } 145 | $x = 1.50 146 | $y += 1.50 147 | } 148 | $x = 1.50 149 | } 150 | 151 | # Resize to fit page 152 | $pagObj.ResizeToFitContents() 153 | 154 | # Zoom to 50% of the drawing - Not working yet 155 | #$Application.ActiveWindow.Page = $pagObj.NameU 156 | #$AppVisio.ActiveWindow.zoom = [double].5 157 | 158 | # Save the diagram 159 | $DocObj.SaveAs("$Savefile") 160 | 161 | # Quit Visio 162 | #$AppVisio.Quit() 163 | Write-Output "Document saved as $savefile" 164 | Disconnect-VIServer -Server $VIServer -Confirm:$false --------------------------------------------------------------------------------