【发布时间】:2020-07-24 08:51:59
【问题描述】:
我目前正在尝试学习 powershell。我有一些代码通过一些 .jpg 图像,将这些图像发送到 exiftool 并将它们的 exif 信息输出到 json 文件。然后我使用 Get-Content 将 Json 文件带入 powershell,然后使用 ConvertFrom-Json 将其转换为 PSObject。我无法理解如何从 PSObject 访问我需要的数据。例如,我想获取图像的文件名及其曝光设置。我试过 psobject.properties.Name ,它只显示 PSObject 的属性,而不是其中的实际对象。然后我尝试了 Get-Member 及其一些属性,但无济于事。我相信 Get-Member 是我需要走的路线,但我不完全确定如何使用它。
这是一个非常简洁的Json示例:
[
"[{",
" \"FileName\": \"DJI_0001.JPG\",",
" \"ExposureTime\": 0.005,",
"},",
"{",
" \"FileName\": \"DJI_0002.JPG\",",
" \"ExposureTime\": 0.003125,",
"},",
"{",
" \"FileName\": \"DJI_0004.JPG\",",
" \"ExposureTime\": 0.00625,",
"}]"
]
这是我尝试过的一些代码:
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Write-Host "Procesing Files..."
$command = $scriptDir + '\exiftool.exe ' + $directory + ' -json -n -r -EXT JPG'
$allmetadata = Invoke-Expression $command
#Write-Host $allmetadata
$allmetadata | ConvertTo-Json | Out-File "$scriptDir\JsonOut.json"
#########################################################################################################
#
# This Gets the Json File and converts it to a PSObject
#
#########################################################################################################
$convertedmeta = Get-Content "$scriptDir/JsonOut.json" -Raw -Encoding UTF8 | ConvertFrom-Json
Write-Host $convertedmeta
#########################################################################################################
#
# This is code where I was trying to explore the information within the PSObject trying to figure out
# where the FileName and ExpsoureTime are kept so I can call the values
#
#########################################################################################################
Write-Host "Exploring JSON File"
#$jsonInfoExplore = $convertedmeta.PsObject.Properties.Value.PsObject.Properties.Name
#$jsonInfoExplore = $convertedmeta.psobject.properties.Name
#$jsonInfoExplore = $convertedmeta.psobject.properties.Value
$jsonInfoExplore = $convertedmeta | Get-Member
Write-Host $jsonInfoExplore
正如您所看到的代码被注释掉的地方,我尝试了几种不同的方法来获取我正在寻找的信息。我只是不完全了解如何从 PSObject 调用数据。
我们将不胜感激任何帮助和指导。
【问题讨论】:
标签: json powershell powershell-4.0 exiftool