拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 在这种情况下,我如何将PowerShellSystem.Object[]转换为字符串值...?

在这种情况下,我如何将PowerShellSystem.Object[]转换为字符串值...?

白鹭 - 2022-03-17 2073 0 0

问候地球人,

我正在寻找有关我当前面临的挑战的一些指导。我在某种程度上是 PowerShell 的中级用户,但是,我的知识存在一些差距。我不是在寻找一个完整的解决方案,只是一些指导。我想非常清楚这一点,因为似乎有些人希望有人为他们撰写所有代码,或者面临假设,事实并非如此。

我想要的结果是获取 AWS 账户中每个正在运行的实体的缺失补丁信息,并将该资料放入 Excel 作业簿中,每个实体都有自己的作业表,其中包括与键匹配的标题和显示每个值的相应列JSON 档案。我已经尝试了多种方法来实作这一点,但我遇到了一些挑战。任何帮助/指导将不胜感激。同样,我不是在找人为我撰写所有代码。我正在努力学习如何钓鱼,而不是 WHO 可以为我钓鱼——尽管在创业世界中,找到 WHO 是成功的关键。

到目前为止我的代码:

$instance_source = "C:\missing-patches\instance_source.txt"

# Get running instanceIds and write them to a source text file.
aws ec2 describe-instances --profile $PROFILE --query "Reservations[*].Instances[*].{InstanceId: InstanceId}" --filters "Name=instance-state-name,Values=running" --output text | Out-File $instance_source

# Get content from source text file, then query each AWS instance for missing patches, then write to file
foreach($instance_id in Get-Content $instance_source){
    $writeToJsonFile = "C:\missing-patches\running-instances\$instance_id.json"
    aws --profile $PROFILE ssm describe-instance-patches --instance-id  $instance_id --filters Key=State,Values=Missing --output json | Out-File $writeToJsonFile
}

# Convert each JSON in the directory and import to an Excel workbook/worksheet
$pathToJsonFiles = Get-ChildItem "C:\missing-patches\running-instances\"

foreach($json_file in $pathToJsonFiles){
    Get-Content $pathToJsonFiles -Raw | ConvertFrom-Json | Export-Excel .\missing-patches.xlsx -WorksheetName $json_file.BaseName
}

Remove-Item $instance_source

一个有效 JSON 档案的内容:

{
    "Patches": [
        {
            "Title": "2020-10 Cumulative Update for .NET Framework 4.8 for Windows Server 2016 for x64 (KB4578969)",
            "KBId": "KB4578969",
            "Classification": "SecurityUpdates",
            "Severity": "Important",
            "State": "Missing",
            "InstalledTime": "1969-12-31T19:00:00-05:00"
        },
        {
            "Title": "Security Update for Windows Server 2016 for x64-based Systems (KB4535680)",
            "KBId": "KB4535680",
            "Classification": "SecurityUpdates",
            "Severity": "Critical",
            "State": "Missing",
            "InstalledTime": "1969-12-31T19:00:00-05:00"
        },
        {
            "Title": "2021-09 Servicing Stack Update for Windows Server 2016 for x64-based Systems (KB5005698)",
            "KBId": "KB5005698",
            "Classification": "SecurityUpdates",
            "Severity": "Critical",
            "State": "Missing",
            "InstalledTime": "1969-12-31T19:00:00-05:00"
        }
    ]
}

.xlsx 中的结果显示的是物件而不是物件的值,一个字符串:

"Patches"
"System.Object[]"
"System.Object[]"
"System.Object[]"

我知道还有更多内容,所以请耐心等待并理解我对编码/PowerShell 的了解并不多;我通过经验而不是理论来学习。对人好点。一些回应者认为你应该知道一切或责备你,因为你不知道。提前致谢!

uj5u.com热心网友回复:

你需要挖1项更深层次地输出的内容中的Patches属性,而不是拥有它的父物件:

Get-Content $pathToJsonFiles -Raw | ConvertFrom-Json |ForEach-Object -MemberName Patches | Export-Excel .\missing-patches.xlsx -WorksheetName $json_file.BaseName

呼叫ForEach-Object Patches将只从输入物件中提取该属性的值(类似于Select-Object -ExpandProperty

uj5u.com热心网友回复:

对于那些需要类似的东西,这是我使用的完整代码。随意编辑您的需求:

# Get running instanceIds and write them to a source text file.
$instance_source = "C:\missing-patches\instance_source.txt"
aws ec2 describe-instances --profile $PROFILE --query "Reservations[*].Instances[*].{InstanceId: InstanceId}" --filters "Name=instance-state-name,Values=running" --output text | Out-File $instance_source

# Get content from source text file, then query each AWS instance for missing patches, then write to file
foreach($instance_id in Get-Content $instance_source){

    $writeToJsonFile = "C:\missing-patches\running-instances\$instance_id.json"
    aws --profile $PROFILE ssm describe-instance-patches --instance-id  $instance_id --filters Key=State,Values=Missing --output json | Out-File $writeToJsonFile

}

#Convert each JSON in the directory and import to an Excel workbook/worksheet
foreach($json_file in $pathToJsonFiles){
    
    $pathToJsonFiles = Get-ChildItem "C:\missing-patches\running-instances\"
    Get-Content $json_file -Raw | ConvertFrom-Json | ForEach-Object -MemberName Patches | Export-Excel -Path .\missing-patches.xlsx -WorksheetName $json_file.BaseName -AutoSize

}

Remove-Item $instance_source
Remove-Item "C:\missing-patches\running-instances\*.json"
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *