Question

Im pulling out information from AD to output alot of information to be handled at a later point.

I need to export this to a csv or something so i can have a rollback ( in effect import file so i can handle each object like i can with the hashtable below. Especially that inside $_.MemberOf).

The hashtable im struggling with handling is:

$logging1 = @{
MemberOf="$users.MemberOf"
OriginalOU="$Ou.DistinguishedName"
DisabledWhen="$descriptionDisabled"
}
$logging = @{$users.SamAccountname = $logging1}

what the rest of the script does is(that isnt pasted ) is:

iterate through a bunch of ou's. Getting users last logged in -90 days ago or more then passing them to the HashTable in question which i like to append to a file to be imported at a later time.

I have been googling for hours without getting somewhere

here is the complete script:

import-module ActiveDirectory
$descriptionDisabled = get-date -Format yyyyMMdd
$Loggdir = "C:\temp"
$array = @{}
$loggname = get-date -f yyyyMMdd
$90days = ((get-date).AddDays(-90))
$searchBase = 'OU=someou4,OU=someou3,OU=someou2,OU=someou1,DC=name,DC=NO'
$ExclusionList = Someexlutions
$OUlist = Get-ADOrganizationalUnit -SearchBase $searchBase 
foreach ($Ou in $OUlist)
{
$ExpiredADusers = get-aduser -Filter {(LastlogonDate -le $90days) 
                                      -and (Enabled -eq $True)}
                             -SearchBase $ou.distinguishedname
                             -Properties * 
#Get information about every user and their groupmembership
foreach ($Users in $ExpiredADusers)
    {
    $users = get-aduser -Identity $users.SamAccountName
                        -Properties * 
             | Select-Object -Property SamAccountName, MemberOf, 
                                       DistinguishedName, ObjectGUID
    $logging =  @{$person.SamAccountName = @{
    MemberOf=$person.MemberOf;
    OriginalOU=$Ou.DistinguishedName;
    DisabledWhen="$descriptionDisabled"}}
    $Array += $Logging   

    }         
}

$array | export-clixml -path somepath -noclobber      

EDIT SOLVED Solved problem and original script in question has been updated to handle Nested hashtables

Original problem I think you can understand what im trying to do. Im making a rollback file incase needed.

So the exported file need to be easy to import. when using hashtables you can use . notations and each objects under memberof is treated as an object

What the rollback needs is so i can iterate through the imported info to move the user back to its original OU placement and restore membership

Was it helpful?

Solution

I think EBGreen really hit the nail on the head here. You need an array of custom objects, and then you can just export it to a CSV like you want to. It's a really minor change in code too.

$Array = @()
$ForEach($User in $Users){
    $logging1 = New-Object PSObject -Property @{
        MemberOf=$user.MemberOf
        OriginalOU=$Ou.DistinguishedName
        DisabledWhen=$descriptionDisabled
    }
    $Array += $Logging1

}

Edit: Hm, so you want to be able to export and import full objects. You don't want a CSV then because you have nested arrays, and a CSV is not designed to handle that for export and import. You need XML, so, as mentioned above, you need to use Export-Clixml and Import-Clixml since XML can handle nested arrays. Just pipe the array to it once the array has all your data and you should be all set.

Edit2: The hashtables within hashtables issue... Ok, so we had it as MemberOf="$user.MemberOf" and that's the issue. It is converting it to a string, so it is expanding the entire $User variable, and tacking .MemberOf to the end of it. We don't really want to do it in this case, but if you want to access a property of an object from within doublequotes you need to put $() around it. For example if you wanted to include the user's distinguishedname as a part of human friendly output you could do something like:

Write-Output "$($Users.Name)'s distinguished name is: $($users.distinguishedname)"

Which would output something like:

TMTech's distinguished name is: CN=TMTech,OU=Awesome,OU=Administrators,DC=Digital,DC=Ghost,DC=net
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top