I'm scripting a new version of a script I've done before, but I'm upgrading it to use MSGraph for Office 365 account creation.
I'm running across an issue here, since I created a function to test if a user exists:
#Function to check account existence
Function AccountExists
{
Param(
[string]$fxEmailAccount
)
#Validamos si la cuenta existe
$return = [System.Convert]::ToBoolean("TRUE")
try {
$user = Get-MgUser -UserId $fxEmailAccount
Write-Output $($user.Count())
}
catch {
$return = [System.Convert]::ToBoolean("FALSE")
}
return $return
}
$result = AccountExists -fxEmailAccount zpinillal@mydomain.com
Partially this works for me, the issue here is how can I properly check for the existence of an account, since Get-MGUser returns an object and every time I run my function, it sometimes fail because of my implemented logic.
I already tried to use Get-MgUserCount, but didnt' got any luck on this approach either.
Can someone tell me what might I'm doing wrong or not interpreting well to do this.
Thanks,
Your function can probably be reduced to the following, PowerShell-idiomatic form:
function Test-AccountExists {
param(
$UserId
)
[bool] (Get-MgUser -UserId $UserId -ErrorAction Ignore)
}
# Sample invocation
$result = Test-AccountExists -UserId zpinillal@mydomain.com
The name Test-AccountExists
uses PowerShell's verb-noun naming convention with the approved verb Test
.
-ErrorAction Ignore
ignores an error resulting from an non-existent user ID and makes Get-MgUser
produce no output.
Casting to [bool]
, using PowerShell's to-Boolean coercion logic, yields $true
if a user object is returned, and $false
in the no-output case.
Since the cast is neither captured in a variable nor sent to a different command, its result is implicitly output, i.e. becomes the "return value" of the function (more on that below).
As Santiago notes, what Get-MgUser
's -UserId
parameter accepts is either a UPN (user principal name) or a GUID, and not every email address is also a UPN, so it's better to use -UserId
as the name of the parameter of your function too.
As for what you tried:
PowerShell has built-in constants (automatic variables) for Boolean values: $true
and $false
, so there is no need for [System.Convert]::ToBoolean("...")
I assume that the error that Get-MgUser
returns when a user with the given identity doesn't exist is a non-terminating error: such an error is not caught with a try
/ catch
statement in PowerShell - unless you promote it to a terminating one with -ErrorAction Stop
. However, in the case at hand, it's simpler to use -ErrorAction Ignore
to ignore the error, and infer the existence from the absence of output, as shown above.
Write-Output $($user.Count())
Due to the aforementioned implicit output behavior:
The statement could be simplified to $user.Count()
However, it looks like the objects returned by Get-MgUser
do not implement a .Count()
method, so this call will likely fail - and trigger the catch
block.
By contrast, any object in PowerShell is guaranteed to have a .Count
property: If a given object's type doesn't have a native property by that name, PowerShell provides it as an intrinsic member, reporting 1
for scalar (non-collection) objects and 0
for $null
.
More importantly, if the statement did write something, it would become part of the function's "return value" (stream of output objects) too - not just what you pass to return
.
return
keyword, see this answer