问题描述:

Hi i am trying to generate visio drwaings using powershell but getting the following error;

PS C:\Users\XXXX\Desktop\Scripts> C:\Users\XXXX\Desktop\Scripts\tempv.ps1

You cannot call a method on a null-valued expression.

At C:\Users\XXXX\Desktop\Scripts\tempv.ps1:14 char:1

+ $pc = $ComputerStencil.Masters.Page("PC")

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (:) [], RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

PS C:\Users\XXXX\Desktop\Scripts>

Please tell me whats wrong here ; i tried lot but couldnt get whats wrong;

$application = New-Object -ComObject Visio.Application

#$application.visible = $false

$documents = $application.Documents

$document = $documents.Add("Basic Network Diagram.vst")

$pages = $application.ActiveDocument.Pages

$page = $pages.Item(1)

$NetworkStencil = $documents.Add("periph_m.vss")

$ComputerStencil = $documents.Add("Computers and Monitors.vss")

$ConnectorStencil = $documents.Add("Connectors.vss")

$pcinfo = Get-ComputerSystem -computer $computer

#***********************WORKS UNTILL HERE**************

#*****ERROR PART***************

$pc = $ComputerStencil.Masters.Page("PC")

$shape1 = $page.Drop($pc, 2.2, 6.8)

$shape1.Text = "$($pcinfo.DNSHostName)`r`n$($pcinfo.Domain)"

网友答案:

Page is not a property of Masters. You need to get the PC Master from the collection using the Item property:

$pc = $ComputerStencil.Masters.Item("PC")
网友答案:

Either $ComputerStencil or $ComputerStencil.Masters is $null. You could just dot-source the script and poke around the variables and objects at the command line (sometimes easier than using the debugger in the ISE).

相关阅读:
Top