powershell - 使用 Literalpath 或有意复制项目的错误?
问题描述
考虑以下非常简单的代码:
Try {
Copy-Item -Path '\\server\g$\thisfolder\thisfiledoesntexist.ini' -Destination 'c:\temp' -ErrorAction Stop
}
Catch {
"Ran into an issue: $_"
}
这可以很好地捕获不存在的源文件的错误。但是以下不会 - 没有产生错误。
Try {
Copy-Item -LiteralPath '\\?\UNC\server\g$\thisfolder\thisfiledoesntexist.ini' -Destination 'c:\temp' -ErrorAction Stop
}
Catch {
"Ran into an issue: $_"
}
但是...这将捕获错误
Try {
Get-ChildItem -LiteralPath '\\?\UNC\server\g$\thisfolder\thisfiledoesntexist.ini' -ErrorAction Stop | Copy-Item
}
Catch {
"Ran into an issue: $_"
}
这是我第一次有机会使用文字路径 - 这种行为是有意的/预期的吗?
解决方案
我认为您Copy-Item
在 Windows PowerShell 下发现了一个错误。似乎?
在输入中的任何位置都包含一个字符,-Path
或者-LiteralPath
阻止错误被困在不存在的路径中。
该错误似乎已在 PowerShell Core 上得到修复,您上面的代码确实会导致异常。
推荐阅读
- c# - 如何检索没有任何属性名称的 JSON 数组?
- go - 使用 gqlgen 为 GraphQL 添加带有 go-chi 路由器的身份验证中间件
- php - 在codeigniter中使用php在mysql数据库中插入json
- meson-build - 如何在介子中添加 Ws2_32.lib 文件?
- bash - 使用 sed 进入循环的问题
- c# - MongoDb C# ToList() 性能下降。有什么改善的方法吗?
- python - PyInstaller 创建 __pycache__ 而不是 dist
- regex - 如何简单地实现包容性或正则表达式?
- set - 我无法将音频文件设置为旧版本设备的铃声。我为新的 android 版本写的部分工作正常
- node.js - 使用 ExpressJS REST API 对 Flutter 客户端进行身份验证