Powershell skip commands if variable is null

Gareth Davies 276 Reputation points
2021-09-24T21:05:26.51+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,382 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2021-09-24T21:39:33.633+00:00

    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
    }
    
    0 comments No comments

  2. Limitless Technology 39,376 Reputation points
    2021-09-28T15:23:43.993+00:00

    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.

    0 comments No comments