如何组织powershell class定义

How to organize powershell class definition

我 运行 在 ise 中执行以下代码片段时遇到问题,方法是全选并单击 运行:

Import-Module ActiveDirectory
class ADUtil
{
    [Microsoft.ActiveDirectory.Management.ADGroup] $Group
}

类型 [Microsoft.ActiveDirectory.Management.ADGroup] 未知。如果我首先显式执行 Import-Module ActiveDirectory,那么我可以 运行 class 定义而不会出现任何错误。

正确。当然,您必须先导入模块,然后再使用其中包含的任何类型。

为了让它更可用,我想到了两种可能性:

  1. 如果是脚本,可以使用#Require语句:
#Require -Module ActiveDirectory
  1. 如果您经常在控制台中使用它,请将导入添加到您的 $Profile
Import-Module ActiveDirectory