为什么 Set-Content 会抛出此错误?

Fre*_*red 5 powershell

我可以使用下面的代码示例重新创建该问题

New-PSDrive -Name Z -PSProvider FileSystem -Root \\10.10.22.6\d$ -Credential $cred -Confirm:$false -Scope Global
Set-Content -Path 'Z:\__unc_test\Powershell\___datefile' -Value ([System.DateTime]::Now).ToString()

Get-ChildItem Z:\__unc_test
Run Code Online (Sandbox Code Playgroud)

Get-ChildItem 将返回目录列表,因此我知道 New-PSDrive 调用已成功,并且可通过 Powershell 使用 UNC。

但是,Set-Content 调用失败并出现以下错误:

Set-Content : Could not find a part of the path '\\10.10.22.6\d$\10.10.22.6\d$\10.10.22.6\d$\__unc_test\Powershell\___datefile'.
At line:1 char:1
+ Set-Content -Path 'Z:\__unc_test\Powershell\___datefile' -Value ([System.DateTim ...
Run Code Online (Sandbox Code Playgroud)

如果您查看该错误,就会发现 Set-Content 正在尝试以某种方式解析路径,并且正在做一些非常古怪的事情。

任何人都可以尝试在他们的机器上重新创建这个和/或向我解释我在这里做错了什么吗?Get-Item、Get-ChildItem 和 Copy-Item 似乎都可以跨该驱动器号工作,但 Set-Content 则不行。

Boo*_*Roo 6

我可以在我的机器上重现这个问题。仅当您调用 Set-Content 之前该文件不存在时,才会发生这种情况。简单的解决方案,在使用 set-content 之前运行它:

New-Item -Path 'Z:\__unc_test\Powershell\___datefile' -Type file -Force
Run Code Online (Sandbox Code Playgroud)

如果我使用“net use”添加驱动器,也不会出现此问题。