Provide two values for the same variable in a remote powershell

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Mohammed Hamada Mohammed Hamada 2 months, 1 week ago.

  • Author
    Posts
  • #66559
    Profile photo of Mohammed Hamada
    Mohammed Hamada
    Participant

    Hi all,

    I created a Powershell that would export users's information from Forest 1, Would created a mapped drive in a second forest DC then copy the exported CSV File to it.
    Then it would import those users's info as contacts and publish them to GAL. The powershell works fine but my problem is that in order to get the contacts published to GAL i'll have to enter two values in a "showaddressbook" attribute which is passed remotely to the second forest's powershell in a $addressbook variable ..

    The error I get when I run this powershell script is "Invalid type 'System.Collections.ArrayList'.
    Parameter name: ShowInAddressBook"

    Without the showaddressbook value the script works fine. it's just that it has two values instead of one.

    I would appreciate any help. thanks

    Code "Import-Module ActiveDirectory"

    "
    $targetComputer = "adlab.lab.com"
    $AddressBook = "CN=All Contacts,CN=All Address Lists,CN=Address Lists Container,CN=lab,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=lab,DC=com","CN=Default Global Address List,CN=All Global Address Lists,CN=Address Lists Container,CN=lab,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=lab,DC=com"
    $Path = $env:username + "csvfiles.csv"
    $encrypted = "01000000d08c9ddf0115d1118c7a00c04fc297eb0100000028a8653db5616e4ba09a8cbc702f25690000000002000000000003660000c000000010000000e22ad9548aa4abeea00e7d6dc2eb97050000000004800000a00000001000000059565d2dd3ad10cf83e9c70c5674d5f010000000f33a20cc4b1dd36c36e71b1cbe18af4614000000b0323e8c35fbd60d18e5e16f11801315f957b154"
    $user = "lab\administrator"
    $password = ConvertTo-SecureString -string $encrypted
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$password
    $parentOU = "OU=All Contacts,DC=lab,DC=com"
    $newOU = "OU=Dogus Holding,$parentOU"

    #=======================Export Mail Enabled Users=======================#

    Get-ADUser -filter * -properties * | ?{($_.emailaddress -ne $null) -and ($_.enabled -eq "True")} | Select displayName, EmailAddress,name,SamAccountName | Export-Csv C:\exported1.csv -NoTypeInformation
    Import-csv c:\exported1.csv | Select-Object *,@{Name="Addressbook";Expression={"$AddressBook"}} | Export-Csv $Path -NoTypeInformation

    $session = New-PSSession -ComputerName $targetComputer -Credential $cred

    Invoke-command $session -ArgumentList $newpath,$newOU -ScriptBlock {Param($X,$Y) Import-CSV $X | foreach {New-ADObject -Type Contact -Name $_.Name -otherattributes @{"displayName"=$_.displayName;"mail"=$_.EmailAddress;"proxyAddresses"="SMTP:$($_.EmailAddress)";"targetaddress"=$_.EmailAddress;"givenName"=$_.name;"msRTCSIP-PrimaryUserAddress"=$_.EmailAddress; "msRTCSIP-UserEnabled"=$true; 'showinaddressbook' = $_.AddressBook; "mailnickname"=$_.SamAccountName } -Path $Y } }

    "

  • #66604
    Profile photo of Sam Boutros
    Sam Boutros
    Participant
    'showinaddressbook' = $($_.AddressBook | select -First 1)
    
    • #66610
      Profile photo of Mohammed Hamada
      Mohammed Hamada
      Participant

      I am sorry I didn't explain further more. the new contacts are created from the exported CSV file which includes name,displayname,sam,email.. during the exporting CSV process I have imported the addressbook column as you can see in this part
      Import-csv c:\exported1.csv | Select-Object *,@{Name="Addressbook";Expression={"$AddressBook"}} | Export-Csv $Path -NoTypeInformation

      However, as you can see in the $Addressbook value it equals two different values "CN=All Contacts,CN=All Address Lists,CN=Address Lists Container,CN=lab,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=lab,DC=com","CN=Default Global Address List,CN=All Global Address Lists,CN=Address Lists Container,CN=lab,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=lab,DC=com"

      I guess this might be what's causing the issue. but I am not sure how do I contain the two values as a single value so it would be accepted and pass.

      I managed to do a workaround by basically sending first a single value and then after the command is finished I added this:
      "$AddressBook = "CN=Default Global Address List,CN=All Global Address Lists,CN=Address Lists Container,CN=lab,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=lab,DC=com"

      Invoke-command $session -ArgumentList $newpath,$newOU,$Addressbook -ScriptBlock {Param($X,$Y,$Z) Import-CSV $X | foreach {Get-ADObject -LDAPFilter "objectclass=contact" -Properties * -SearchBase $Y | Set-ADObject -Add @{'showinaddressbook' = $Z }}}"

      That has worked but I would just like to know how to include these two values in one for the $addressbook variable.

  • #66606
    Profile photo of Ron
    Ron
    Participant

    If it won't accept them as an array, you may have to add them one at a time after you create the object. You can use the -passthru parameter to return the created object into a variable, then use that variable to add the items.

    Untested:

    $obj = New-ADObject ...
    $_.AddressBook | % { Set-ADObject $obj -add @{'showinaddressbook' = $_} }
    • #66613
      Profile photo of Mohammed Hamada
      Mohammed Hamada
      Participant

      Hi Ron, could you please show how I am going to put that in the PSscript ?
      Thanks

You must be logged in to reply to this topic.