TBD
TBD

Reputation: 1

How to use a value as a variable?

I'm creating a Powershell script that can be used to convert a mailbox to a shared mailbox in Office 365. But first I want to check if the mailbox has status as Shared or not.

If I run this command I get a answer:

Get-Mailbox -identity $user | select-object IsShared

IsShared
--------
   False

So I want to use this value that in this case is False.

$user = Read-Host -Prompt "What mailbox to check?"
$status = Get-Mailbox -identity $user | select-object IsShared 

If ($status -eq "false") 
    {Write-Host "$user status is NOT shared"}
Else 
    {write-host "$user status is shared"}

Problem is that I only get the last message in return. Even if I change this code:

If ($status -eq true) 

So obviously there is something I need to change. But what?

Upvotes: 0

Views: 696

Answers (2)

henrycarteruk
henrycarteruk

Reputation: 13217

Even though it just shows an output of False, it's not a string "true" / "false" (note quotes) it's a boolean $true/$false

To check IsShared, rather than using Select-Object (would also need ExpandProperty to work correctly), you can use $status.IsShared to refer to just that property. This leaves the original object intact should you wish to use any of it's other properties later in your code.

(Small blog post on object properties and values.)

Which when put together gives:

$user = Read-Host -Prompt "What mailbox to check?"
$status = Get-Mailbox -identity $user

if ($status.IsShared -eq $false) { Write-Host "$user status is NOT shared" }
else { Write-Host "$user status is shared" }

You could also flip the if as it will check for $true by default:

if ($status.IsShared) { Write-Host "$user status is shared" }
else { Write-Host "$user status is NOT shared" }

Upvotes: 2

Mudit Bahedia
Mudit Bahedia

Reputation: 246

You will have to replace Get-Mailbox -identity $user | select-object IsShared with Get-Mailbox -identity $user | select -ExpandProperty IsShared The former one is not exactly returning false while the latter one returns false.

Upvotes: 0

Related Questions