I have this script I’m developing for my employer that will allow us to quickly create and terminate users. With such a high turnover, it gets pretty time consuming. I’ve got most of it working, but I can’t get past one issue (I hope it’s the last one anyways)
(pre)function New-PVSUser {
[CmdLetBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Position=0,Mandatory=$true)]
[string]$Alias,
[Parameter(Position=1,Mandatory=$true)]
[string]$FirstName,
[Parameter(Position=2,Mandatory=$true)]
[string]$LastName,
[Parameter(Position=3,Mandatory=$false)]
[string]$Copied,
[switch]$Licensed
)
# Create user without license
if ($Licensed) {
# Create new user with exchange license
New-MsolUser -FirstName $FirstName -LastName $LastName -DisplayName “$FirstName $LastName” -UserPrincipalName “$Alias@propertyvaluationservices.net” -LicenseAssignment propertyvaluation:EXCHANGESTANDARD -UsageLocation US
Write-Verbose “Copying groups from $Copied and adding $FirstName $LastName to them”
# Copy distribution group membership
$copied_dn = (get-mailbox $Copied).distinguishedname
$new_dn = (get-mailbox $Alias).distinguishedname
$groups = Get-DistributionGroup -ResultSize unlimited
foreach ($group in $groups) {
if ((get-distributiongroupmember $group.identity | select -expand distinguishedname) -contains $copied_dn) {
Add-DistributionGroupMember -Identity $group.identity -Member $Alias
Write-Verbose "Added to $($group.identity)"
}
}
}
else {
Write-Verbose "Creating user"
New-MsolUser -FirstName $FirstName -LastName $LastName -DisplayName "$FirstName $LastName" -UserPrincipalName "$Alias@propertyvaluationservices.net" -UsageLocation US
Write-Verbose "User creation complete"
}
Write-Verbose "Changing user password to Welcome1"
Set-MsolUserPassword -UserPrincipalName "$Alias@propertyvaluationservices.net" -NewPassword Welcome1
Write-Verbose "Password is now Welcome1"
}(/pre)
All of my verbose messages display, but I keep getting the same error when it’s copying distros no matter what I try
(pre)The operation couldn’t be performed because object ‘dummy6@propertyvaluationservices.net’ couldn’t be found on
‘BN1PR02A001DC03.NAMPR02A001.prod.outlook.com’.
+ CategoryInfo : NotSpecified: ( [Get-Mailbox], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : [Server=BY2PR02MB188,RequestId=99ca3eb8-06dc-4fc6-a056-56c015ade2df,TimeStamp=10/9/2014 7:28:55 PM] [FailureCategory=Cmd
let-ManagementObjectNotFoundException] 4C68FEBC,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
+ PSComputerName : pod51035psh.outlook.com(/pre)