Wstęp
Witam wszystkich czytelników bloga poświęconego architekturze IT i szkoleniom.
Kontynuujemy publikację artykułów pod wspólnym tytułem „Architektura i projektowanie. Wdrożenie i eksploatacja. Kod i programowanie”.
W poprzednim materiale omawialiśmy sposób przedstawienia schematu architektury systemu Microsoft System Center Virtual Machine Manager 2007 w formacie Visio w ramach dedykowanego cyklu artykułów. Do realizacji tego zadania wykorzystaliśmy kod PowerShell. Rezultat w pełni spełnił nasze oczekiwania. Jednak istotnym minusem okazała się duża objętość kodu oraz brak strategii jego ponownego użycia. Dzięki funkcjom możemy uporządkować bloki kodu tak, aby móc je wielokrotnie wykorzystywać i jednocześnie zmniejszyć jego ogólną objętość.
Temu właśnie będzie poświęcony ten wpis.
Funkcje PowerShell
Aby korzystać z funkcji PowerShell, konieczne jest umieszczenie kodu w pliku oraz zastosowanie odpowiedniej składni poleceń i inicjalizacji tych elementów. Dlatego utworzymy nowy plik PSVisio.ps1, w którym zgromadzimy wszystkie nasze rozwiązania.
Aby zainicjalizować kod napisanych przez nas funkcji PowerShell, należy wykonać następujące polecenie:
. .\PSVisio.ps1
W przypadku wprowadzenia zmian w kodzie, zmodyfikowaną funkcję należy ponownie załadować poleceniem:
rm Function:\NameOfFunction
A następnie ponownie należy wykonać inicjalizację poleceniem opisanym powyżej.
Na początku tworzenia naszego kodu i uzupełniania go funkcjami plik PSVisio.ps1 będzie zawierał nagłówek w formie komentarzy oraz początkową inicjalizację kilku zmiennych. Zostaną one wykorzystane do tworzenia nazw obiektów poprzez ich kolejne numerowanie:
#######################################################################
#
# Powershell functions for operate Visio Drawing
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 14.09.2007
# Purpose/Change: Initial script development.
# Create function New-VisioApplication.
#
# Run:
#
# . .\PSVisio.ps1
#
#######################################################################
# Set Variables
$Shape=0
$Line=0
$Icon=0
$Text=0
$PolyLine=0
New-VisioApplication
Przypomnijmy sobie początkowy fragment kodu, który opisaliśmy w poprzednim wpisie:
# Step 1.
# Create Visio Object
# Create Document from Blank Template
# Set Active Page
$Application = New-Object -ComObject Visio.Application
$Application.Visible = $True
$Documents = $Application.Documents
$Document=$Application.Documents.Add('')
$Page=$Application.ActivePage
$Application.ActivePage.PageSheet
# Step 2.
# Add Basic Visio Stensils
# Set Masters Item Rectangle
$Stensil1 = $Application.Documents.Add("BASIC_M.vss")
$Rectangle = $Stensil1.Masters.Item("Rectangle")
Jak widać, zaczyna się on od poleceń tworzących obiekt aplikacji Visio. Uporządkujmy te proste kroki w postaci pierwszej funkcji – New-VisioApplication.
Function New-VisioApplication {
#######################################################################
#
# Powershell function for create Visio Application
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 14.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# New-VisioApplication
#
#######################################################################
# Create Visio Object
$Script:Application = New-Object -ComObject Visio.Application
$Script:Application.Visible = $True
}
Funkcja okazała się dość prosta i nie wymaga żadnych parametrów. Nie zapominajmy o dodawaniu komentarzy wyjaśniających nasz kod.
New-VisioDocument
Opiszmy kolejną funkcję służącą do tworzenia nowego dokumentu Visio: New-VisioDocument.
Funkcja utworzy nowy dokument Visio na podstawie pustego szablonu. Funkcja ta również nie posiada żadnych parametrów wejściowych.
Function New-VisioDocument {
#######################################################################
#
# Powershell function for create Visio Document
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 14.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# New-VisioDocument
#
#######################################################################
# Create Document from Blank Template
$Script:Documents = $Script:Application.Documents
$Script:Document=$Script:Application.Documents.Add('')
}
Set-VisioPage
Funkcja Set-VisioPage również jest prosta. Jej logika polega na ustawieniu aktywnej strony, która będzie podstawą naszej przyszłej schemy. Funkcja nie posiada parametrów wejściowych.
Function Set-VisioPage {
#######################################################################
#
# Powershell function for Set Active Visio Document Page
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 14.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Set-VisioPage
#
#######################################################################
# Set Visio Active Page
$Script:Page=$Script:Application.ActivePage
$Script:Application.ActivePage.PageSheet
}
Add-VisioStensil
W Visio figury opierają się na szablonach (stencils). Dlatego zadaniem kolejnej funkcji Add-VisioStensil jest dodanie szablonów figur z odpowiedniego pliku .vss. Funkcja przyjmuje dwa parametry wejściowe: Name – nazwa obiektu szablonu, File – nazwa pliku szablonów.
Function Add-VisioStensil ($Name, $File) {
#######################################################################
#
# Powershell function for Add Visio Stensil
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 15.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Add-VisioStensil -Name "Basic" -File "BASIC_M.vss"
#
#######################################################################
# Set Expression and Add Visio Stensil
$Expression = '$Script:' + $Name + ' = $Script:Application.Documents.Add("' + $File +'")'
Invoke-Expression $Expression
}
Set-VisioStensilMasterItem
Aby pracować z konkretną figurą, należy określić, który element będzie głównym wzorcem figury. Logikę tej operacji zaimplementujemy w postaci funkcji Set-VisioStensilMasterItem. Funkcja posiada dwa parametry wejściowe: Stensil – nazwa obiektu szablonu, Item – nazwa figury.
Function Set-VisioStensilMasterItem ($Stensil, $Item) {
#######################################################################
#
# Powershell function for Set Stensil Master Item
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 15.09.2007
# Purpose/Change: Initial script development
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Reorganize Variables
#
# Run:
#
# Set-VisioStensilMasterItem -Stensil "Basic" -Item "Rectangle"
#
#######################################################################
# Set Expression And Set Masters Item Rectangle
$ItemWithoutSpace = $Item -replace " ",""
$Expression = '$Script:' + $ItemWithoutSpace + ' = $Script:' + $Stensil + '.Masters.Item("' + $Item + '")'
Invoke-Expression $Expression
}
Draw-VisioItem
Dotarliśmy wreszcie do funkcji, które będą wykonywać operacje tworzenia i wyświetlania obiektów o określonych właściwościach. Na początek będą to proste figury geometryczne – prostokąt, koło, linia itp.
Ponownie przypomnijmy sobie fragment kodu, który wykonuje te operacje.
# Step 3.
# Draw Main Rectangle, Set Size, Set Colour
# Set Text, Size, Color, Align
# Draw Line, Set Weight, Color
$Shape1 = $Page.Drop($Rectangle, 6.375, 7.125)
$Shape1.Cells('Width').Formula = '12.2501'
$Shape1.Cells('Height').Formula = '7.25'
$Shape1.Cells('FillForegnd').Formula = '=RGB(0,153,204)'
$Shape1.Cells('LinePattern').Formula = 0
$Shape1.Text = "Microsoft Virtual Machine Manager Architecture"
$Shape1.Cells('VerticalAlign') = 0
$Shape1.Cells('Para.HorzAlign') = 0
$Shape1.Cells('Char.Size').Formula = '20 pt'
$Shape1.Cells('Char.Color').Formula = '=RGB(255,255,255)'
$Line1 = $Page.DrawLine(0.3125, 10.3438, 12.4948, 10.3438)
$Line1.Cells('LineWeight').Formula = '1 pt'
$Line1.Cells('LineColor').Formula = '=RGB(255,255,255)'
# Step 4.
# Draw Client Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
$Shape2 = $Page.Drop($Rectangle, 1.7656, 9.2344)
$Shape2.Cells('Width').Formula = '2.7813'
$Shape2.Cells('Height').Formula = '1.9688'
$Shape2.Cells('FillForegnd').Formula = '=RGB(209,235,241)'
$Shape2.Cells('LinePattern').Formula = 1
$Shape2.Text = "Client"
$Shape2.Cells('VerticalAlign') = 0
$Shape2.Cells('Char.Size').Formula = '14 pt'
$Line2 = $Page.DrawLine(0.4297, 9.9427, 3.0833, 9.9427)
$Line2.Cells('LineWeight').Formula = '0.5 pt'
$Line2.Cells('LineColor').Formula = '=RGB(0,0,0)'
# Step 5.
# Add Computer Items Visio Stensils
# Set Masters item PC
$Stensil2 = $Application.Documents.Add("COMPS_M.vss")
$PC1 = $Stensil2.Masters.Item("PC")
# Step 6.
# Draw item PC
# Set Text, Size
$Shape3 = $Page.Drop($PC1, 1.1173, 9.1693)
$Shape3.Text = "Administrator Console"
$Shape3.Cells('Char.Size').Formula = '10 pt'
# Step 7.
# Draw Powershell Icon
# Set Position, Size
# Set Text
$Picture1 = $Page.Import("c:\!\powershell.png")
$Picture1.Cells('Width').Formula = '0.9843'
$Picture1.Cells('Height').Formula = '0.9843'
$Picture1.Cells('PinX').Formula = '2.5547'
$Picture1.Cells('PinY').Formula = '9.2682'
$Picture1.Text = "Windows Powershell"
$Picture1.Cells('Char.Size').Formula = '10 pt'
Funkcja Draw-VisioItem posiada trzy obowiązkowe parametry: Master – nazwa wzorca figury, współrzędne położenia X i Y. Pozostałe parametry, takie jak: FillForegnd – kolor wypełnienia figury, LinePattern – styl linii obrysu figury, Text – tekst figury, VerticalAlign i ParaHorzAlign – parametry wyrównania tekstu w pionie i poziomie, CharSize i CharColor – rozmiar i kolor znaków tekstu, są opcjonalne.
Function Draw-VisioItem ($Master, $X, $Y, $Width, $Height, $FillForegnd, $LinePattern, $Text, $VerticalAlign, $ParaHorzAlign, $CharSize, $CharColor) {
#######################################################################
#
# Powershell function for Draw Visio Item
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 15.09.2007
# Purpose/Change: Initial script development
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 16.09.2007
# Purpose/Change: Add Flow Control Input Parameters
#
# Version: 0.3
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Reorganize Variables
#
# Run:
#
# Draw-VisioItem -Master "Rectangle" -X 6.375 -Y 7.125 -Width 12.2501 -Height 7.25 -FillForegnd "RGB(0,153,204)"`
# -LinePattern 0 -Text "Microsoft Virtual Machine Manager Architecture" -VerticalAlign 0 -ParaHorzAlign 0`
# -CharSize "20 pt" -CharColor "RGB(255,255,255)"
#
#######################################################################
$Script:Shape++
$Master = $Master -replace " ",""
# Set Expression And Draw Item
$Expression = '$Script:Shape' + $Script:Shape + ' = $Script:Page.Drop(' + '$' + $Master + ',' + $X + ',' + $Y + ')'
Invoke-Expression $Expression
# Set Item Width Properties
If ($Width)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Width").Formula = ' + $Width
Invoke-Expression $Expression
}
# Set Item Height Properties
If ($Height)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Height").Formula = ' + $Height
Invoke-Expression $Expression
}
# Set Item FillForegnd Properties
If ($FillForegnd)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("FillForegnd").Formula = "=' + $FillForegnd + '"'
Invoke-Expression $Expression
}
# Set Item LinePattern Properties
If ($LinePattern)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("LinePattern").Formula = ' + $LinePattern
Invoke-Expression $Expression
}
# Set Item Text
If ($Text)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Text = "' + $Text + '"'
Invoke-Expression $Expression
}
# Set Item VerticalAlign Properties
If ($VerticalAlign)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("VerticalAlign").Formula = ' + $VerticalAlign
Invoke-Expression $Expression
}
# Set Item HorzAlign Properties
If ($ParaHorzAlign)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Para.HorzAlign").Formula = ' + $ParaHorzAlign
Invoke-Expression $Expression
}
# Set Item Char.Size Properties
If ($CharSize)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Char.Size").Formula = "' + $CharSize + '"'
Invoke-Expression $Expression
}
# Set Item Char.Color Properties
If ($CharColor)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Char.Color").Formula = "=' + $CharColor + '"'
Invoke-Expression $Expression
}
}
Draw-VisioLine
Funkcja Draw-VisioLine została opracowana do rysowania różnych linii. Posiada kilka obowiązkowych parametrów: współrzędne początku BeginX, BeginY oraz końca EndX, EndY. Dodatkowo można określić parametry opcjonalne: LineColor – kolor linii, BeginArrow, EndArrow – styl strzałek na początku i końcu linii.
Function Draw-VisioLine ($BeginX, $BeginY, $EndX, $EndY, $LineWeight, $LineColor, $BeginArrow, $EndArrow) {
#######################################################################
#
# Powershell function for Draw Visio Line
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 15.09.2007
# Purpose/Change: Initial script development
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Add Flow Control Input Parameters
#
# Run:
#
# Draw-VisioLine -BeginX 0.3125 -BeginY 10.3438 -EndX 12.4948 -EndY 10.3438 -LineWeight "1 pt"`
# -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
#
#######################################################################
$Script:Line++
# Set Expression And Draw Line
$Expression = '$Script:Line' + $Script:Line + ' = $Script:Page.DrawLine(' + $BeginX + ',' + $BeginY + ',' + $EndX + ',' + $EndY + ')'
Invoke-Expression $Expression
# Set Line Width Properties
If ($LineWeight)
{
$Expression = '$Script:Line' + $Script:Line + '.Cells("LineWeight").Formula = "' + $LineWeight + '"'
Invoke-Expression $Expression
}
# Set Line Color Properties
$Expression = '$Script:Line' + $Script:Line + '.Cells("LineColor").Formula = "=' + $LineColor + '"'
Invoke-Expression $Expression
# Set Line Begin Arrow Properties
If ($BeginArrow)
{
$Expression = '$Script:Line' + $Script:Line + '.Cells("BeginArrow").Formula = ' + $BeginArrow
Invoke-Expression $Expression
}
# Set Line End Arrow Properties
If ($EndArrow)
{
$Expression = '$Script:Line' + $Script:Line + '.Cells("EndArrow").Formula = ' + $EndArrow
Invoke-Expression $Expression
}
}
Draw-VisioIcon
Funkcja Draw-VisioIcon umożliwia dodawanie do schematów wcześniej przygotowanych ikon w formacie .png
lub .jpeg
. Posiada kilka obowiązkowych parametrów: IconPath – ścieżka i nazwa pliku ikony, wymiary Width, Height, współrzędne PinX, PinY. Parametry opcjonalne to: Text – tekst etykiety, CharSize – rozmiar znaków tekstu.
Function Draw-VisioIcon ($IconPath, $Width, $Height, $PinX, $PinY, $Text, $CharSize) {
#######################################################################
#
# Powershell function for Draw Visio Icon
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 16.09.2007
# Purpose/Change: Initial script development
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Add Flow Control Input Parameters
#
# Run:
#
# Draw-VisioIcon -IconPath "c:\!\powershell.png" -Width 0.9843 -Height 0.9843 -PinX 2.5547 -PinY 9.2682`
# -Text "Windows Powershell" -CharSize "10 pt"
#
#######################################################################
$Script:Icon++
# Import Icon Item
$Expression = '$Script:Icon' + $Script:Icon + ' = $Script:Page.Import("' + $IconPath + '")'
Invoke-Expression $Expression
# Set Icon Width Properties
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("Width").Formula = ' + $Width
Invoke-Expression $Expression
# Set Icon Height Properties
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("Height").Formula = ' + $Height
Invoke-Expression $Expression
# Set Icon PinX Properties
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("PinX").Formula = ' + $PinX
Invoke-Expression $Expression
# Set Icon PinY Properties
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("PinY").Formula = ' + $PinY
Invoke-Expression $Expression
# Set Icon Text
If ($Text)
{
$Expression = '$Script:Icon' + $Script:Icon + '.Text = "' + $Text + '"'
Invoke-Expression $Expression
}
# Set Icon Char.Size Properties
If ($CharSize)
{
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("Char.Size").Formula = "' + $CharSize + '"'
Invoke-Expression $Expression
}
}
Resize-VisioPageToFitContents
Funkcja Resize-VisioPageToFitContents jest niezwykle prosta – jej zadaniem jest dopasowanie rozmiaru strony do wymiarów rysunku. Funkcja nie posiada parametrów wejściowych.
Function Resize-VisioPageToFitContents {
#######################################################################
#
# Powershell function for Resize Active Visio Document Page to Fit Contents
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Resize-VisioPageToFitContents
#
#######################################################################
# Resize Page to Fit Contents
$Script:Page.ResizeToFitContents()
}
Save-VisioDocument
Pozostały drobne, końcowe kroki. Logika funkcji Save-VisioDocument polega na zapisaniu utworzonej diagramu w postaci pliku Visio .vsd
. Funkcja posiada obowiązkowy parametr File – nazwa pliku Visio .vsd
.
Function Save-VisioDocument ($File) {
#######################################################################
#
# Powershell function for save Visio Document
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Save-VisioDocument -File 'C:\!\MsSCVMM2007Arch.vsd'
#
#######################################################################
# Save Document
$Expression = '$Script:Document.SaveAs("' + $File + '")'
Invoke-Expression $Expression
}
Close-VisioApplication
Kończy tę linię narracyjną funkcja Close-VisioApplication. Nie posiada parametrów, a jej logika polega wyłącznie na zamknięciu aplikacji Visio.
Function Close-VisioApplication {
#######################################################################
#
# Powershell function for create Visio Application
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Close-VisioApplication
#
#######################################################################
# Close Visio Application
$Script:Application.Quit()
}
PSVisio.ps1
Ostateczny rezultat kodu funkcji w pliku PSVisio.ps1 wygląda następująco:
#######################################################################
#
# Powershell functions for operate Visio Drawing
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 14.09.2007
# Purpose/Change: Initial script development.
# Create function New-VisioApplication.
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 14.09.2007
# Purpose/Change: Create function New-VisioDocument.
#
# Version: 0.3
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 14.09.2007
# Purpose/Change: Create function Set-VisioPage.
#
# Version: 0.4
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 15.09.2007
# Purpose/Change: Create function Add-VisioStensil.
#
# Version: 0.5
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 15.09.2007
# Purpose/Change: Create function Set-VisioStensilMasterItem.
#
# Version: 0.6
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 15.09.2007
# Purpose/Change: Create function Draw-VisioItem.
#
# Version: 0.7
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 15.09.2007
# Purpose/Change: Create function Draw-VisioLine.
#
# Version: 0.8
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 16.09.2007
# Purpose/Change: Change function Draw-VisioItem.
#
# Version: 0.9
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 16.09.2007
# Purpose/Change: Create function Draw-VisioIcon.
#
# Version: 0.10
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 17.09.2007
# Purpose/Change: Change function Draw-VisioIcon.
#
# Version: 0.11
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 17.09.2007
# Purpose/Change: Change function Draw-VisioLine.
#
# Version: 0.12
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 17.09.2007
# Purpose/Change: Change function Set-VisioStensilMasterItem.
#
# Version: 0.13
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 17.09.2007
# Purpose/Change: Change function Draw-VisioItem.
#
# Version: 0.14
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 17.09.2007
# Purpose/Change: Add function Resize-VisioPageToFitContents.
#
# Version: 0.15
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 17.09.2007
# Purpose/Change: Add function Save-VisioDocument.
#
#
# Version: 0.16
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 18.09.2007
# Purpose/Change: Add function Draw-VisioText.
#
# Version: 0.17
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 18.09.2007
# Purpose/Change: Add function Draw-VisioPolyLine.
#
# Version: 0.18
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Change Date: 19.09.2007
# Purpose/Change: Change function Draw-VisioText.
#
# Run:
#
# . .\PSVisio.ps1
#
#######################################################################
# Set Variables
$Shape=0
$Line=0
$Icon=0
$Text=0
$PolyLine=0
Function New-VisioApplication {
#######################################################################
#
# Powershell function for create Visio Application
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 14.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# New-VisioApplication
#
#######################################################################
# Create Visio Object
$Script:Application = New-Object -ComObject Visio.Application
$Script:Application.Visible = $True
}
Function New-VisioDocument {
#######################################################################
#
# Powershell function for create Visio Document
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 14.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# New-VisioDocument
#
#######################################################################
# Create Document from Blank Template
$Script:Documents = $Script:Application.Documents
$Script:Document=$Script:Application.Documents.Add('')
}
Function Set-VisioPage {
#######################################################################
#
# Powershell function for Set Active Visio Document Page
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 14.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Set-VisioPage
#
#######################################################################
# Set Visio Active Page
$Script:Page=$Script:Application.ActivePage
$Script:Application.ActivePage.PageSheet
}
Function Add-VisioStensil ($Name, $File) {
#######################################################################
#
# Powershell function for Add Visio Stensil
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 15.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Add-VisioStensil -Name "Basic" -File "BASIC_M.vss"
#
#######################################################################
# Set Expression and Add Visio Stensil
$Expression = '$Script:' + $Name + ' = $Script:Application.Documents.Add("' + $File +'")'
Invoke-Expression $Expression
}
Function Set-VisioStensilMasterItem ($Stensil, $Item) {
#######################################################################
#
# Powershell function for Set Stensil Master Item
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 15.09.2007
# Purpose/Change: Initial script development
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Reorganize Variables
#
# Run:
#
# Set-VisioStensilMasterItem -Stensil "Basic" -Item "Rectangle"
#
#######################################################################
# Set Expression And Set Masters Item Rectangle
$ItemWithoutSpace = $Item -replace " ",""
$Expression = '$Script:' + $ItemWithoutSpace + ' = $Script:' + $Stensil + '.Masters.Item("' + $Item + '")'
Invoke-Expression $Expression
}
Function Draw-VisioItem ($Master, $X, $Y, $Width, $Height, $FillForegnd, $LinePattern, $Text, $VerticalAlign, $ParaHorzAlign, $CharSize, $CharColor) {
#######################################################################
#
# Powershell function for Draw Visio Item
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 15.09.2007
# Purpose/Change: Initial script development
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 16.09.2007
# Purpose/Change: Add Flow Control Input Parameters
#
# Version: 0.3
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Reorganize Variables
#
# Run:
#
# Draw-VisioItem -Master "Rectangle" -X 6.375 -Y 7.125 -Width 12.2501 -Height 7.25 -FillForegnd "RGB(0,153,204)"`
# -LinePattern 0 -Text "Microsoft Virtual Machine Manager Architecture" -VerticalAlign 0 -ParaHorzAlign 0`
# -CharSize "20 pt" -CharColor "RGB(255,255,255)"
#
#######################################################################
$Script:Shape++
$Master = $Master -replace " ",""
# Set Expression And Draw Item
$Expression = '$Script:Shape' + $Script:Shape + ' = $Script:Page.Drop(' + '$' + $Master + ',' + $X + ',' + $Y + ')'
Invoke-Expression $Expression
# Set Item Width Properties
If ($Width)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Width").Formula = ' + $Width
Invoke-Expression $Expression
}
# Set Item Height Properties
If ($Height)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Height").Formula = ' + $Height
Invoke-Expression $Expression
}
# Set Item FillForegnd Properties
If ($FillForegnd)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("FillForegnd").Formula = "=' + $FillForegnd + '"'
Invoke-Expression $Expression
}
# Set Item LinePattern Properties
If ($LinePattern)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("LinePattern").Formula = ' + $LinePattern
Invoke-Expression $Expression
}
# Set Item Text
If ($Text)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Text = "' + $Text + '"'
Invoke-Expression $Expression
}
# Set Item VerticalAlign Properties
If ($VerticalAlign)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("VerticalAlign").Formula = ' + $VerticalAlign
Invoke-Expression $Expression
}
# Set Item HorzAlign Properties
If ($ParaHorzAlign)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Para.HorzAlign").Formula = ' + $ParaHorzAlign
Invoke-Expression $Expression
}
# Set Item Char.Size Properties
If ($CharSize)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Char.Size").Formula = "' + $CharSize + '"'
Invoke-Expression $Expression
}
# Set Item Char.Color Properties
If ($CharColor)
{
$Expression = '$Script:Shape' + $Script:Shape + '.Cells("Char.Color").Formula = "=' + $CharColor + '"'
Invoke-Expression $Expression
}
}
Function Draw-VisioLine ($BeginX, $BeginY, $EndX, $EndY, $LineWeight, $LineColor, $BeginArrow, $EndArrow) {
#######################################################################
#
# Powershell function for Draw Visio Line
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 15.09.2007
# Purpose/Change: Initial script development
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Add Flow Control Input Parameters
#
# Run:
#
# Draw-VisioLine -BeginX 0.3125 -BeginY 10.3438 -EndX 12.4948 -EndY 10.3438 -LineWeight "1 pt"`
# -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
#
#######################################################################
$Script:Line++
# Set Expression And Draw Line
$Expression = '$Script:Line' + $Script:Line + ' = $Script:Page.DrawLine(' + $BeginX + ',' + $BeginY + ',' + $EndX + ',' + $EndY + ')'
Invoke-Expression $Expression
# Set Line Width Properties
If ($LineWeight)
{
$Expression = '$Script:Line' + $Script:Line + '.Cells("LineWeight").Formula = "' + $LineWeight + '"'
Invoke-Expression $Expression
}
# Set Line Color Properties
$Expression = '$Script:Line' + $Script:Line + '.Cells("LineColor").Formula = "=' + $LineColor + '"'
Invoke-Expression $Expression
# Set Line Begin Arrow Properties
If ($BeginArrow)
{
$Expression = '$Script:Line' + $Script:Line + '.Cells("BeginArrow").Formula = ' + $BeginArrow
Invoke-Expression $Expression
}
# Set Line End Arrow Properties
If ($EndArrow)
{
$Expression = '$Script:Line' + $Script:Line + '.Cells("EndArrow").Formula = ' + $EndArrow
Invoke-Expression $Expression
}
}
Function Draw-VisioIcon ($IconPath, $Width, $Height, $PinX, $PinY, $Text, $CharSize) {
#######################################################################
#
# Powershell function for Draw Visio Icon
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 16.09.2007
# Purpose/Change: Initial script development
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Add Flow Control Input Parameters
#
# Run:
#
# Draw-VisioIcon -IconPath "c:\!\powershell.png" -Width 0.9843 -Height 0.9843 -PinX 2.5547 -PinY 9.2682`
# -Text "Windows Powershell" -CharSize "10 pt"
#
#######################################################################
$Script:Icon++
# Import Icon Item
$Expression = '$Script:Icon' + $Script:Icon + ' = $Script:Page.Import("' + $IconPath + '")'
Invoke-Expression $Expression
# Set Icon Width Properties
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("Width").Formula = ' + $Width
Invoke-Expression $Expression
# Set Icon Height Properties
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("Height").Formula = ' + $Height
Invoke-Expression $Expression
# Set Icon PinX Properties
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("PinX").Formula = ' + $PinX
Invoke-Expression $Expression
# Set Icon PinY Properties
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("PinY").Formula = ' + $PinY
Invoke-Expression $Expression
# Set Icon Text
If ($Text)
{
$Expression = '$Script:Icon' + $Script:Icon + '.Text = "' + $Text + '"'
Invoke-Expression $Expression
}
# Set Icon Char.Size Properties
If ($CharSize)
{
$Expression = '$Script:Icon' + $Script:Icon + '.Cells("Char.Size").Formula = "' + $CharSize + '"'
Invoke-Expression $Expression
}
}
Function Resize-VisioPageToFitContents {
#######################################################################
#
# Powershell function for Resize Active Visio Document Page to Fit Contents
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Resize-VisioPageToFitContents
#
#######################################################################
# Resize Page to Fit Contents
$Script:Page.ResizeToFitContents()
}
Function Save-VisioDocument ($File) {
#######################################################################
#
# Powershell function for save Visio Document
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Save-VisioDocument -File 'C:\!\MsSCVMM2007Arch.vsd'
#
#######################################################################
# Save Document
$Expression = '$Script:Document.SaveAs("' + $File + '")'
Invoke-Expression $Expression
}
Function Close-VisioApplication {
#######################################################################
#
# Powershell function for create Visio Application
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 17.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Close-VisioApplication
#
#######################################################################
# Close Visio Application
$Script:Application.Quit()
}
Function Draw-VisioText ($X, $Y, $Width, $Height, $FillForegnd, $LinePattern, $Text, $VerticalAlign, $ParaHorzAlign, $CharSize, $CharColor, $CharStyle, $FillForegndTrans) {
#######################################################################
#
# Powershell function for Draw Visio Text Label
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 18.09.2007
# Purpose/Change: Initial script development
#
# Version: 0.2
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 19.09.2007
# Purpose/Change: Add CharStyle Parameter
#
# Run:
#
# Draw-VisioText -X 4.25 -Y 8.875 -Width 1.3751 -Height 0.375 -Text "Microsoft Virtual Machine Manager Architecture"`
# -LinePattern "0" -FillForegndTrans "100%" -CharStyle 17
#
#######################################################################
$Script:Text++
$Master = "Rectangle"
# Set Expression And Draw Text
$Expression = '$Script:Text' + $Script:Text + ' = $Script:Page.Drop(' + '$' + $Master + ',' + $X + ',' + $Y + ')'
Invoke-Expression $Expression
# Set Item Width Properties
If ($Width)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("Width").Formula = ' + $Width
Invoke-Expression $Expression
}
# Set Item Height Properties
If ($Height)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("Height").Formula = ' + $Height
Invoke-Expression $Expression
}
# Set Item FillForegnd Properties
If ($FillForegnd)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("FillForegnd").Formula = "=' + $FillForegnd + '"'
Invoke-Expression $Expression
}
# Set Item LinePattern Properties
If ($LinePattern)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("LinePattern").Formula = ' + $LinePattern
Invoke-Expression $Expression
}
# Set Item Text
If ($Text)
{
$Expression = '$Script:Text' + $Script:Text + '.Text = "' + $Text + '"'
Invoke-Expression $Expression
}
# Set Item VerticalAlign Properties
If ($VerticalAlign)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("VerticalAlign").Formula = ' + $VerticalAlign
Invoke-Expression $Expression
}
# Set Item HorzAlign Properties
If ($ParaHorzAlign)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("Para.HorzAlign").Formula = ' + $ParaHorzAlign
Invoke-Expression $Expression
}
# Set Item Char.Size Properties
If ($CharSize)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("Char.Size").Formula = "' + $CharSize + '"'
Invoke-Expression $Expression
}
# Set Item Char.Color Properties
If ($CharColor)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("Char.Color").Formula = "=' + $CharColor + '"'
Invoke-Expression $Expression
}
# Set Item Char.Style Properties
If ($CharStyle)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("Char.Style").Formula = "' + $CharStyle + '"'
Invoke-Expression $Expression
}
# Set Item FillForegndTrans Properties
If ($FillForegndTrans)
{
$Expression = '$Script:Text' + $Script:Text + '.Cells("FillForegndTrans").Formula = "' + $FillForegndTrans + '"'
Invoke-Expression $Expression
}
}
Function Draw-VisioPolyLine ($PolyLine, $LineWeight, $LineColor, $BeginArrow, $EndArrow) {
#######################################################################
#
# Powershell function for Draw Visio PolyLine
#
# Version: 0.1
# Author: Andrii Romanenko
# Website: blogs.airra.net
# Creation Date: 18.09.2007
# Purpose/Change: Initial script development
#
# Run:
#
# Draw-VisioPolyLine -Polyline 1.0938,9.0625,1.4063,9.0625,1.4063,8.6563,1.0938,8.6563 -LineWeight "0.5 pt"`
# -LineColor "RGB(255,255,255)" -BeginArrow 1 -EndArrow 1
#
#######################################################################
$Script:PolyLine++
[double[]]$PolyLineCoordinates=@()
$PolyLineCoordinates += $Polyline
# Set Expression And Draw PolyLine
$Expression = '$Script:PolyLine' + $Script:PolyLine + ' = $Script:Page.DrawPolyLine(($PolyLineCoordinates),0)'
Invoke-Expression $Expression
# Set Line Width Properties
If ($LineWeight)
{
$Expression = '$Script:PolyLine' + $Script:PolyLine + '.Cells("LineWeight").Formula = "' + $LineWeight + '"'
Invoke-Expression $Expression
}
# Set Line Color Properties
$Expression = '$Script:PolyLine' + $Script:PolyLine + '.Cells("LineColor").Formula = "=' + $LineColor + '"'
Invoke-Expression $Expression
# Set Line Begin Arrow Properties
If ($BeginArrow)
{
$Expression = '$Script:PolyLine' + $Script:PolyLine + '.Cells("BeginArrow").Formula = ' + $BeginArrow
Invoke-Expression $Expression
}
# Set Line End Arrow Properties
If ($EndArrow)
{
$Expression = '$Script:PolyLine' + $Script:PolyLine + '.Cells("EndArrow").Formula = ' + $EndArrow
Invoke-Expression $Expression
}
}
Aktualna wersja powyższego kodu znajduje się w repozytorium Git pod tym linkiem: PSVisio.ps1.
MsSCVMM2k7ArchDiagramV1.ps1
Odpowiednio, kod tworzenia diagramu architektury systemu Microsoft System Center Virtual Machine Manager 2007 w formacie Visio :
. .\PSVisio.ps1
# Step 1.
# Create Visio Application
# Create Document from Blank Template
# Set Active Page
New-VisioApplication
New-VisioDocument
Set-VisioPage
# Step 2.
# Add Basic Visio Stensils
# Set Masters Item Rectangle
Add-VisioStensil -Name "Basic" -File "BASIC_M.vss"
Set-VisioStensilMasterItem -Stensil Basic -Item "Rectangle"
# Step 3.
# Draw Main Rectangle, Set Size, Set Colour
# Set Text, Size, Color, Align
# Draw Line, Set Weight, Color
Draw-VisioItem -Master "Rectangle" -X 6.375 -Y 7.125 -Width 12.2501 -Height 7.25 -FillForegnd "RGB(0,153,204)"`
-LinePattern 0 -Text "Microsoft Virtual Machine Manager Architecture" -VerticalAlign '0' -ParaHorzAlign '0'`
-CharSize "20 pt" -CharColor "RGB(255,255,255)"
Draw-VisioLine -BeginX 0.3125 -BeginY 10.3438 -EndX 12.4948 -EndY 10.3438 -LineWeight "1 pt" -LineColor "RGB(255,255,255)"
# Step 4.
# Draw Client Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 1.7656 -Y 9.2344 -Width 2.7813 -Height 1.9688 -FillForegnd "RGB(209,235,241)"`
-LinePattern 1 -Text "Client" -VerticalAlign '0' -ParaHorzAlign '1'`
-CharSize "14 pt" -CharColor "RGB(0,0,0)"
Draw-VisioLine -BeginX 0.4297 -BeginY 9.9427 -EndX 3.0833 -EndY 9.9427 -LineWeight "0.5 pt" -LineColor "RGB(0,0,0)"
# Step 5.
# Add Computer Items Visio Stensils
# Set Masters item PC
Add-VisioStensil -Name "Computer" -File "COMPS_M.vss"
Set-VisioStensilMasterItem -Stensil "Computer" -Item "PC"
# Step 6.
# Draw item PC
# Set Text, Size
Draw-VisioItem -Master "PC" -X 1.1173 -Y 9.1693 -Text "Administrator Console" -CharSize "10 pt"
# Step 7.
# Draw Powershell Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\powershell.png" -Width 0.9843 -Height 0.9843 -PinX 2.5547 -PinY 9.2682 `
-Text "Windows Powershell" -CharSize "10 pt"
# Step 8.
# Draw WCF Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 4.2682 -Y 6.9063 -Width 1.5 -Height 6.625 -FillForegnd "RGB(255,255,255)"`
-LinePattern 1 -Text "Windows Communication Foundation" -VerticalAlign '0'`
-CharSize "14 pt"
Draw-VisioLine -BeginX 3.5664 -BeginY 9.4063 -EndX 4.9492 -EndY 9.4063 -LineWeight "0.5 pt" -LineColor "RGB(0,0,0)"
# Step 9.----
# Draw WCF Icon
# Set Position, Size
Draw-VisioIcon -IconPath "c:\!\WCF.png" -Width 1.412 -Height 1.0443 -PinX 4.2708 -PinY 6.9779
# Step 10.
# Draw Line communication from Client to WCF
# Set Arrow
Draw-VisioLine -BeginX 3.1563 -BeginY 9.2396 -EndX 3.5174 -EndY 9.2396 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 11.
# Draw Web Client Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 1.7656 -Y 6.8776 -Width 2.7813 -Height 2.1094 -FillForegnd "RGB(209,235,241)"`
-LinePattern 1 -Text "Web Client" -VerticalAlign '0'`
-CharSize "14 pt"
Draw-VisioLine -BeginX 0.4297 -BeginY 7.6562 -EndX 3.0833 -EndY 7.6562 -LineWeight "0.5 pt" -LineColor "RGB(0,0,0)"
# Step 12.
# Draw Self Service Portal Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\SelfServicePortal.png" -Width 0.8438 -Height 0.8438 -PinX 1.1094 -PinY 6.9271 `
-Text "Self Service Portal" -CharSize "10 pt"
# Step 13.
# Draw Powershell Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\powershell.png" -Width 0.9843 -Height 0.9843 -PinX 2.4922 -PinY 6.9192 `
-Text "Windows Powershell" -CharSize "10 pt"
# Step 14.
# Draw Line communication from Web Client to WCF
# Set Arrow
Draw-VisioLine -BeginX 3.158 -BeginY 6.8802 -EndX 3.5191 -EndY 6.8802 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 15.
# Draw Scripting Client Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 1.7657 -Y 4.5469 -Width 2.7813 -Height 1.9062 -FillForegnd "RGB(209,235,241)"`
-LinePattern 1 -Text "Scripting Client" -VerticalAlign '0'`
-CharSize "14 pt"
Draw-VisioLine -BeginX 0.4297 -BeginY 5.1979 -EndX 3.0834 -EndY 5.1979 -LineWeight "0.5 pt" -LineColor "RGB(0,0,0)"
# Step 16.
# Draw Powershell Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\powershell.png" -Width 0.9843 -Height 0.9843 -PinX 1.7631 -PinY 4.6015 `
-Text "Windows Powershell" -CharSize "10 pt"
# Step 17.
# Draw Line communication from Scripting Client to WCF
# Set Arrow
Draw-VisioLine -BeginX 3.158 -BeginY 4.5729 -EndX 3.5191 -EndY 4.5729 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 18.
# Draw Microsoft System Center
# Virtual Machine Manager Server 2007 Rectangle
# Set Size, Set Colour
# Set Text, Align
Draw-VisioItem -Master "Rectangle" -X 6.3281 -Y 8.2812 -Width 1.5 -Height 2.8125 -FillForegnd "RGB(255,192,0)"`
-LinePattern 1 -Text "Microsoft System Center Virtual Machine Manager Server 2007" -CharSize "14 pt"
# Step 19.
# Draw Microsoft SQL Server 2005 Rectangle
# Set Size, Set Colour
# Set Text, Align
Draw-VisioItem -Master "Rectangle" -X 6.3281 -Y 4.9219 -Width 1.5 -Height 2.6563 -FillForegnd "RGB(255,192,0)"`
-LinePattern 1 -Text "Microsoft SQL Server 2005" -CharSize "14 pt"
# Step 20.
# Add Server Items Visio Stensils
# Set Masters item Management Server and Database Server
Add-VisioStensil -Name "Server" -File "SERVER_M.vss"
Set-VisioStensilMasterItem -Stensil "Server" -Item "Management server"
Set-VisioStensilMasterItem -Stensil "Server" -Item "Database server"
# Step 21.
# Draw item Management Server
Draw-VisioItem -Master "Management server" -X 5.6043 -Y 9.7421
# Step 22.
# Draw item Management Server
Draw-VisioItem -Master "Database server" -X 5.5832 -Y 6.3046
# Step 23.
# Draw Line communication from WCF to Microsoft System Center
# Virtual Machine Manager Server 2007
# Set Arrow
Draw-VisioLine -BeginX 5.0052 -BeginY 8.2604 -EndX 5.6024 -EndY 8.2604 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 24.
# Draw Line communication from Microsoft System Center
# Virtual Machine Manager Server 2007 to Microsoft SQL Server 2005
# Set Arrow
Draw-VisioLine -BeginX 6.3272 -BeginY 6.2344 -EndX 6.3272 -EndY 6.8802 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 0
# Step 25.
# Draw Win-RM Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 8.4375 -Y 6.9219 -Width 1.5 -Height 6.6563 -FillForegnd "RGB(255,255,255)"`
-LinePattern 1 -Text "Windows Remote Managemet (Win-RM)" -VerticalAlign '0'`
-CharSize "14 pt"
Draw-VisioLine -BeginX 7.75 -BeginY 9.1875 -EndX 9.1328 -EndY 9.1875 -LineWeight "0.5 pt" -LineColor "RGB(0,0,0)"
# Step 26.
# Draw Win-RM Icon
# Set Position, Size
Draw-VisioIcon -IconPath "c:\!\Win-RM.png" -Width 1.1563 -Height 1.1563 -PinX 8.4293 -PinY 7
# Step 27.
# Draw Line communication from Win-RM to Microsoft System Center
# Virtual Machine Manager Server 2007
# Set Arrow
Draw-VisioLine -BeginX 7.0781 -BeginY 8.2812 -EndX 7.6753 -EndY 8.2812 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 28.
# Draw P2V Source Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 10.9844 -Y 9.5 -Width 2.7813 -Height 1.5 -FillForegnd "RGB(209,235,241)"`
-LinePattern 1 -Text "P2V Source" -VerticalAlign '0'`
-CharSize "14 pt"
Draw-VisioLine -BeginX 9.6485 -BeginY 9.9739 -EndX 12.3021 -EndY 9.9739 -LineWeight "0.5 pt" -LineColor "RGB(0,0,0)"
# Step 29.
# Draw VMM Agent Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\VMMAgent.png" -Width 0.8438 -Height 0.8438 -PinX 10.2969 -PinY 9.5 `
-Text "VMM Agent" -CharSize "10 pt"
# Step 30.
# Draw Powershell Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\powershell.png" -Width 0.7968 -Height 0.7968 -PinX 11.6016 -PinY 9.6016 `
-Text "Windows Powershell" -CharSize "10 pt"
# Step 31.
# Draw Line communication from P2V Source to Win-RM
# Set Arrow
Draw-VisioLine -BeginX 9.1875 -BeginY 9.487 -EndX 9.599 -EndY 9.487 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 32.
# Draw Host Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 10.9792 -Y 6.9427 -Width 2.7813 -Height 1.5 -FillForegnd "RGB(209,235,241)"`
-LinePattern 1 -Text "Host" -VerticalAlign '0'`
-CharSize "14 pt"
Draw-VisioLine -BeginX 9.6472 -BeginY 7.4219 -EndX 12.3008 -EndY 7.4219 -LineWeight "0.5 pt" -LineColor "RGB(0,0,0)"
# Step 33.
# Draw VMM Agent Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\VMMAgent.png" -Width 0.8438 -Height 0.8438 -PinX 10.2917 -PinY 6.9427 `
-Text "VMM Agent" -CharSize "10 pt"
# Step 34.
# Draw Microsoft Virtual Server 2005 R2 Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\WS.jpg" -Width 0.8125 -Height 0.7404 -PinX 11.5938 -PinY 6.9423 `
-Text "Microsoft Virtual Server 2005 R2" -CharSize "7 pt"
# Step 35.
# Draw Line communication from Host to Win-RM
# Set Arrow
Draw-VisioLine -BeginX 9.1849 -BeginY 6.941 -EndX 9.5964 -EndY 6.941 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 36.
# Draw Library Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 10.9792 -Y 4.3542 -Width 2.7813 -Height 1.5 -FillForegnd "RGB(209,235,241)"`
-LinePattern 1 -Text "Library" -VerticalAlign '0'`
-CharSize "14 pt"
Draw-VisioLine -BeginX 9.6419 -BeginY 4.8073 -EndX 12.2955 -EndY 4.8073 -LineWeight "0.5 pt" -LineColor "RGB(0,0,0)"
# Step 37.
# Draw VMM Agent Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\VMMAgent.png" -Width 0.8438 -Height 0.8438 -PinX 10.2917 -PinY 4.3542 `
-Text "VMM Agent" -CharSize "10 pt"
# Step 38.
# Draw Windows Server 2003 R2 Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\WS.jpg" -Width 0.8125 -Height 0.7404 -PinX 11.5625 -PinY 4.3702 `
-Text "Windows Server 2003 R2" -CharSize "7 pt"
# Step 39.
# Draw Line communication from Library to Win-RM
# Set Arrow
Draw-VisioLine -BeginX 9.1901 -BeginY 4.3611 -EndX 9.6016 -EndY 4.3611 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 40.
# Draw BITS Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 10.9844 -Y 8.2292 -Width 2.75 -Height 0.5625 -FillForegnd "RGB(255,255,0)"`
-LinePattern 1 -Text "BITS" -ParaHorzAlign '0'`
-CharSize "14 pt"
# Step 41.
# Draw BITS Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\BITS.jpg" -Width 0.5417 -Height 0.5417 -PinX 10.974 -PinY 8.2344
# Step 42.
# Draw Line communication from BITS to P2V Source
# Set Arrow
Draw-VisioLine -BeginX 10.9844 -BeginY 8.5104 -EndX 10.9844 -EndY 8.75 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 43.
# Draw Line communication from BITS to Host
# Set Arrow
Draw-VisioLine -BeginX 10.9844 -BeginY 7.6849 -EndX 10.9844 -EndY 7.9219 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 44.
# Draw BITS Rectangle, Set Size, Set Colour
# Set Text, Align
# Draw Line
Draw-VisioItem -Master "Rectangle" -X 10.9844 -Y 5.6555 -Width 2.75 -Height 0.5625 -FillForegnd "RGB(255,255,0)"`
-LinePattern 1 -Text "BITS" -ParaHorzAlign '0'`
-CharSize "14 pt"
# Step 45.
# Draw BITS Icon
# Set Position, Size
# Set Text
Draw-VisioIcon -IconPath "c:\!\BITS.jpg" -Width 0.5417 -Height 0.5417 -PinX 10.974 -PinY 5.6607
# Step 46.
# Draw Line communication from BITS to Host
# Set Arrow
Draw-VisioLine -BeginX 10.9844 -BeginY 5.9245 -EndX 10.9844 -EndY 6.1901 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 47.
# Draw Line communication from BITS to Library
# Set Arrow
Draw-VisioLine -BeginX 10.9792 -BeginY 5.1042 -EndX 10.9792 -EndY 5.375 -LineColor "RGB(255,255,255)" -BeginArrow 4 -EndArrow 4
# Step 48.
# Resise Page To Fit Contents
Resize-VisioPageToFitContents
# Step 49.
# Save Document
# And Quit Application
Save-VisioDocument -File 'C:\!\MsSCVMM2007Arch.vsd'
Close-VisioApplication
Aktualna wersja powyższego kodu znajduje się w repozytorium Git pod tym linkiem: MsSCVMM2k7ArchDiagramV1.ps1.
Короткий Підсумок.
Отже фінальний результат повністю аналогічний описаному в попередніх публікаціях, проте оптимізація за допомогою функцій дозволила скоротити об’єм коду з 516 рядків до 326. Код став більш оптимальним і читабельним. Позитивним фактором також являється стратегія повторного використання коду.
Проте залишаються ще задачі до реалізації: функції мають мати код перевірки вхідних параметрів на коректність, і додати і розширити алгоритми роботи із більш складними фігурами.
Але про це вже в наступних публікаціях.
До зустрічі.
З повагою, AIRRA!