简体   繁体   English

PowerShell:检查 WIA(COM?)object 是否具有特定属性

[英]PowerShell: check if WIA (COM?) object has a specific Property

I'm still relatively new to PowerShell. This is a follow up to Use PowerShell to extract GPS Latitude etc. (Properties > Details) from an image file and using code like this (from @stackprotector):我对 PowerShell 还是比较陌生。这是使用 PowerShell 从图像文件中提取 GPS 纬度等(属性 > 详细信息)并使用这样的代码(来自@stackprotector)的跟进:

# Create an ImageFile object and load an image file
$image = New-Object -ComObject Wia.ImageFile
$image.LoadFile("C:\Absolute\path\to\my.jpg")

# Read your desirered metadata
$image.Properties.Item('GpsLatitude').Value

As some images have no GPS data, this gets an error.由于某些图像没有 GPS 数据,因此会出现错误。 How can I check if Properties.Item('GpsLatitude') actually exists in that file, before trying to extract it?在尝试提取文件之前,如何检查Properties.Item('GpsLatitude')是否确实存在于该文件中?

Thank you.谢谢你。

For this specific purpose, you can use the Properties.Exists method :为此特定目的,您可以使用Properties.Exists 方法

if ($image.Properties.Exists('GpsLatitude')) {
    $image.Properties.Item('GpsLatitude').Value
}

In general, you can always use exception handling .通常,您始终可以使用异常处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM