【问题标题】:Active Directory Module活动目录模块
【发布时间】:2016-12-21 00:00:39
【问题描述】:

我编写了一个 PowerShell 脚本,一个允许 PC Refresh 在 AD 对象上设置 CompanyDepartmentNumber 等的应用程序。在开发中一切正常。显然我的机器上安装了 AD。我已将我的应用程序编译为 .exe 并将其放置在网络共享上,技术人员将在其中执行它,因为他们启动新计算机或主要从 Windows 7 刷新到 Windows 10。

问题是新电脑此时不会安装 Active Directory。我需要找到一种方法来安装、导入和运行 Active Directory,就像在启动新计算机或更新计算机时一样。我该如何做到这一点?

下面是我用来导入模块的一些相关代码,如果它存在于机器上。

$RestoreForm_Load = {
  # Load the ActiveDirectory module if it's available
  # Check if the ActiveDirectory module is installed
  if ((Get-Module -ListAvailable | where { $_.Name -eq 'ActiveDirectory' }) -eq $null) {
    $labelDialogRedRestore.Text += "You need to install the ActiveDirectory module!`n"
  } else {
    # Check if the ActiveDirectory module is allready Imported
    if ((Get-Module ActiveDirectory) -eq $null) {
      Import-Module ActiveDirectory -ErrorAction 'SilentlyContinue'
      $labelDialogGreenRestore.Text += "ActiveDirectory module imported`n"
    } else {
      $labelDialogGreenRestore.Text += "ActiveDirectory allready imported`n"
    }
  }

【问题讨论】:

  • 这只是模块,我尝试了你的建议,但它不起作用。作为一个测试,我将其放置如下:我收到以下错误:错误:术语“Add-WindowsFeature”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含 pa ERROR: th,请验证路径是否正确并重试。
  • $No_Load = { Add-WindowsFeature net-framework-core Add-WindowsFeature RSAT-AD-PowerShell # 加载 ActiveDirectory 模块,如果它可用 # 检查是否安装了 ActiveDirectory 模块 if ((Get-Module -ListAvailable | where { $_.Name -eq 'ActiveDirectory' }) -eq $null) { $labelDialogRedNewNo.Text += "你需要安装 ActiveDirectory 模块!`n" }
  • 现在我得到这个错误:Import-Module : The specified module 'ServerManager' is not loaded because no valid module file was found in any module directory.
  • 您在哪个操作系统上运行Import-Module ServerManager

标签: powershell active-directory


【解决方案1】:

只有 Windows Server 版本具有 AD 模块或 RSAT(远程服务器管理工​​具)的任何其他部分,可用于开箱即用的安装。您可以使用Add-WindowsFeature(或Install-WindowsFeature,在Windows 2012 及更高版本中取代前者)在服务器上安装模块:

Import-Module ServerManager

$os = (Get-WmiObject Win32_OperatingSystem).Caption
switch -wildcard ($os) {
  'Windwos Server 2008*' {
    Add-WindowsFeature RSAT-AD-PowerShell -IncludeAllSubFeatures
  }
  'Windows Server 2012*' {
    Install-WindowsFeature RSAT-AD-PowerShell -IncludeAllSubFeatures
  }
}

Windows 客户端版本不附带 RSAT。您需要先安装 correct RSAT package,然后才能安装 AD PowerShell cmdlet。不过,知识库文章中的链接列表有点过时了。 Windows 10 Preview RSAT 包的链接不再有效。 Here是发布版本的下载链接。

在客户端上安装更新后,您可以安装模块,例如通过dism

dism /Online /Enable-Feature /FeatureName:RemoteServerAdministrationTools-Roles-AD-Powershell

请注意(至少在客户端版本上)功能名称可能因版本而异(该功能在 Windows 7 RSAT 中名为 RemoteServerAdministrationTools-Roles-AD-Powershell,但在 Windows 10 RSAT 中名为 RSATClient-Roles-AD-Powershell),因此您可能需要在客户端上也使用switch 声明:

$os = (Get-WmiObject Win32_OperatingSystem).Caption
$name = switch -wildcard ($os) {
  'Windows 7*'  { 'RemoteServerAdministrationTools-Roles-AD-Powershell' }
  'Windows 8*'  { '???' }
  'Windows 10*' { 'RSATClient-Roles-AD-Powershell' }
}

& dism /Online /Enable-Feature /FeatureName:$name

另外,请注意,无论您在哪个系统(服务器或客户端)上安装模块(服务器或客户端),您必须安装 .NET 框架 3.5.1 或 4.5,否则模块将无法工作(如果您甚至可以首先安装它)。

【讨论】:

    【解决方案2】:

    您可以使用以下命令在脚本中安装模块:

    Add-WindowsFeature RSAT-AD-PowerShell
    

    请注意,此功能也需要 .NET Framework 3.5.1 功能,可以使用以下命令安装:

    Add-WindowsFeature net-framework-core
    

    【讨论】:

    • 这只是 ActiveDirectory PowerShell 模块而不是 AD-Feautre 本身吗?如果是这样-谢谢,不知道
    • @SimonS 这绝对不是 AD 功能本身,因为 rsat 代表远程服务器管理工​​具,我不能 100% 确定它只是 AD 模块,但名称中的 AD 让我相当有信心;)
    • Add-WindowsFeature 仅适用于服务器版本,并且仅适用于 Windows Server 2008 R2。
    【解决方案3】:

    虽然您可以在这些机器上安装 Active Directory 只是为了运行您的代码,但我建议您设置一个与已经安装了它的计算机的会话。如果技术人员亲自动手并拥有访问 AD 的凭据,那么这会更好。

    $Session = New-PSSession -ComputerName DC -Credential (Get-Credential) -Name AD
    Enter-PSSession -Session $Session
    Import-Module ActiveDirectory
    Doing-AD -Stuff
    ...
    Disconnect-PSSession
    

    使用这种方法,将提示技术人员输入他们的凭据,脚本将运行您的 AD 内容,并且客户端计算机不会启用或安装 RSAT 工具。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多