如何使 powershell 的错误不再那么冗长?

发布于 2024-12-10 02:43:26 字数 3244 浏览 0 评论 0原文

例如,考虑以下控制台记录:

PS C:\dev\windows> rmdir -Recurse .\bin
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Implementation\Common.DTO.XML: Access to the path 'Common.DTO.XML' is denied.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : PermissionDenied: (Common.DTO.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug\Implementation cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (Implementation:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.Contract.XML: Access to the path 'Shunra.Common.Contract.XML' is denied.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : PermissionDenied: (Shunra.Common.Contract.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.XML: Access to the path 'Shunra.Common.XML' is denied.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : PermissionDenied: (Shunra.Common.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (Debug:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Directory C:\dev\windows\bin\DotNet cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (DotNet:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Directory C:\dev\windows\bin cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (C:\dev\windows\bin:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

PS C:\dev\windows>

现在将其与普通 shell (cmd.exe) 进行比较:

C:\dev\windows>rmdir /s/q bin
bin\DotNet\Debug\IMPLEM~1\Common.DTO.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.Contract.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.XML - Access is denied.

C:\dev\windows>

差异是显而易见的,与 powershell 的冗长相比,我更喜欢 cmd.exe 的简洁性。

我可以在 powershell 中拥有同样的简洁性吗?如果不是所有命令,那么也许只是我经常使用的删除项目?

For instance, consider the following console transcript:

PS C:\dev\windows> rmdir -Recurse .\bin
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Implementation\Common.DTO.XML: Access to the path 'Common.DTO.XML' is denied.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : PermissionDenied: (Common.DTO.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug\Implementation cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (Implementation:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.Contract.XML: Access to the path 'Shunra.Common.Contract.XML' is denied.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : PermissionDenied: (Shunra.Common.Contract.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Cannot remove item C:\dev\windows\bin\DotNet\Debug\Shunra.Common.XML: Access to the path 'Shunra.Common.XML' is denied.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : PermissionDenied: (Shunra.Common.XML:FileInfo) [Remove-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
Remove-Item : Directory C:\dev\windows\bin\DotNet\Debug cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (Debug:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Directory C:\dev\windows\bin\DotNet cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (DotNet:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Directory C:\dev\windows\bin cannot be removed because it is not empty.
At line:1 char:6
+ rmdir <<<<  -Recurse .\bin
    + CategoryInfo          : WriteError: (C:\dev\windows\bin:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

PS C:\dev\windows>

Now compare it with the ordinary shell (cmd.exe):

C:\dev\windows>rmdir /s/q bin
bin\DotNet\Debug\IMPLEM~1\Common.DTO.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.Contract.XML - Access is denied.
bin\DotNet\Debug\Shunra.Common.XML - Access is denied.

C:\dev\windows>

The difference is obvious and I like the laconicity of cmd.exe much much more than the verbosity of the powershell.

Can I have the same laconicity in powershell? If not for all the commands then maybe just for the Remove-Item, which I use often?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-12-17 02:43:26

您可以获得的最接近的结果是将全局 $ErrorView 值更改为“CategoryView”。另一种方法是创建您自己的视图。

The closest result you can get is by changing the global $ErrorView value to "CategoryView". Another way is to create your own view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文