如何将数据从Powershell导出到Csv文件并排成一行,而其他列中已有数据?

时间:2018-10-18 18:07:01

标签: powershell

我要执行的操作是根据需要将数据添加到Csv文件中一个单元一次,一次插入一个单元格。我当前的脚本使用Add-Content cmdlet,后跟-Value ",,$data"会根据逗号数插入所需的列。但是,Add-Content cmdlet每次调用都会插入到新行中,因此我的数据看起来非常分散。

我唯一能找到的其他解决方案是创建一个临时对象以导出到Csv(即New-Object PSObject -Property @{col1 = $data}),但这不能让我在标题行之后插入多行。 / p>

我当前的代码:

ForEach ($user in $subSite.Users) {
    if (($user -ne $null) -and ($user.Name -ne $null)) {
        if ($user.Roles.Name -contains "Full Control") {
            Add-Content -Path $outFile -Value ",$user"
        }
        elseif ($user.Roles.Name -contains "Site Owner") {
            Add-Content -Path $outFile -Value ",,$user"
        }
        elseif ($user.Roles.Name -contains "Site Administrator") {
            Add-Content -Path $outFile -Value ",,,$user"
        }
    }
}

感谢所有帮助。谢谢!

编辑:这是写入输出文件的示例。并不是很重要,但是标题已在脚本的前面添加了。

https://drive.google.com/file/d/1Sbye-ZU9PouOPwBOXdtke-xJooloaFRz/view?usp=sharing

请注意分段,因为Add-Content会在每次调用时创建一个新行。我考虑了-NoNewLine参数,但是如果我需要向同一列添加新的数据,那将无法正常工作。

0 个答案:

没有答案