gpt4 book ai didi

powershell - 在 PowerShell 中使用 StreamWriter 附加到文件

转载 作者:行者123 更新时间:2023-12-03 01:02:18 24 4
gpt4 key购买 nike

我创建了一个 PowerShell 脚本,该脚本使用 StreamWriter 将 Windows 中文件夹权限的结果写入 CSV 文件。目前,脚本会为每个文件夹创建一个新文件。我的问题是,StreamWriter 可以附加到文件而不是每次都创建或覆盖。谢谢,

$file_stream_output = New-Object IO.StreamWriter "$path_to_file\results.csv"

$file_stream_output.WriteLine('FolderName,AD Group or User,Permission')

foreach ($directory in $directories_to_search_depth_2[$k])
{
$acl = Get-Acl -Path $directory.FullName
foreach ($access_right in $acl.Access)
{
if ( ($access_right.FileSystemRights -notmatch $exclude_filesystem_rightss_regex) )
{
$file_stream_output.WriteLine(('{0}, {1}, {2}' -f $directory.FullName, $access_right.IdentityReference, $access_right.FileSystemRights))
}
}
}
$file_stream_output.Close()

我的目标是将结果附加到文件中。

最佳答案

作为Jeff Zeitlin alludes to , StreamWriter 类有 a constructor overloadappend 值作为其第二个参数 - 将其设置为 $true:

$file_stream_output = [System.IO.StreamWriter]::new("$path_to_file\results.csv", $true)

关于powershell - 在 PowerShell 中使用 StreamWriter 附加到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54256798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com