使用PowerShell映射基于IP /子网和AD组成员身份的网络驱动器

时间:2013-04-10 21:42:43

标签: powershell

我正在使用类似下面的PowerShell脚本,它会查看AD安全组成员身份,并根据它为用户映射驱动器。我想要实现的是,当它看到用户从证书IP或子网登录时,除了从AD组成员资格中获取的驱动器之外,它还映射了一组不同的驱动器,我们将不胜感激。

    # The section below determines what AD groups is the user member of
    $strName = $env:username

    function get-GroupMembership($DNName,$cGroup){

        $strFilter = "(&(objectCategory=User)(samAccountName=$strName))"

        $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
        $objSearcher.Filter = $strFilter

        $objPath = $objSearcher.FindOne()
        $objUser = $objPath.GetDirectoryEntry()
        $DN = $objUser.distinguishedName

        $strGrpFilter = "(&(objectCategory=group)(name=$cGroup))"
        $objGrpSearcher = New-Object System.DirectoryServices.DirectorySearcher
        $objGrpSearcher.Filter = $strGrpFilter

        $objGrpPath = $objGrpSearcher.FindOne()

        If (!($objGrpPath -eq $Null)){

            $objGrp = $objGrpPath.GetDirectoryEntry()

            $grpDN = $objGrp.distinguishedName
            $ADVal = [ADSI]"LDAP://$DN"

            if ($ADVal.memberOf.Value -eq $grpDN){
                $returnVal = 1
                return $returnVal = 1
            }else{
                $returnVal = 0
                return $returnVal = 0

            }

        }else{
                $returnVal = 0
                return $returnVal = 0

        }

    }

    # The section below maps network drives based on users AD Security Group memberships

    $result = get-groupMembership $strName "SecurityGrtoup1"
    if ($result -eq '1') {
        $(New-Object -ComObject WScript.Network).RemoveNetworkDrive("G:");
        $(New-Object -ComObject WScript.Network).MapNetworkDrive("G:", "\\server1\Group");
        $(New-Object -ComObject WScript.Network).RemoveNetworkDrive("P:");
        $(New-Object -ComObject WScript.Network).MapNetworkDrive("P:", "\\server2\Common");
    }

    $result = get-groupMembership $strName "SecurityGroup3"
    if ($result -eq '1') {
        $(New-Object -ComObject WScript.Network).RemoveNetworkDrive("N:");
        $(New-Object -ComObject WScript.Network).MapNetworkDrive("N:", "\\Server3\files");
    }

1 个答案:

答案 0 :(得分:0)

这应该将IPv4地址作为字符串对象:

$ip = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress} | Select-Object -ExpandProperty IPAddress)[0]

然后你可以解析你需要的东西,你也可以使用.IPSubnet或.DefaultIPGateway属性,如果那样能得到你所需要的东西。

对于其他可用属性,请尝试:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object {$_.IPAddress} | Get-Member