Format pscustomobject the right way

 

Hi All,

I am using the following script to return two columns: 1 for source users and the 2 for the destination users:

$SourceDestinationUsers = @()

for ($i = 0; $i -lt $SourceUser.length; $i++)

{

$SourceDestinationUsers += [pscustomobject]@{SourceUser = $SourceUser[$i]; DestinationUser = $DestinationUser[$i] }

}

$SourceDestinationUsers | Export-Csv .\out.csv -NoTypeInformation

The script works but returns me the results in the wrong format:
SourceUser                                       DestinationUser
----------                                         ---------------
@{SourceUser=user1@old.domain.com} @{DestinationUser=user1@new.domain.com}
@{SourceUser=user2@old.domain.com} @{DestinationUser=user2@new.domain.com}
@{SourceUser=user3@old.domain.com} @{DestinationUser=user3@new.domain.com}
How can I remove the unneeded characters and obtain the following result?
SourceUser              DestinationUser
----------              ---------------
user1@old.domain.com user1@new.domain.com
user2@old.domain.com user2@new.domain.com
user3@old.domain.com user3@new.domain.com
 

Thanks

Assuming you have source data like this:

$SourceUser = 'user1@old.domain.com', 'user2@old.domain.com', 'user3@old.domain.com'
$DestinationUser = 'user1@new.domain.com', 'user2@new.domain.com', 'user3@new.domain.com'

Somethin like this should work actually:

$SourceDestinationUsers = for ($i = 0; $i -lt $SourceUser.length; $i++) {
    [pscustomobject]@{
        SourceUser      = $SourceUser[$i]
        DestinationUser = $DestinationUser[$i] 
    }
}
$SourceDestinationUsers | Export-Csv .\out.csv -NoTypeInformation

But there are easier ways to achieve what you want …

$sourceUserList = 'user1@old.domain.com', 'user2@old.domain.com', 'user3@old.domain.com'

$SourceDestinationUsers = foreach ($sourceUser in $sourceUserList) {
    [pscustomobject]@{
        SourceUser      = $sourceUser
        DestinationUser = $sourceUser -replace '@old.domain.com','@new.domain.com'
    }
}
$SourceDestinationUsers | Export-Csv .\out.csv -NoTypeInformation

Hi Olaf,

 

The first script is still returning the same wrong format. Regarding the last script that would not be an option as going forward, there will be several old domains to match with the new one.

 

$sourceuser and $destination users are in two variables in the following format:

 

SourceUser
----------
test1@old.domain.com
test2@old.domain.com
test3@old.domain.com

and

DestinationUser
----------
test1@new.domain.com
test2@new.domain.com
test3@new.domain.com

 

Ideally, I would like to get the first option for working.

Hmmm … you really like to do it the hard way, don’t you? :wink:

$SourceUserSource = 'user1@old.domain.com', 'user2@old.domain.com', 'user3@old.domain.com'
$DestinationUserSource = 'user1@new.domain.com', 'user2@new.domain.com', 'user3@new.domain.com'

$SourceUser = foreach ($User in $SourceUserSource ) {
    [PSCustomObject]@{
        SourceUser = $User
    }
}

$DestinationUser = foreach ($User in $DestinationUserSource ) {
    [PSCustomObject]@{
        DestinationUser = $User
    }
}

$SourceDestinationUsers = for ($i = 0; $i -lt $SourceUser.length; $i++) {
    [pscustomobject]@{
        SourceUser      = $SourceUser[$i].SourceUser
        DestinationUser = $DestinationUser[$i].DestinationUser
    }
}
$SourceDestinationUsers | Export-Csv .\out.csv -NoTypeInformation

… and what exactly would be the issue with that? :wink: Do you have more than one new domain? If that’s the case it might be easier to provide the input data in another format … like CSV for example where you have exact relations between the old account and the new account.

Edit: to use 2 actually unrelated arrays as input might be error prone.

[quote quote=215655][/quote]

Right - the idea is to export the file in CSV and then manipulate it going forward. So far I have that two variables that I want to merge in one CSV file and continue the script.

Unfortunately, the output is now empty. I think could be easier to manipulate the part where I obtained the two different output so that we can have sourceuser and destinationuser in one format altogether.

 

