Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Try..Catch within a new-object?

$
0
0

Hey all Smile

I have a script and I am trying to perform a try...catch statement with a new object hash table but I am getting an error:

Script

$lines = @()

foreach($msolUser in (Get-MSOLUser -All))

{

    $UserInfo = Get-User -Identity $msolUser.UserPrincipalName

    foreach($license in $msolUser.Licenses)

    {

        $lines += New-Object PsObject -Property @{

                    "Username"="$($UserInfo.DisplayName)";

"UPN"="$($UserInfo.UserPrincipalName)";

                    "Company"="$($UserInfo.Company)";

                    "AccountSKUID"="$($license.AccountSKUid)"

Try {

Get-Mailbox $UserInfo.UserPrincipalName -ErrorAction Stop

"Mailbox Status"="$("O365 Mailbox")";

}

Catch {

"Mailbox Status"="$("On-Premise")";

}

                  }

    }

}

$lines | Export-CSV C:\Users\swatto\desktop\Licenses1.csv -NoTypeInformation

 

Error

At C:\Users\swatto\Scripts\Powershell\GetLicenseInfo365.ps1:12 char:9

+                     Try{

+                        ~

Missing '=' operator after key in hash literal.

At C:\Users\swatson\Scripts\Powershell\GetLicenseInfo365.ps1:3 char:1

+ {

+ ~

Missing closing '}' in statement block.

    + CategoryInfo          : ParserError: (:) [], ParseException

    + FullyQualifiedErrorId : MissingEqualsInHashLiteral


Viewing all articles
Browse latest Browse all 6937

Trending Articles