通过Powershell脚本更改Visio形状的颜色



我正试图通过Powershell和Visio自动创建网络图,但遇到了障碍。我想更改Cloud(内置Visio形状(的颜色,但由于某些原因,颜色没有更改。你能帮我解决这个问题吗?(参见下面的代码(

Import-Module VisioBot3000 -Force
# Variables
$Username = $env:USERNAME
#set_x = 1
#set_y = 1
#Servers = Read-Host -Prompt "How many routers do you have?
# Opens the Visio application
New-VisioApplication
# Opens the Visio Document
Open-VisioDocument
# Register the network shapes stencil
$PERouterStencil = Register-VisioStencil -Name NetworkSymbols -Path 'C:Program Files...NETSYM_U.vssx'
$VisioStencil2 = Register-VisioStencil -Name NetworkLocations -Path 'C:Program Files...NETLOC_U.vssx'
#Register the router shape
$PE_Router_Shape = Register-VisioShape -Name Router -StencilName Network Symbols -MasterName Router
$Cloud_Register = Register-VisioShape -Name Cloud -StencilName NetworkLocations -MasterName Cloud
#Creating the Cloud
$Cloud = New-VisioShape Cloud -Label Cloud -x 5.6443 -y 4.481
#Width of the Cloud
$Cloud_Width = $Cloud.CellsU('Width').FormulaU = '3 in'
$Cloud_Height = $Cloud.CellsU('Height').FormulaU =  '1.89 in'
$Cloud.CellsU('FillForegnd').FormulaForceU = '=RGB(255,0,0)'
$Cloud.CellsU('FillBkgnd').FormulaForceU= 'THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME("FillColor"),THEME("FillColor2"))))'

下面是一个设置颜色的函数。Visio形状很复杂,包括子形状和渐变着色。此函数将所有形状(和子形状(设置为该颜色。

<#
.SYNOPSIS
Sets the color of a shape
.DESCRIPTION
Sets the color of a shape and subshapes to a given color
.EXAMPLE
$shape= add-visioshape -stencil BasicShapes -name Square
Set-ShapeColor -shape $shape -color Red
.INPUTS
You cannot pipe any objects into Set-ShapeColor
.OUTPUTS
None
.PARAMETER Shape
The shape to apply the color to
.PARAMETER Color
The color you want the shape to be
#>
function Set-VisioShapeColor {
[CmdletBinding()]
Param($Shape,
[System.Drawing.Color]$Color,
[ValidateSet('FillForegnd', 'FillBkgnd')]$FillType = 'FillBkgnd'
)
$ColorFormula = "=THEMEGUARD(rgb($($Color.R),$($Color.G),$($Color.B)))"

$shape.CellsU($fillType).FormulaForce = $ColorFormula
$shape.CellsU('FillGradientEnabled').FormulaForce = 'FALSE'
$shape.CellsU('FillPattern').FormulaForce = '1'
$shape.shapes | foreach-object {
$_.CellsU('FillGradientEnabled').FormulaForce = 'FALSE'
$_.CellsU($fillType).FormulaForce = $colorFormula 
}
}

最新更新