使用New-Item创建目录的竞争条件?

Nia*_*ton 7 windows powershell

我在调用New-Item以使用UNC路径在外部计算机上创建目录时看到了竞争条件.代码如下:

New-Item $target -itemType Directory -Force -Verbose |
        %{ Write-Host "Creating dir" $_.FullName }
Run Code Online (Sandbox Code Playgroud)

之后立即使用Test-Path返回false.我放置了一个Test-Path - > sleep 1秒重试循环,睡眠1秒后,Test-Path返回true.

New-Item是阻止通话吗?我应该在打电话给New-Item后等待吗?

Fro*_* F. 1

我无法重现你的问题。

PS > New-Item "test" -itemType Directory -Force -Verbose | %{ Test-Path $_.FullName }
VERBOSE: Performing the operation "Create Directory" on target "Destination: C:\Users\Frode\Desktop\test".
True
Run Code Online (Sandbox Code Playgroud)

New-Item通过获取父目录的DirectoryInfo对象并调用它的CreateSubDirectory创建一个新目录,例如:

DirectoryInfo subdirectory = new DirectoryInfo(parentPath).CreateSubdirectory(childName);
Run Code Online (Sandbox Code Playgroud)

我不是开发人员,但据我所知,这意味着这是一个阻塞调用,因为它等待DirectoryInfo-object 作为返回。所以问题可能出在您的存储子系统上。