我从我编写的模块(https://github.com/joxz/PSEXOS)中的函数(Invoke-EXOScommand)中获得了一些奇怪的行为。该模块旨在通过JSONRPC连接到我的网络交换机。
错误和工作场景都在json的基础上:https://gist.github.com/joxz/195d6ec6d211f6e00421cf13436411fd
到目前为止一切顺利,我将JSON作为响应并将其转换为ConvertFrom-Json转换为Powershell对象。
{
"id": "10",
"jsonrpc": "2.0",
"result": [
{
"CLIoutput": "Port Link Tx Pkt Tx Byte Rx Pkt Rx Byte Rx Pkt Rx Pkt Tx Pkt
Tx Pkt\n State Count Count Count Count Bcast Mcast Bcast M
cast\n========= ===== =========== =========== =========== =========== =========== =========== =========== ===========\n1
R 0 0 0 0 0 0 0 0\n========
= ===== =========== =========== =========== =========== =========== =========== =========== ===========\n > in
Port indicates Port Display Name truncated past 8 characters\n > in Count indicates value exceeds column width.
Use 'wide' option or '0' to clear.\n Link State: A-Active, R-Ready, NP-Port Not Present, L-Loopback\n"
},
{
"show_ports_stats": {
"dot1dTpPortInDiscards": 0,
"dot1dTpPortInFrames": 0,
"dot1dTpPortMaxInfo": 1500,
"dot1dTpPortOutFrames": 0,
"linkState": 0,
"port": 1,
"portList": 1,
"portNoSnmp": 1,
"rxBcast": 0,
"rxByteCnt": 0,
"rxMcast": 0,
"rxPktCnt": 0,
"txBcast": 0,
"txByteCnt": 0,
"txMcast": 0,
"txPktCnt": 0
},
"status": "SUCCESS"
}
]
}
当我将函数复制/粘贴到Powershell窗口时,它按预期工作:
VERBOSE:
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CLIoutput NoteProperty string CLIoutput=Port Link Tx Pkt Tx Byte Rx Pkt Rx Byte Rx
Pk...
Port Link Tx Pkt Tx Byte Rx Pkt Rx Byte Rx Pkt Rx Pkt Tx Pkt Tx Pkt
State Count Count Count Count Bcast Mcast Bcast Mcast
========= ===== =========== =========== =========== =========== =========== =========== =========== ===========
当我安装我的模块并调用该函数时,我得到一个解析错误:
VERBOSE:
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CLIoutput NoteProperty System.String CLIoutput=Port Link Tx Pkt Tx Byte Rx Pkt Rx Byte
...
Property 'CLIoutput' cannot be found on this object. Make sure that it exists.
At C:\maintenance\PSEXOS\Functions\Invoke-EXOScommand.ps1:114 char:9
+ $clioutput = $responseobj.result.CLIoutput
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
在这两种情况下,我都将'CLIoutput'作为NoteProperty,那么为什么从模块调用时它不被识别?这是范围问题吗?所有函数都是.psm1文件中的点源。
模块中的其他两个功能(Get-Vlans,Get-VlanPortInfo)也是以一种熟悉的方式设计并且可以工作......
答案 0 :(得分:1)
查看您的JSON:https://graph.facebook.com/10153608167431961?fields=likes.limit(0).summary(true)?access_token=EAANep****
属性是一个数组,其中第一个对象具有result
属性,而第二个对象没有。CLIoutput
属性。引用不存在的属性会导致严格模式(Set-StrictMode -Version Latest
文件中的PSEXOS.psm1
行)出错。我的猜测是你忘记将这一行添加到交互式PowerShell会话中,因此它不是以严格模式运行,而是仅为现有属性返回$null
。看起来你只需要引用result
数组的第一个元素:
$clioutput = $responseobj.result[0].CLIoutput