【发布时间】:2020-08-31 10:05:42
【问题描述】:
我有一个 .ps1 脚本,它获取我们的一些内部 SQL 数据并将其同步到 Google API。
它通过针对我们的数据运行 foreach、使用逻辑生成 API 命令并通过 System.IO.StreamWriter 将这些命令写入文本文件来实现这一点。然后 API 处理器对文件运行批处理作业。
我遇到的问题是批处理作业部分似乎在 foreach 完成写入文件之前被触发,这使得整个脚本失败。
这是我的代码的简化版本:
$stream = [System.IO.StreamWriter] "C:\GAM\uploads\stream\gamBatch$today.txt";
## Loop csv and generate Google OU path
Write-Host "Generating location strings, OU paths, and update commands..."
Import-CSV "C:\uploads\Upload$today.csv" | ForEach-Object {
## Define fancy vars here
## Do fancy logic here
## Stream command list to log file
$line = "update $deviceId assetid $Barcode location $location ou $ouPath";
$stream.WriteLine($line);
};
## Trim top line of batch file (Removes header line)
(Get-Content "C:\uploads\stream\Batch$today.txt" | Select-Object -Skip 1) | Set-Content "C:\uploads\stream\Batch$today.txt";
## Close Stream instance and bulk run commands
Write-Host "Running batch command..."
$stream.WriteLine("commit-batch");
$stream.close();
apiBatch "C:\uploads\stream\Batch$today.txt";
这会在我的日志中生成此错误:
PS>TerminatingError(Set-Content): "The process cannot access the file
'C:\uploads\stream\Batch2020-05-14.txt' because it is being used by another process."
如何让 Powershell 在触发批处理命令之前等待 txt 文件完成写入?
【问题讨论】:
-
为什么不直接skip在第一行添加文件?
-
它是一个 csv 文件,第一行是我没有包含的逻辑所需的标题。不是完全需要删除它,但更清洁
标签: powershell foreach batch-processing synchronous streamwriter