Creating a Hash Table

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 :slight_smile:

$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:"

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:'

Brilliant! That looks like exactly what I need.

Thanks for the help Tim!