Creating a Hash Table

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Paul Johnston Paul Johnston 1 year, 8 months ago.

  • Author
    Posts
  • #23511
    Profile photo of Paul Johnston
    Paul Johnston
    Participant

    I've wrriten the below code to modify a users' HomeDirectory in ActiveDirectory by reading a drive mapping from their logon script. It works, but the problem is that every value is added and not just the one from each particular users' logon script. e.g. "\\server1\user1" should be added but "\\server\user1 \\server\user2 \\server\user3" is actually added.

    I'm thinking I need to create a hash table to get it to work but I'm not having much joy. Hopefully someone can help me out 🙂

    
    $LogonScript = Get-AdUser -filter * -SearchBase "OU=Test,DC=company,DC=org,DC=uk" -properties HomeDirectory, ScriptPath | 
    Where-Object {($_.HomeDirectory -eq $null) -and $_.ScriptPath } | select SamAccountName, ScriptPath
    
    $HDrive = $LogonScript | ForEach-Object {
    get-content -Path ("\\company.org.uk\netlogon\" + $_.ScriptPath) | select-string -pattern "H: `\\"
    }
    
    $HDrive = $HDrive | ForEach-Object {
    $_.ToString()
    }
    
    $HDrive = $HDrive | ForEach-Object {
    $_.TrimStart("net use h:")
    }
    
    $HDrive | gm
    
    $LogonScript | ForEach-Object {
    Set-AdUser $_.SamAccountName -HomeDirectory $HDrive -HomeDrive "H:"
    }
    
    
  • #23513
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hey Paul,

    I think it should just be a case of keeping things within a single iteration for each user object if I get your code right, so something like this:

    $LogonScript = Get-AdUser -filter * -SearchBase 'OU=Test,DC=company,DC=org,DC=uk' -properties HomeDirectory, ScriptPath | 
    Where-Object -FilterScript {
        ($_.HomeDirectory -eq $null) -and $_.ScriptPath
    } |
    Select-Object -Property SamAccountName, ScriptPath
     
    $LogonScript | ForEach-Object -Process {
        $HDrive = (Get-Content -Path ('\\company.org.uk\netlogon\' + $_.ScriptPath) | Select-String -Pattern "H: `\\").ToString()
        $HDrive = $HDrive.TrimStart('net use h:')
        Set-AdUser $_.SamAccountName -HomeDirectory $HDrive -HomeDrive 'H:'
    }
    
  • #23515
    Profile photo of Paul Johnston
    Paul Johnston
    Participant

    Brilliant! That looks like exactly what I need.

    Thanks for the help Tim!

You must be logged in to reply to this topic.