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

Changing the Color of a Visio Shape via Powershell Script

我正在尝试通过 Powershell 和 Visio 自动创建网络图,但是我遇到了障碍。我想更改云(内置 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 
    }
}