【问题标题】:Powershell - Compiling with ModulesPowershell - 使用模块编译
【发布时间】:2013-04-25 19:00:03
【问题描述】:

我创建了一个基于 Active-Directory 模块的登录脚本,用于查询用户组成员以映射他的驱动器等。

我已经用 PowerGui 编译了它,并创建了一个 EXE 文件。 问题是,用户计算机上不存在该模块。

有没有办法在没有模块的情况下做到这一点,或者将模块添加到编译中?

【问题讨论】:

  • 为什么不从您的 DC 或 \\Domain\Sysvol 调用它们?

标签: powershell module compilation active-directory


【解决方案1】:

对于组成员资格,您也可以在不连接 AD 的情况下获取它,并解析 WHOAMI 实用程序的输出

$groups = WHOAMI /GROUPS /FO CSV | ConvertFrom-Csv | Select-Object -ExpandProperty 'Group Name'

if($groups -contains 'group1')
{
   do something
}

【讨论】:

    【解决方案2】:

    一种方法是使用 Active-Directory 服务接口 (ADSI)

    您可以在另一篇 SO 帖子 (Can I match a user to a group accross different domains?) 中找到一种使用 ADSI 查找用户所属的所有组的方法,在帖子中它是 C# 代码,但很容易翻译。

    这是一个简单搜索开始的小例子。

    Clear-Host
    $dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://WM2008R2ENT:389/dc=dom,dc=fr","jpb@dom.fr","Pwd")
    
    # Look for a user
    $user2Find = "user1"
    $Rech = new-object System.DirectoryServices.DirectorySearcher($dn)
    $rc = $Rech.filter = "((sAMAccountName=$user2Find))"
    $rc = $Rech.SearchScope = "subtree"
    $rc = $Rech.PropertiesToLoad.Add("mail");
    
    $theUser = $Rech.FindOne()
    if ($theUser -ne $null)
    {
      Write-Host $theUser.Properties["mail"]
    } 
    

    另一种方法是使用System.DirectoryServices.AccountManagement Namespace

    这种方式也是使用ADSI,不过是封装的,需要Framework .NET 3.5。您还会在 same post 但在 Edited (2011-10-18 13:25) 部分找到使用这种方式的 C# 代码。

    您也可以使用 WMI:

    $user2Find = "user1"
    $query = "SELECT * FROM ds_user where ds_sAMAccountName='$user2find'"
    $user = Get-WmiObject -Query $query -Namespace "root\Directory\LDAP"
    $user.DS_mail
    

    您可以在本地服务器上或域内的计算机上使用此解决方案,但从域外向 WMI 进行身份验证要复杂一些。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 2013-04-08
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 2020-02-23
      • 2015-09-01
      相关资源
      最近更新 更多