Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

How to run two “Get User” commands and get one result at the same time?

$
0
0

Hello /r/Powershell, Powershell newbie here.

The script below currently checks to see if a user’s’ email exists in O365, then creates it if it doesn’t (AzureAD Connect). This leads to issues because it fails to check against our local AD. I thought a line like “$ExistingUser = Get-ADUser -Filter {UserPrincipalName -eq $NewEmail}; Get-MSOLUser -UserPrincipalName $NewEmail”, basically combing the two commands. But that skips the first part. Separating them out like below just ignores the first step.

Any suggestions/pointers/guides would be helpful!

import-module activedirectory Import-Module MSOnline $RunAsAccount = "" $RunAsPass = "" $Pass = $RunAsPass | ConvertTo-SecureString -AsPlainText -Force $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $RunAsAccount, $pass Connect-MSOLService -Credential $Cred $EmailSuffix = "mydomain" $FirstName = "Frisky" $LastName = "Duck" $FirstIn = $FirstName.Substring(0,1).ToLower().ToString() $NewEmail = $LastName + $FirstIn + $EmailSuffix $NewEmail = $NewEmail.replace(" ","").replace("’","").replace("'","").replace("..",".").replace('`'," ") $FoundEmail = $false $upn = "$NewEmail" $N = 1 Do { $ExistingUser = $null $ExistingUser = Get-ADUser -Filter {UserPrincipalName -eq $NewEmail} $ExistingUser = Get-MSOLUser -UserPrincipalName $NewEmail if ($ExistingUser -ne $null) { $NewEmail = $LastName + $FirstIn + $N + $EmailSuffix $N++ } else {$FoundEmail = $true} } while ($FoundEmail -eq $false) $NewEmail 

Thank you, FriskyDuck

submitted by /u/FriskyDuck
[link] [comments]

The post How to run two “Get User” commands and get one result at the same time? appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles