你会发现在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