【问题标题】:Where can I find PowerShell cmdlet to Object Class documentation?在哪里可以找到 PowerShell cmdlet to Object Class 文档?
【发布时间】:2015-08-24 09:32:06
【问题描述】:

我是 PowerShell 的新手,我有一个非常以 Java 为中心的开发背景。因此,对于我正在使用的任何 API,我都非常依赖 Javadoc。我现在正在尝试编写一些比简单脚本更健壮的 PowerShell 代码。例如,我想使用 Try/Catch/Finally 来捕获错误并处理它们。但是,我不想只捕获基本的 [system.exception] 类。

在Javadoc中,如果一个动作抛出一个异常,我可以在Javadoc中非常清楚地看到那个异常是什么,并捕获它(在可能抛出多个异常的情况下,我想做不同的事情取决于哪个例如)。

我正在寻找 PowerShell 的类似文档。例如,

New-Item F:\ExistingFolder\NewFolder -Type Directory

如果NewFolder 已经存在,将抛出异常。如果ExistingFolder 确实 存在,它也可能抛出不同的异常。

如果有此类文档,在哪里?

【问题讨论】:

  • 对不起,这类问题不在 StackOverflow 上。 MSDN 的 Powershell 文档可以(不太可能)有一些关于 cmdlet 抛出什么异常的数据。否则,最好的办法就是反复试验。
  • 我不认为 MSDN 实际上有这个可用...如果您运行“Get-Help New-Item”或查看在线文档,错误记录(请参阅:msdn.microsoft.com/en-us/library/ms714465%28v=vs.85%29.aspx)可以抛出未列出,与 C# 不同的是,它们实际上列出了方法可以抛出的所有异常。
  • @Vesper 这个问题怎么跑题了?我想知道什么会使这个问题成为主题以供将来参考。如果我不能在 Stack Overflow 上问这样的问题,那我可以在哪里问呢?

标签: .net powershell powershell-2.0


【解决方案1】:

您必须找出用于完成这项工作的底层 .NET 方法并查看其文档。对于您的示例,可能是CreateDirectory 方法:

Exception                    Condition

IOException                  The directory specified by path is a file.
                             -or-
                             The network name is not known.

UnauthorizedAccessException  The caller does not have the required permission.

ArgumentException            path is a zero-length string, contains only white space,
                             or contains one or more invalid characters. You can query
                             for invalid characters by using the GetInvalidPathChars
                             method.
                             -or-
                             path is prefixed with, or contains, only a colon character
                             (:).

ArgumentNullException        path is null.

PathTooLongException         The specified path, file name, or both exceed the system-
                             defined maximum length. For example, on Windows-based
                             platforms, paths must be less than 248 characters and
                             file names must be less than 260 characters.

DirectoryNotFoundException   The specified path is invalid (for example, it is on an
                             unmapped drive).

NotSupportedException        path contains a colon character (:) that is not part of
                             a drive label ("C:\").

我不知道官方(或非官方)文档引用了 PowerShell cmdlet 调用的 .NET 方法。

【讨论】:

    猜你喜欢
    • 2011-07-31
    • 2013-09-04
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    相关资源
    最近更新 更多