【问题标题】:Powershell Get-Content + ArrayPowershell 获取内容 + 数组
【发布时间】:2019-09-18 13:58:18
【问题描述】:

怎么样了?

我是 Powershell 的新手,我正在尝试简化我的代码,以便我需要在两个文件中执行相同的操作,唯一改变的是文件名和 ReadCount 大小(第一个文件为 15000第二个是 50000)。

当我运行它时,错误显示:

Get-Content : 指定路径的对象 C:\Folder\08_configuration_items 11_CI-Contract-new[0].csv 没有 存在,或已被 -Include 或 -Exclude 参数过滤。在 行:2 字符:7 + $i=0;获取内容 "C:\Folder\$fileArray[$len].csv" -ReadCount $sizeA ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (System.String[]:String[]) [Get-Content], 异常 + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content : 指定路径的对象 C:\Folder\08_configuration_items 11_CI-Contract-new[0]_1.csv 没有 存在,或已被 -Include 或 -Exclude 参数过滤。在 行:3 字符:20 + ... bookContent = Get-Content "C:\Folder\$fileArray[$len]_1.csv" |选择... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (System.String[]:String[]) [Get-Content], 异常 + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand

这是代码,不确定我是否在 Powershell 上使用正确的方式访问数组。

$sizeArray = @(15000,50000)
$fileArray = @("08_configuration_items", "11_CI-Contract-new")
for($len=0; $len -le 1; $len++) {
    $i=0; Get-Content "C:\Folder\$fileArray[$len].csv" -ReadCount $sizeArray[$len] | %{$i++; $_ | Out-File "C:\Folder\$fileArray[$len]_$i.csv" -Encoding "UTF8"}
    $WorkbookContent = Get-Content "C:\Folder\$fileArray[$len]_1.csv" | Select -Index 0
    for($j=2; $j -le $i; $j++) {
        $CurrentFileContent = Get-Content "C:\Folder\$fileArray[$len]_$j.csv"
        @($WorkbookContent, $CurrentFileContent) | Set-Content "C:\Folder\$fileArray[$len]_$j.csv"
    }
}

有什么想法吗?

非常感谢

【问题讨论】:

    标签: arrays powershell file


    【解决方案1】:

    这里的问题在于字符串插值。字符串中的变量名称将向上扩展,直到它到达该名称中的特殊字符。然后它将附加字符串的其余部分和之后的任何插值字符串。在访问字符串中对象的属性时,这通常发生在 . 字符上。一个简单的解决方案是使用子表达式运算符 ($())。

    Get-Content "C:\Folder\$($fileArray[$len]).csv"
    

    另一种方法是用另一种方式构建路径字符串,然后将其传递给命令。下面的方法使用格式运算符(-f)。

    $Path = "C:\Folder\{0}.csv" -f $fileArray[$len]
    Get-Content $Path
    

    添加了子表达式运算符的代码如下所示:

    $sizeArray = @(15000,50000)
    $fileArray = @("08_configuration_items", "11_CI-Contract-new")
    for($len=0; $len -le 1; $len++) {
        $i=0; Get-Content "C:\Folder\$($fileArray[$len]).csv" -ReadCount $sizeArray[$len] | %{$i++; $_ | Out-File "C:\Folder\$($fileArray[$len])_$i.csv" -Encoding "UTF8"}
        $WorkbookContent = Get-Content "C:\Folder\$($fileArray[$len])_1.csv" | Select -Index 0
        for($j=2; $j -le $i; $j++) {
            $CurrentFileContent = Get-Content "C:\Folder\$($fileArray[$len])_$j.csv"
            @($WorkbookContent, $CurrentFileContent) | Set-Content "C:\Folder\$($fileArray[$len])_$j.csv"
        }
    }
    

    您可以使用 $fileArray 变量以更简单的方式查看此行为。

    $filearray
    08_configuration_items
    11_CI-Contract-new
    
    # Notice how the [0] gets appended to the string-cast $fileArray
    "$filearray[0]"
    08_configuration_items 11_CI-Contract-new[0]
    
    $filearray[0]
    08_configuration_items
    
    "$($filearray[0])"
    08_configuration_items
    

    由于$fileArray 是一个字符串数组,你会得到另一个意想不到的效果。使用"$fileArray[0]"$fileArray 将被插值并转换为 string 输出而不是 array。默认情况下,当转换为字符串时,PowerShell 将通过单个空格连接数组元素。所以得到的输出格式是arrayItem1 arrayItem2 arrayItem3[0][0]包含在变量评估中。

    【讨论】:

    • 添加子表达式操作符后效果很好。非常感谢您提供所有详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2021-03-13
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多