Creating a Hash Table

This topic contains 2 replies, has 2 voices, and was last updated by  Paul Johnston 3 years, 3 months ago.

  • Author
  • #23511

    Paul Johnston

    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 ("\\\netlogon\" + $_.ScriptPath) | select-string -pattern "H: `\\"
    $HDrive = $HDrive | ForEach-Object {
    $HDrive = $HDrive | ForEach-Object {
    $_.TrimStart("net use h:")
    $HDrive | gm
    $LogonScript | ForEach-Object {
    Set-AdUser $_.SamAccountName -HomeDirectory $HDrive -HomeDrive "H:"
  • #23513

    Tim Pringle

    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 ('\\\netlogon\' + $_.ScriptPath) | Select-String -Pattern "H: `\\").ToString()
        $HDrive = $HDrive.TrimStart('net use h:')
        Set-AdUser $_.SamAccountName -HomeDirectory $HDrive -HomeDrive 'H:'
  • #23515

    Paul Johnston

    Brilliant! That looks like exactly what I need.

    Thanks for the help Tim!

You must be logged in to reply to this topic.