#query for source users

$csvdata = Import-CSV -path $csvFile | Select-Object id

$SourceUser = foreach ($domain in $Sourcedomains)

{

$csvdata | ForEach-Object { try

    { Get-ADUser -Server $domain -Identity $_.muid -EA 1

    }

    catch

    {

    } } | Select-Object @{Name = "SourceUser"; Expression = { $_.UserPrincipalName } }

}


#query for destination users

$csvdata = Import-CSV -path $csvFile | Select-Object id

$DestinationUser = foreach ($domain in $DestinationDomains)

{

$csvdata | ForEach-Object { try

    { Get-ADUser -Server $domain -Identity $_.muid -EA 1

    }

    catch

    {

    } } | Select-Object @{Name = "DestinationUser"; Expression = { $_.UserPrincipalName } }

}

 

Do you think are we able to make the change here so that we can obtain the CSV file all in one format?

 

SourceUser,DestinationUser

1userxxx,1userxxx

2userxxx,2userxxx

3userxxx,3userxxx

Thanks

Cleaned from useless white space and code you’re not using anyway (try catch) your code looks like this:

#query for source users
$csvdata = Import-CSV -path $csvFile | Select-Object id
$SourceUser$domain= foreach ($domain in $Sourcedomains) {
$csvdata |
ForEach-Object {
Get-ADUser -Server $domain -Identity $.muid -EA 1
} |
Select-Object @{Name = “SourceUser”; Expression = { $
.UserPrincipalName } }
}

#query for destination users
$csvdata = Import-CSV -path $csvFile | Select-Object id
$DestinationUser = foreach ($domain in $DestinationDomains) {
$csvdata | ForEach-Object {
Get-ADUser -Server $domain -Identity $.muid -EA 1
} | Select-Object @{Name = “DestinationUser”; Expression = { $
.UserPrincipalName } }
}

The resulting two arrays are actually completely unrealted. How do you match an account from an old domain to an account in a new domain?

Edit: I noticed just now … even worse: you create your $csvdata by selecting the property “id” but you use it later with the property “muid”. That cannot work actually at all!?

[quote quote=215673]Cleaned from useless white space and code you’re not using anyway (try catch) your code looks like this:

PowerShell
17 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59771px; left: 51px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#query for source users
$csvdata = Import-CSV -path $csvFile | Select-Object id
$SourceUser$domain= foreach ($domain in $Sourcedomains) {
$csvdata |
ForEach-Object {
Get-ADUser -Server $domain -Identity $_.muid -EA 1
} |
Select-Object @{Name = "SourceUser"; Expression = { $_.UserPrincipalName } }
}
#query for destination users
$csvdata = Import-CSV -path $csvFile | Select-Object id
$DestinationUser = foreach ($domain in $DestinationDomains) {
$csvdata | ForEach-Object {
Get-ADUser -Server $domain -Identity $_.muid -EA 1
} | Select-Object @{Name = "DestinationUser"; Expression = { $_.UserPrincipalName } }
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The resulting two arrays are actually completely unrealted. How do you match an account from an old domain to an account in a new domain?

Edit: I noticed just now … even worse: you create your $csvdata by selecting the property “id” but you use it later with the property “muid“. That cannot work actually at all!?

[/quote]

Hi Olaf,

The script is working until giving the result “$sourcedestinatinuser” but I understand that logic is not giving the best effect. For this reason, I am trying to make changes and improve it based on your recommendations.

As far as I could see, one of the suggestions you gave me did work after a few tests, but I needed to delete something and keep it like that:

$SourceDestinationUsers = for ($i = 0; $i -lt $SourceUser.length; $i++)
{
    [pscustomobject]@{
        SourceUser      = $SourceUser[$i].SourceUser
        DestinationUser = $DestinationUser[$i].DestinationUser
    }
}
$SourceDestinationUsers | Export-Csv .\SourceDestinationUsers.csv -NoTypeInformation
That was the main reason to open this Topic :-)

 

The error with the “id” you noticed is just a typo and atm am examining to relate both results “source user” and “destination user” with the UPN.

 

Thanks