Private Sub _save_Click(sender As Object, e As EventArgs) Handles _save.Click
Dim newGroupName As String = _groupName.Text
CheckPrimarySmtpExOnline(newGroupName)
End Sub
Private Function CheckPrimarySmtpExOnline(newGroupName As String) As Boolean
Dim _ExOnlineAppId As String = ""
Dim _ExOnlineCert As String = ""
Dim _result As Boolean = False
Try
Dim initialSessionState As InitialSessionState = InitialSessionState.CreateDefault()
Dim modules As String() = {"ExchangeOnlineManagement"}
initialSessionState.ImportPSModule(modules)
Dim runspace As Runspace = RunspaceFactory.CreateRunspace(initialSessionState)
Dim powershell As System.Management.Automation.PowerShell = System.Management.Automation.PowerShell.Create()
powershell.Runspace = runspace
runspace.Open()
Dim myScript As String = """
Param (
[Parameter(mandatory=$true)]
[string]$ExOnlineAppID,
[string]$ExOnlineCert,
[string]$name
)
try {
Set-Executionpolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
$splat = @{
appid = $ExOnlineAppID
Organization = 'myorg.onmicrosoft.com'
CertificateThumbprint = $ExOnlineCert
CommandName = @('Get-Recipient', 'Get-Mailbox', 'New-Mailbox', 'Add-MailboxPermission', 'Remove-MailboxPermission')
Showbanner = $false
ErrorAction = 'Stop'
}
Connect-ExchangeOnline @splat
$Recipient = Get-Recipient [email protected] | Select PrimarySmtpAddress
}
catch {
$PSItem | Export-CLIXML 'D:..error.xml'
}
Return $Recipient.PrimarySmtpAddress
Disconnect-ExchangeOnline -Confirm:$false
"""
powershell.AddScript(myScript)
powershell.AddParameter("ExonlineAppID", _ExOnlineAppId)
powershell.AddParameter("ExOnlineCert", _ExOnlineCert)
powershell.AddParameter("name", newGroupName)
Dim results As Collection(Of PSObject) = powershell.Invoke(Nothing)
If results.Count > 0 Then
Dim firstResult As PSObject = results(0)
If firstResult IsNot Nothing Then
_result = True
End If
End If
Catch ex As Exception
Throw New Exception("Error checking primary SMTP address in Exchange Online", ex)
End Try
End Function
So I’m trying to implement in an existing app that when you try to create a new group this checks if the email which will be applied to this is not present in ExchangeOnline.
I test only the script in PS and it’s retrieving the PrimarySmtpAddress(if present).
The issue is when I store the PSObject in results variable… I’m not capable to find the value for primarysmtpaddress in this PSObject. It returns results
So the validation I’m doing always finds something, always returns true. I tried some variations but the return types are always the same.
Is there any way to do this validation? Thanks in advance.