如何获取对象的属性?

时间:2017-04-26 17:22:42

标签: powershell

我遇到一个问题,我无法从变量$filearray获取属性。当我运行$filearray.gettype()时,会返回objectsystem.array$filearray设置如下:

Directory                                Name
test                                     test
test2                                    test2

我希望能够将名称和目录作为单独的变量从此列表中提取,然后能够将路径复制到文件并复制到新目标。

function FindFiles {

    param(
        [string]$filestore
    )

    $length = $filestore.Length
    $GuidArray = @()

    for($line=0;$line -le $filestore.Count;$line++){

            $check = $filestore[$line]
            echo $check
            $length2 = $check.Length


            $fileGuid = $check | ForEach-Object{$_.FileGuid}





            $GuidArray = $GuidArray + $fileGuid    
    }

    write-host "-------------------------------------------------------------" -ForegroundColor Yellow

    $filepath = Read-Host " Please Enter File Path to Search"

    for ($counter=0;$counter -lt $GuidArray.Count;$counter++){
        $fileArray = @()
        $guidcheck = $GuidArray[$counter]
        $file = Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -like "*$guidcheck*") } | Select-Object Directory,Name| Format-Table -AutoSize 
        $fileArray += $file
    }

    Write-Output $fileArray
    #for ($counter=0;$counter -lt $fileArray.Count;$counter++){

        #echo $fileArray[$counter]
        #$filetest = $fileArray[$counter] 
        #Copy-Item $filetest -Destination "C:\test"  
    #}
    return $Global:fileArray





}

function CopyFiles {

    param(
        [array]$fileArray
    )
    echo $fileArray

    echo $fileArray.GetType()


    $results = $fileArray | select -ExpandProperty Name
    echo $results

    #echo "hello"
    #for ($counter = 0;$counter -lt $fileArrray.Count;$counter++){
        #echo $fileArray[$counter]
        #$filetest = $fileArray[$counter]


        #Copy-Item 
    #}

}

function execute {
    $filestore = Import-Excel 'C:\594 Sample of Filestore.xlsx'
    echo $filestore

    #FindFiles($filestore)
    $fileArray = @(FindFiles($fileArray))
    echo "test"
    #echo "--------------------------------------------------------------"
    echo $fileArray
    #echo "----------------------------------------------------------------------------------"
    #echo $fileArray.GetType()
    echo "----------------------------------------------------------------------------------"
    CopyFiles($fileArray)

3 个答案:

答案 0 :(得分:1)

由于你已经包含了如此多的代码,我无法准确地显示你需要如何改变它,因为它有太多的东西。

以下是一个简单示例,说明如何在foreach循环中引用Directory的{​​{1}}和Name属性:

$filearray

答案 1 :(得分:0)

查看for函数末尾的FindFiles循环(我添加的换行符):

for ($counter=0;$counter -lt $GuidArray.Count;$counter++){
    $fileArray = @()
    $guidcheck = $GuidArray[$counter]
    $file = Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue | Where-Object { 
        ($_.PSIsContainer -eq $false) -and  ( $_.Name -like "*$guidcheck*") 
    } | Select-Object Directory,Name| Format-Table -AutoSize 
    $fileArray += $file               # ^
}                                     # |
                                      # This right here is your issue 
                                      # Format-Table will turn the output into 
                                      # machine-inspectable formatted text

从该管道中删除Format-Table

您的代码可能仍会遇到一些问题,但至少您现在应该可以$fileArray[0].Name

答案 2 :(得分:0)

通过使用import-excel导入的对象,我可以调用$ filestore.FileGuid或您对象中可能包含的任何参数,并可以动态构建数组。这允许我将$ filestore.fileguid存储在一个新的数组中,比如$ guidArray,我可以使用像$ GuidArray [0]

这样的数组点调用