title: Map network drives based on IP/subnet and AD group memberships using powershell
body: <p>I am using a powershell script like the one below, it looks at AD Security group memberships and based on that it maps drives for the users. What I am trying to accomplish is that when it sees users log on from a certian IP or Subnet it maps a different set of drives in addition to the ones it grabs from AD group memberships, any insight will be appreciated.</p>
  
  <pre><code>    # The section below determines what AD groups is the user member of
      $strName = $env:username
  
      function get-GroupMembership($DNName,$cGroup){
  
          $strFilter = "(&amp;(objectCategory=User)(samAccountName=$strName))"
  
          $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
          $objSearcher.Filter = $strFilter
  
          $objPath = $objSearcher.FindOne()
          $objUser = $objPath.GetDirectoryEntry()
          $DN = $objUser.distinguishedName
  
          $strGrpFilter = "(&amp;(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");
      }
  </code></pre>