Ignored Variables?

Hello powershell.org,

I am trying to smooth out the wrinkles in a script (below) I am working on and it seems that some of my variables are being discarded and some are not. As an example, if after running the script I run echo $ManagerSMTP, I get the expected result. If I run echo $SMTP, nothing is returned whatsoever…

Thoughts?

[pre]

Manager Direct Reports

By Harrison Jacobsen

August 28th, 2019

This script obtains a list of all people managers in the organization and checks if there is a corresponding direct reports dynamic distribution group. It then generates a direct reports

dynamic distribution group for any manager that does not already have one.

Step 1: Define service account, obtain credentials and connect to office 365

$ServiceAccont = “sa_ddg_create@nexus.contoso.com
$ServiceAccontPW = Get-Content “E:\Office365 DDG Creation\o365pw.txt” | ConvertTo-SecureString
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $ServiceAccont,$ServiceAccontPW

Connect to Office 365

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
Import-PSSession $Session

Step 2: Set Log Path

$LogPath = “E:\Office365 DDG Creation\Log\DDG Create.log”

Step 3: Define Variables

$Managers = (Get-ADUser -SearchBase ‘OU=Staff,DC=nexus,DC=contoso,DC=com’ -Filter “DirectReports -like ‘*’” | Select-Object -Property ,@{Name=‘PrimarySmtpAddress’;Expression={$_.sAMAccountName + ‘-reports@contoso.com’}}).primarysmtpaddress
$Lists = (Get-DynamicDistributionGroup -Filter {Alias -like "
-reports"}).PrimarySMTPAddress
$Compare = Compare-Object -ReferenceObject $Managers -DifferenceObject $Lists | ?{$_.sideIndicator -eq “<=”} |select inputobject

Step 4: Execute comparison operation and create missing groups

$Compare | foreach {

$SamAccountName = $_.InputObject.split(“-”)[0]
$Display = (Get-ADuser $SamAccountName -Properties Name).Name + “'s Direct Reports”
$CN = (Get-Recipient $SamAccountName).DistinguishedName
$ManagerSMTP = ($SamAccountName + “@contoso.com”)
$Alias = ($SamAccountName.split(“.”)[0] + “-reports”)
$SMTP = ($SamAccountName.split(“.”)[0] + “-reports@contoso.com”)

Try {
New-DynamicDistributionGroup -Name $Display -PrimarySmtpAddress $SMTP -RecipientFilter “Manager -eq ‘$CN’ -or UserPrincipalname -like ‘$ManagerSMTP’” -WhatIf
Write-Log -Message “Group $SMTP was successfully created” -Path $LogPath -Level Info
}
Catch {
Write-Log -Message "Failed to create $SMTP” -Path $LogPath -Level Error
Send-MailMessage -Subject “DDG creation failed” -body “Failed to create $SMTP” -From it-auto@contoso.com -To hjacobsen@contoso.com -SmtpServer smtp.contoso.com

}

If ($error -eq $NULL) {
Write-Log -Message “Script has completed with no errors” -Path $LogPath -Level Info
}
Else {
Write-Log -Message “Script has completed with the following errors. $error” -Path $LogPath -Level Error
}
}

[/pre]

Are you dot-sourcing the script when you run it? If not, then none of the variables in the script should exist in your session after you run the script. So, if $ManagerSMTP exists, then it had to have been created outside of that script.

Admittedly, I am not 100% familiar with dot sourcing but my understanding is that allows referencing a different script, which I have not done in this instance. The only script is what I originally posted.