无法获取Item的字符串属性并将其转换为Int

时间:2019-03-29 21:02:28

标签: powershell

public class SourceParent
{
    public int Id { get; set; }
    public List<SourceChild> Childs { get; set; }
}

public class SourceChild
{
    public string OtherProperty { get; set; }
    public int ChildId { get; set; }
}

public class Destination
{
    public int SourceParentId { get; set; }
    public string OtherProperty { get; set; }
    public int ChildId { get; set; }
}

我正在尝试将名为“ Breedte”(英语中的“ Width”)和“ Hoogte”(英语中的“ Height”)的属性的字符串值转换为Integers,以便进行比较。但是,当我尝试转换它们时,它们什么也不返回。奇怪的是,它确实可以使用相同的方法来处理'Grootte'属性(英文为Size)。

预先感谢

这是我的Powershell脚本:

# Solution by @BACON (I changed the following lines in the code)

[convert]::ToInt32(($_.$widthProperty.split(' ')[0] -replace [char]8206), 10)
[convert]::ToInt32(($_.$heightProperty.split(' ')[0] -replace [char]8206), 10)

这是我在此脚本中使用的功能Get-FileMetaData:

$threshold = 160 # size in kB

# Config for Property names (Translations)
$nameProperty = 'Naam';
$widthProperty = 'Breedte';
$heightProperty = 'Hoogte';
$sizeProperty = 'Grootte';

Clear-Host

# Get all files
Get-FileMetaData 'C:\Users\Anton\Pictures\StackOverflow' | Select-Object -Property Breedte, Hoogte, Grootte, @{
        Name = 'Name'
        Expression = {
            $_.$nameProperty
        }}, @{
        Name = 'Width'
        Expression = {
            [convert]::ToInt32($_.$widthProperty.split(' ')[0], 10)
        }}, @{
        Name = 'Height'
        Expression = {
            [convert]::ToInt32($_.$heightProperty.split(' ')[0], 10)
        }}, @{
        Name = 'Size'
        Expression = {
            [convert]::ToInt32($_.$sizeProperty.split(' ')[0], 10)
        }}

这是我使用的属性的列表,您可以看到Width和Height没有值。

# ----------------------------------------------------------------------------- 
# Script: Get-FileMetaDataReturnObject.ps1 
# Author: ed wilson, msft 
# Date: 01/24/2014 12:30:18 
# Keywords: Metadata, Storage, Files 
# comments: Uses the Shell.Application object to get file metadata 
# Gets all the metadata and returns a custom PSObject 
# it is a bit slow right now, because I need to check all 266 fields 
# for each file, and then create a custom object and emit it. 
# If used, use a variable to store the returned objects before attempting 
# to do any sorting, filtering, and formatting of the output. 
# To do a recursive lookup of all metadata on all files, use this type 
# of syntax to call the function: 
# Get-FileMetaData -folder (gci e:\music -Recurse -Directory).FullName 
# note: this MUST point to a folder, and not to a file. 
# ----------------------------------------------------------------------------- 
Function global:Get-FileMetaData 
{ 
  <# 
   .Synopsis 
    This function gets file metadata and returns it as a custom PS Object  
   .Description 
    This function gets file metadata using the Shell.Application object and 
    returns a custom PSObject object that can be sorted, filtered or otherwise 
    manipulated. 
   .Example 
    Get-FileMetaData -folder "e:\music" 
    Gets file metadata for all files in the e:\music directory 
   .Example 
    Get-FileMetaData -folder (gci e:\music -Recurse -Directory).FullName 
    This example uses the Get-ChildItem cmdlet to do a recursive lookup of  
    all directories in the e:\music folder and then it goes through and gets 
    all of the file metada for all the files in the directories and in the  
    subdirectories.   
   .Example 
    Get-FileMetaData -folder "c:\fso","E:\music\Big Boi" 
    Gets file metadata from files in both the c:\fso directory and the 
    e:\music\big boi directory. 
   .Example 
    $meta = Get-FileMetaData -folder "E:\music" 
    This example gets file metadata from all files in the root of the 
    e:\music directory and stores the returned custom objects in a $meta  
    variable for later processing and manipulation. 
   .Parameter Folder 
    The folder that is parsed for files  
   .Notes 
    NAME:  Get-FileMetaData 
    AUTHOR: ed wilson, msft 
    LASTEDIT: 01/24/2014 14:08:24 
    KEYWORDS: Storage, Files, Metadata 
    HSG: HSG-2-5-14 
   .Link 
     Http://www.ScriptingGuys.com 
 #Requires -Version 2.0 
 #> 
 Param([string[]]$folder) 
 foreach($sFolder in $folder) 
  { 
   $a = 0 
   $objShell = New-Object -ComObject Shell.Application 
   $objFolder = $objShell.namespace($sFolder) 

   foreach ($File in $objFolder.items()) 
    {  
     $FileMetaData = New-Object PSOBJECT 
      for ($a ; $a  -le 266; $a++) 
       {  
         if($objFolder.getDetailsOf($File, $a)) 
           { 
             $hash += @{$($objFolder.getDetailsOf($objFolder.items, $a))  = 
                   $($objFolder.getDetailsOf($File, $a)) } 
            $FileMetaData | Add-Member $hash 
            $hash.clear()  
           } #end if 
       } #end for  
     $a=0 
     $FileMetaData 
    } #end foreach $file 
  } #end foreach $sfolder 
} #end Get-FileMetaData

0 个答案:

没有答案