PS Script is exporting an empty CSVs

前端 未结 3 846
花落未央
花落未央 2020-12-30 18:18

I am having a helluva time trying to understand why this script is not working as intended. It is a simple script in which I am attempting to import a CSV, select a few colu

3条回答
  •  暖寄归人
    2020-12-30 18:35

    As Matt commented, your last $PSItem ($_) not related to the Get-ChildItem anymore but for the Select-Object cmdlet which don't have a FullName Property

    You can use differnt foreach approach:

    $RootPath = "D:\SomeFolder"
    $csvFilePaths = Get-ChildItem $RootPath -Recurse -Include *.csv
    
    foreach ($csv in $csvFilePaths)
    {
    Import-CSV $csv.FullName |
    Select-Object Test_Name,Test_DataName,Device_Model,Device_FW,Data_Avg_ms,Data_StdDev | 
    Export-Csv $csv.FullName -NoType -Force
    }
    

    Or keeping your code, add $CsvPath Variable containing the csv path and use it later on:

    $RootPath = "D:\SomeFolder"
    Get-ChildItem $RootPath -Recurse -Include *.csv | ForEach-Object{
    $CsvPath = $_.FullName
    Import-CSV $CsvPath |
    Select-Object Test_Name,Test_DataName,Device_Model,Device_FW,Data_Avg_ms,Data_StdDev | 
    Export-Csv $CsvPath -NoType -Force
    }
    

提交回复
热议问题