# The Below command will will pass the value from ADManager Plus
param($Arg1)
# Supply the LinkedDomain credentials 
$username = "domain\adminuser"
# $password = "paSSWorD" | convertto-securestring
# When sometimes cant able to convert the plaintext to secure string.. so that we are converting forcely with help Force option
$password = ConvertTo-SecureString -String "PassWorD123" -AsPlainText -Force
$cred = New-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password


# Load the snap-in for the Exchange commands (If exchange 2010)
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010


# Load the snap-in for the Exchange commands (If exchange 2007)
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
#Create Linked Mailbox 
#We can create new linked mailbox or enabled linked mailbox with help of ADMP templates. 
#powershell  -ExecutionPolicy ByPass  "C:\ManageEngine\LinkedMailbox_Test2.ps1" -Arg1 %username%
#If sometimes ps1 files execution is denied (check with get-ExecutionPolicy, generally if remotesigned type policy will allow to execute the policy.. Even sometimes that type also deny the execution... so that we explicity set the execution poliy (bypass)

#New-Mailbox -Name $Arg1 -Alias $Arg1 -UserPrincipalName $Arg1@xchange.com -Database 'Mailbox Database 1191661938' -LinkedMasterAccount 'adap\45TestUser100' -LinkedDomainController 'adap-dc1.adap.internal.com' -LinkedCredential:$cred

#New-Mailbox -Name $Arg1 -Alias $Arg1 -UserPrincipalName $Arg1@xchange.com -Database 'Mailbox Database 1191661938' -LinkedMasterAccount 'adap\45TestUser15' -LinkedDomainController 'adap-dc1.adap.internal.com' -LinkedCredential:$cred

#If you want create new LinkedMailbox user, then following cmdlet will be used.. UserPrinciplaName is manadatory when create new account

#New-Mailbox -Name $Arg1 -Alias $Arg1 -UserPrincipalName $Arg1@xchange.com -Database 'Mailbox Database 1191661938' -LinkedMasterAccount 'adap\45TestUser151' -LinkedDomainController 'adap-dc1.adap.internal.com' -LinkedCredential:$cred

#Linked Mailbox User should be disabled state in that domain. If already disabled user exist in that domain, we can enable as Linked Mailbox user with help of followin cmdlet
Enable-Mailbox -Identity $Arg1 -Alias $Arg1 -Database 'Mailbox Database 1191661938' -LinkedMasterAccount 'masterDomain\LinkedMasterAccount' -LinkedDomainController '<dns formate of linked domaincontrolled' -LinkedCredential:$cred
