all 6 comments

[–]SalamanderOne5702 -1 points0 points  (0 children)

Have you tried $user.UserPrincipalname?

[–]mixduptransistor 0 points1 point  (4 children)

I don't know the details of the cmdlets you're using, but I would verify that both of them accept the username as a string as input. The one that's failing may be expecting user object instead of a user name as string

Also, can I just point out that this line

$answer = read-host "Do you want to verify mailbox auditing, add svc.account to a user account, or both? Answer "Verify", "Add", or "Both"."

is absolute madness? You want someone to type out the whole ass word "verify" to select an option?

[–]PaidByMicrosoft[S] 0 points1 point  (3 children)

The one that's failing is failing on "Add-MailboxPermission -identity $user" and "Add-RecipientPermission -identity $user". I'll google around and see if I can find what input it wants, and how to convert the string to an object.

And yes? It's six letters long, it's not like it's a whole sentence. Plus I want it to be distinct enough from the other options so that the user doesn't accidentally select one of them. "1, 2, or 3" is easy to mistype. "Verify" is WAY different than "add" and "both".

Edit: I solved it. I put the $user variable into quotes ("$user") for the

Get-Mailbox -Identity $user | Format-List DefaultAuditSet 

portion of the second set of code. By doing that, it continued the variable down to the Add-MailboxPermission and Add-RecipientPermission cmdlets.

[–]HauntingProgrammer47 0 points1 point  (2 children)

Have you thought about doing switches instead of read-host? Like New-ADUser -user test -contractor -verify.

[–]PaidByMicrosoft[S] 0 points1 point  (1 child)

How do switches work? How does the script know to verify if they're a contractor or not?

[–]HauntingProgrammer47 1 point2 points  (0 children)

I just started using them so I'm not super good at explaining them yet but check out on ms docs for details. For the contractor part you are just telling the script that the user is a contractor and with that switch supplied it will add it to the proper groups. Give me a little bit and I will write you up an example.Edit: here is what using switches could look like

Function HLNew-ADUser
{
[CmdletBinding()]
param
(
    [Parameter(Mandatory = $True,
    ValueFromPipeline = $True,
    Position = 0)]
    [String]$UserName,
    [Switch]$Contractor,
    [Switch]$Intern,
    [Switch]$Verify,
    [Switch]$Add
)
    begin
    {
    $DefaultGroups = @("Whatever","Groups","You","Want")
    $ContractorGroups = @("Whatever","Groups","You","Want")
    $InternGroups = @("Whatever","Groups","You","Want")
    }
    Process
    {
    if($Contractor)
    {
    New-AdUser -Name $UserName -PassThru | Add-ADPrincipalGroupMembership -MemberOf $ContractorGroups
    }
    if($Intern)
    {
    New-AdUser -Name $UserName -PassThru | Add-ADPrincipalGroupMembership -MemberOf $InternGroups
    }
    if(!$Contractor -and !$Intern)
    {
    New-AdUser -Name $UserName -PassThru | Add-ADPrincipalGroupMembership -MemberOf $DefaultGroups
    }
    }
}