Microsoft の Web サイトで、PowerShell 式の'角かっこ'の奇妙な動作についての説明を見つけることができます。
しかし
次のように(括弧なしで)書くと、うまくいきません:
Set-Content -LiteralPath "\\server\share\temp\test.txt" -Value "coucou"
ただし(Microsoftの記事によると)次のように機能します
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"
そして、次の作品
set-content -literalpath "u:\temp\[test].txt" -Value "coucou"
回避策 2 : 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でのサポートが不十分であるということだと思います