【问题标题】:PowerShell Passing PSCUSTOM object to jsonPowerShell 将 PSCUSTOM 对象传递给 json
【发布时间】:2021-04-01 16:14:48
【问题描述】:

我认为这与它通过 azure 自动化有关...

这是我正在做的事情:

$tBody = @{
    'caller'=$caller # this is just a string 'something@something.com'
    'pscustomobject' = $pscustomobject # this is a pscustom object
    }        
$passToAction = [pscustomobject]@{
    paramblock = $paramBlock # this is a pscustom object and one of the objects is another pscustomobject called pscustomobject which is a pscustomobject that was convertto-csv -notypeinformation converted
}
$passToActionJSON = $passToAction| ConvertTo-Json -Depth 99

另一方面,长话短说......我尽我所能做 converfrom-json。但我最终得到一个看起来像这样的字符串:

$b.paramblock.pscustomobject
@{TimeGenerated=2020-12-22T23:22:17.527Z; protocol=Kerberos; AuthServer=stuff; IpAddress=suff; NonExistentUser=stuff; TargetDomainName=
stuff; vmName=stuff}
@{TimeGenerated=2020-12-22T23:22:17.527Z; protocol=Kerberos; AuthServer=stuff; IpAddress=suff; NonExistentUser=stuff; TargetDomainName=
stuff; vmName=stuff}
@{TimeGenerated=2020-12-22T23:22:17.527Z; protocol=Kerberos; AuthServer=stuff; IpAddress=suff; NonExistentUser=stuff; TargetDomainName=
stuff; vmName=stuff}

有人知道如何将其转变成可用的东西吗?我真正想要的是它回到一个 pscustomobject 中。如果您有更好的方法来发送它,但我在 azure 自动化中工作,所以不承诺任何 .net 类在那里。

【问题讨论】:

  • 相当混乱...$b 是什么? $tbody 去哪儿了?尝试在您的问题中构建minimal reproducible example。另见:how to ask a good question
  • 无论如何要了解您的 PowerShell 表达式应该如何构建您的特定 json 字符串,试试这个$Object = ConvertFrom-Json $Json (-AsHashTable) 而不是ConvertTo-Expression $Object
  • 没有显示所有未定义变量的来源以及它们应该包含的内容,我非常怀疑任何人都能够给你一个答案..
  • (同意上面的cmets)..什么是$caller变量类型和值? $pscustomobject 变量类型和值是什么? $paramBlock 变量类型和值是什么?您的“另一面”看起来像 [PSCustomObject] 数组,但您的示例显示没有使用 TimeGeneratedprotocolAuthServerIpAddressvmName 属性分配或声明。

标签: .net azure powershell automation


【解决方案1】:

这就是我的工作流程

-> $pscustomobject =
--> something = something
--> something2 = ($pscustomobject | convertto-json)
--> something3 = something3

-> $newpscustomobject =
--> paramblock = $pscustomobject

-> $newpscustomobject | convertto-json -depth 99

这把它变成了普通的垃圾

长话短说,将每个 pscustomobject 保持在两个级别,否则 powershell 会将其提升

        $paramBlock = @()
        $paramBlock = [pscustomobject]@{
            ticket_body = $ticket_body
            ticket_short_description = $subject
            category = 'stuff'
            subcategory = 'Other'
            assignment_group = 'stuff'
            csvname = 'stuff.csv'
            location = 'waco'
        }
        $passToAction = [pscustomobject]@{
            paramblock = $paramBlock # pscustomobject
            csvattachment = $eventsQueryResults #pscustomobject
        }
        $passToActionJson = $passToAction | ConvertTo-Json -Depth 99
        Invoke-AutomationWatcherAction -Message $subject -Data $passToActionJson

【讨论】:

    猜你喜欢
    • 2016-03-30
    • 2016-04-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2017-01-11
    相关资源
    最近更新 更多