7

これは私が抱えている問題です:

# The following line works
Add-Content -LiteralPath "$Env:USERPROFILE\Desktop\[Test].txt" -Value "This is a test"

# The following line does not work and does not output an error message
Add-Content -LiteralPath "\\Server\Share\[Test].txt" -Value "This is a test"

権限を確認済みで、ネットワーク共有への書き込み権限があることは間違いありません。残念ながら、私がやっていることに必要な「LiteralPath」パラメーターを使用した場合にのみ問題が発生します。

角かっこを含む UNC パスにデータを書き込むにはどうすればよいですか?

4

2 に答える 2

6

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でのサポートが不十分であるということだと思います

于 2011-06-25T04:01:50.243 に答える
0

角括弧のワイルドカードは、コマンドレットの -path パラメーターでのみ実装されているようです。fileinfo オブジェクトのデフォルトのメソッドは、引き続きそれらをリテラルと見なします。

 $fic = [io.directoryinfo]"\\server\share\temp\[test].txt"
于 2011-06-25T08:33:36.640 に答える