question

GarethDavies-6974 avatar image
0 Votes"
GarethDavies-6974 asked LimitlessTechnology-2700 answered

Powershell skip commands if variable is null

I have a form used by an IT tech to create AD user accounts. The scrip uses the data entered to build the UPN, email address and a few other attributes.
It also populates group memberships using combo boxes listing the security and distribution groups available in AD. I recently asked how to make it ignore these if there was no value selected and got the answer I was looking for.
However, another combo box is used to select from a specific list of security groups which are used in Azure to assign from a selection of different Office 365 license types we have available.
Some users are not given an email address , they do not have a computer and never log in, their AD account is simply for admin purposes.

So now I have a question regarding how to make the script skip the section that pauses while the account is created and an AADC sync is triggered. If the user is not getting a license e don't need to push anything to Azure.
So what I need to know is how to make it skip lines of code if the Office 365 license security group is left blank

The code in question is

 if ($Office365LicenseDropDown.Text.Trim().Length -gt 0) {Add-ADGroupMember -Identity $Office365LicenseDropDown.Text -Members $saMAccountNameentry.Text -confirm:$false}
 Start-sleep -milliseconds 5000
 $Session = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri http://On prem Exchange server/powershell -Authentication Kerberos
 Import-PSSession $Session -DisableNameChecking -AllowClobber
 Enable-MailUser -Identity $email -ExternalEmailAddress $targetaddress
 Remove-PSSession $Session
 Invoke-Command -Computer <AADC Server> -Scriptblock {Start-ADSyncSyncCycle -PolicyType Delta}
 start-sleep -milliseconds 10000
 $Session = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri http://On prem Exchange server/powershell -Authentication Kerberos
 Import-PSSession $Session -DisableNameChecking -AllowClobber
 Enable-RemoteMailbox -identity $email –remoteroutingaddress $targetaddress
 Remove-PSSession $Session

After this it is just a series of commands to clear the text boxes ready for the next account, so I want, if $Office365LicenseDropDown.Text is blank to have it skip the AADC sync commands and go straight to the section that clears the data entered by the tech

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

If the property in question is defined as a string, then this is nice and concise:

 if (-not [string]::IsNullOrEmpty($Object.Property)){
     # do your processing here
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello GarethDavies,

Checking on any progress with your script after the collaborations. The community would appreciate your confirmation if it worked, by voting or marking as an answer or if you need more help, by posting your latest tests and results,



As always if you have any questions please don't hesitate to contact us.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.