Batch import of users into Microsoft exchange

I have been running this script and can’t seem to figure out what is going wrong if any one has any advice?

$access = “FullAccess”
$mailbox = Get-Mailbox -Identity “School House Private Office booking”
$identity = $mailbox.UserPrincipalName
$permissions = Get-MailboxPermission -identity $identity

$users = Import-Csv -Path “C:\import\test.csv” -Delimiter “`n”
foreach($user in $users){
try{
$setPermissions = Add-MailboxPermission -Identity $identity -User $user -AccessRights $access
Write-Host “Successfully added permissions for $user” -ForegroundColor Green
}catch{
Write-Host “Failed to add permissions for $user” -ForegroundColor Red
}
}

A positional parameter cannot be found that accepts argument ‘House’.
+ CategoryInfo : InvalidArgument: (:slight_smile: [Get-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Get-Mailbox
+ PSComputerName : Outlook

Cannot bind argument to parameter ‘Identity’ because it is null.
+ CategoryInfo : InvalidData: (:slight_smile: [Get-MailboxPermission], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Get-MailboxPermission
+ PSComputerName : Outlook

Cannot bind argument to parameter ‘Identity’ because it is null.
+ CategoryInfo : InvalidData: (:slight_smile: [Add-MailboxPermission], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Add-MailboxPermission
+ PSComputerName : outlook

Successfully added permissions for @{name=agg}
Cannot bind argument to parameter ‘Identity’ because it is null.
+ CategoryInfo : InvalidData: (:slight_smile: [Add-MailboxPermission], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Add-MailboxPermission
+ PSComputerName : outlook.office365.com

Successfully added permissions for @{name=hidden}Preformatted text

Connor,
Welcome to the forum. :wave:t4:

Your second line of code already errors out. The rest of the errors depend on the first one. And you do not show the original code …

$mailbox = Get-Mailbox -Identity Hidden

… vs …

A positional parameter cannot be found that accepts argument ‘House’.

You have to specify a unique identifier of the mailbox you want to get.

And please … when you post code or error messages format it as code using the preformatted text button ( </> ). Simply place the cursor on an empty line, click the button and paste your ocde.

Thanks in advance

Hi there,

I have updated all of the code. I am now getting an error after updating the second line to have quotations “MailBoxName” see error below, Any advice with regards to this?

Cannot process argument transformation on parameter 'User'. Cannot convert value "@{name=aejenkins}" to type 
"Microsoft.Exchange.Configuration.Tasks.SecurityPrincipalIdParameter". Error: "Cannot convert the "@{name=aejenkins}" value of type 
"Deserialized.System.Management.Automation.PSCustomObject" to type "Microsoft.Exchange.Configuration.Tasks.SecurityPrincipalIdParameter"."
    + CategoryInfo          : InvalidData: (:) [Add-MailboxPermission], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Add-MailboxPermission
    + PSComputerName        : outlook.office365.com

You may change this …

$setPermissions = Add-MailboxPermission -Identity $identity -User $user -AccessRights $access

… to this …

$setPermissions = Add-MailboxPermission -Identity $identity -User $($user.Name) -AccessRights $access

And BTW: Your Import-Csv command looks weird. Do you really use line breaks as delimiter?

Yeah I do have to use the line breaks, Updated that part of the script that you advised and it worked. Thanks loads for the help!!