How can I fix this simple script in Powershell?

deetswerdnawork 0 Reputation points
2023-05-02T13:31:29.8466667+00:00

Hi All,

I am really new to PowerShell and have been trying to create my first ever script. It is a script to find out which mailboxes a user has access too

this is what I have so far:

$input1 = Read-Host Please your LBB email address
Connect-ExchangeOnline -UserPrincipalName $input1
$input2 = Read-Host Please enter users username
Get-Mailbox | Get-MailboxPermission -$input2 -ResultSize unlimited

It works well up until this point:

Get-Mailbox | Get-MailboxPermission -$input2 -ResultSize unlimited

I'm not sure what I'm doing wrong but doing these steps manually harbours a successful result maybe because i disregard the $inputs manually - I assume its an issue with these.

Please help haha

Exchange Server
Exchange Server
A family of Microsoft client/server messaging and collaboration software.
496 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
3,382 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
762 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 39,266 Reputation points
    2023-05-02T15:35:21.3733333+00:00

    The variable $input2 isn't the NAME of a parameter, so don't place a hyphen in front of it.

    Like this:

    $input1 = Read-Host "Please your LBB email address"
    Connect-ExchangeOnline -UserPrincipalName $input1
    $input2 = Read-Host "Please enter users username"
    Get-Mailbox | Get-MailboxPermission $input2 -ResultSize unlimited   # don't place a hyphen in front of $input2. It's not a parameter name!
    

  2. Rich Matheisen 39,266 Reputation points
    2023-05-03T14:35:53.2333333+00:00

    If you're going to pipe the results of Get-Mailbox, then use $input2 as the value of positional parameter 1 (i.e., -Identity) and remove the $input2 from the Get-MailboxPermission cmdlet:

    Get-Mailbox $input2 | Get-MailboxPermission -ResultSize unlimited
    
    

    You really don't need the "-ResultSize unlimited" on the Get-MailboxPermission. There shouldn't be more than one match for $input2.

    0 comments No comments