Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Map drives based on AD Membership from a non-trusted AD domain

$
0
0

Hi guys,

I'm fairly new to scripting and hope you can help.  

I am performing data migration between 2 domains (Domain A & Domain B) that aren't trusted. I need to map drives to the new domain B based on group membership using new domain B credentials.

I have a script to map drives based on group membership which works fine in the same domain.

I am having trouble connecting to domain B from A and then running script as Domain B credentials and not not default username if that makes sense

Passing credentials bit i need help with

$cred = Get-Credential -Message "Please enter your username Domain\Username" 

$networkCred = $cred.GetNetworkCredential()

Enter-PSSession –computerName server -credential $networkcred

$strName = $networkcred.username

 

Map drives script ( works fine in same domain)

# ====================================================

# Queries user account in AD for user group membership

# ====================================================

 

$strName = $env.username (changed to $networkcred.username whilst trying above)

 

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

}

}

 

# ====================================================

# Map network drives

# ====================================================

 

$result = get-groupMembership $strName "map_Z"

if ($result -eq '1') {

$(New-Object -ComObject WScript.Network).RemoveNetworkDrive("Z:");

$(New-Object -ComObject WScript.Network).MapNetworkDrive("Z:", "\\server\share");


Viewing all articles
Browse latest Browse all 8583

Trending Articles