简体   繁体   中英

Add ProxyAddresses to separate script

Goal: Append script to add three different SMTP addresses.

Script: https://thesysadminchannel.com/how-to-create-o365-mailboxes-hybrid-exchange/

Code Snippet:

Write-Host "Continuing will create the AD account and O365 Email." -ForegroundColor:Green
Write-Host
$Proceed = $null
$Proceed = Read-Host "Continue? (y/n)"

if ($Proceed -ieq 'y') {
      
    Write-Host "Creating the O365 mailbox and AD Account."
    New-RemoteMailbox -Name $fullname -FirstName $firstname -LastName $lastname -DisplayName 
   $fullname -SamAccountName $logonname -UserPrincipalName $logonname@$domain -PrimarySmtpAddress 
   $logonname@$domain -Password $password -OnPremisesOrganizationalUnit $OU -DomainController $Server
    Write-Host "Done..."
    Write-Host
    Write-Host
    Sleep 5


    Write-Host "Adding Properties to the new user account."
    Get-ADUser $logonname -Server $Server | Set-ADUser -Server $Server -Description $Description - 
    Office $Office -StreetAddress $StreetAddress -City $City -State $State -PostalCode $PostalCode - 
    Country $Country -Title $Title -Department $Department -Company $Company -Manager $Manager - 
    EmployeeID 
    $EmployeeID
    Write-Host "Done..."
    Write-Host
    Write-Host

    if ($MemberOf) {
        Write-Host "Adding Membership Groups to the new user account."
        Get-ADUser $logonname -Server $Server  | Add-ADPrincipalGroupMembership -Server $Server - 
    MemberOf $MemberOf
        Write-Host "Done..."
        Write-Host
        Write-Host
        }
    }  
     Get-PSSession | Remove-PSSession

What I'm trying to add are these three values:

SMTP:user@domain.com
smtp:user@domain.mail.onmicrosoft.com
smtp:user@domain.onmicrosoft.com

This is along the lines of what I've come up with:

Write-Host "Adding Properties to the new user account."
    Get-ADUser $logonname -Server $Server | Set-ADUser -Server $Server -Description $Description     
    -Office $Office -StreetAddress $StreetAddress -City $City -State $State -PostalCode $PostalCode - 
    Country $Country -Title $Title -Department $Department -Company $Company -Manager $Manager - 
    EmployeeID $EmployeeID
    Write-Host "Done..."
    Write-Host
    Write-Host
    Write-Host “Setting up TCS E-mail Standard”
                $userinfo.ProxyAddresses = "SMTP:" + ($FirstInitial.Add(1)) +  ($userinfo.sn) + 
 "@DOMAIN.COM"
                #$userinfo.ProxyAddresses += "smtp:" + ($userinfo.givenname) + "." + ($userinfo.sn) + 
 "@domain.mail.onmicrosoft.com"
                $userinfo.ProxyAddresses += "smtp:" +  ($FirstInitial.Remove(1)) + ($userinfo.sn) + 
 "@domain.onmicrosoft.com"
                $userinfo.targetAddress = "SMTP:" +   ($userinfo.sAMAccountName)  + 
 "@domain.mail.onmicrosoft.com"
    Write-Host "Done..."
    Write-Host

Then I get this error:

You cannot call a method on a null-valued expression.
At C:\Users\Temp\CreateStudentEmail.ps1:233 char:21
+ ...             $userinfo.ProxyAddresses = "SMTP:" + ($FirstInitial.Add(1 ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\Temp\CreateStudentEmail.ps1:235 char:21
+ ...             $userinfo.ProxyAddresses += "smtp:" +  ($FirstInitial.Rem ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

The property 'targetAddress' cannot be found on this object. Verify that the property exists and can                         
be set.
At C:\Users\Temp\CreateStudentEmail.ps1:236 char:21
+ ...             $userinfo.targetAddress = "SMTP:" +   ($userinfo.sAMAccou ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

Just trying to find a way to incorporate the proxy address section so that I don't have to worry about extra steps.

I pulled the code from another PowerShell I used years ago, then tweaked it a bit, but it DOES actually input SMTP address fields into the AD character. It still throws errors though.

Here is what you need to do:

  set-Aduser $samaccountname -Add @{ proxyAddresses = "SMTP:user@domain.com","smtp:user@domain.mail.onmicrosoft.com","smtp:user@domain.onmicrosoft.com" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM