你會發現在Microsoft web site關於'sqare括號'在PowerShell表達式中的奇怪行爲的解釋。
但
當我只寫了以下(不含括號),它只是簡化版,工作對我來說:
Set-Content -LiteralPath "\\server\share\temp\test.txt" -Value "coucou"
但(根據微軟的文章)以下工作
Set-Content -Path "\\server\share\temp\test.txt" -Value "coucou"
set-content -path '\\server\share\temp\`[test`].txt' -Value "coucou"
我試圖用PowerShell Drive解決這個問題
New-PSDrive -Name u -PSProvider filesystem -Root "\\server\share"
而且它甚至最壞
Set-Content : Impossible de trouver une partie du chemin d'accès '\\server\share\server\share\server\share\temp\test.txt'.
Au niveau de ligne : 1 Caractère : 12
+ set-content <<<< -literalpath "u:\temp\test.txt" -Value "coucou"
+ CategoryInfo : ObjectNotFound: (\\server\shar...e\temp\test.txt:String) [Set-Content], DirectoryNotFo
undException
+ FullyQualifiedErrorId : GetContentWriterDirectoryNotFoundError,Microsoft.PowerShell.Commands.SetContentCommand
打開角落找尋解決方案1: 我解決它使用:
net use u: \\server\share
set-content -literalpath "u:\temp\test.txt" -Value "coucou"
然後是followwing工作
set-content -literalpath "u:\temp\[test].txt" -Value "coucou"
打開角落找尋解決辦法2:using FileInfo
# Create the file
set-content -path '\\server\share\temp\`[test`].txt' -Value "coucou"
# Get a FileInfo
$fic = Get-Item '\\server\share\temp\`[test`].txt'
# Get a stream Writer
$sw = $fic.AppendText()
# Append what I need
$sw.WriteLine("test")
# Don't forget to close the stream writter
$sw.Close()
我認爲對此的解釋是,在-literalpath
UNC不善支持
也就是說離奇,它應該工作,但是我可以複製你的問題,也是 – ProfessionalAmateur
有趣。我無法複製這個。 – zdan
我很好奇 - 爲什麼要使用-LiteralPath開關? – Goyuix