Hi all, Discovered UD a couple of weeks ago (loving the product and forum support) and have been working on getting it setup in our environment. We’re an IT Provider so we have a lot of multi tenant resources we have to access, including managing our client’s O365.
I have a scheduled endpoint to run every so often and get our client’s names, primary domain and tenantid(customerid) from the partnercenter module and is stored in $cache:365partners.
I then made a page to take that list, using New-UDSelect / Option to give me a drop down of the cached client info, when you select a client I have it onClick import the partnercenter module again, get an access token, and connect to the clients exchange environment, this all works currently.
However, when I try to run a command, like Get-AcceptedDomains or Get-Mailbox, they run without error but don’t save in their respective variables. I know they run because I see the output of the commands in the log/console. The $Session:Variables are $null and I can see they are $null in the Diagnostics area.
What I think should work (which I’m sure is wrong) is this:
New-UDPage -Name 'Client Mailboxes' -Id 'ServiceAppsM365ClientMailboxes' -AuthorizationPolicy 'Service' -Content {
New-UDHeading -Text 'Client Mailboxes' -Size 5
New-UDButton -Text 'Try' -OnClick{
Show-UDModal -Content {
New-UDSelect -Label 'Select Client' -Option {
New-UDSelectOption -Name 'Choose Client' -Value 'none'
foreach ($client in $Cache:365Partners){
New-UDSelectOption -Name $client.Name -Value $client.CustomerId
}
} -OnChange {
$Session:SelectedTenantId = $EventData
$Session:SelectedCustomer = $Cache:365partners|Where-Object -Property CustomerId -EQ -Value "$Session:SelectedTenantId"
Show-UDToast -Message "You selected $Session:SelectedTenantId" -Duration 15000
}
New-UDButton -Text 'Select' -OnClick {
Import-Module "$env:PSRuntimeModulesPath\PartnerCenter" -Force
$token = New-PartnerAccessToken -ApplicationId 'a0c73c16-a7e3-4564-9a95-2bdf47383716'-RefreshToken $env:EXOTOKEN -Scopes 'https://outlook.office365.com/.default' -Tenant $Session:SelectedTenantId
$credential = New-Object System.Management.Automation.PSCredential('myemail@domain.com', (ConvertTo-SecureString "Bearer $($token.AccessToken)" -AsPlainText -Force))
$exosession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.outlook.com/powershell-liveid?DelegatedOrg=$($Session:SelectedCustomer.Domain)&BasicAuthToOAuthConversion=true" -Credential $credential -Authentication Basic -AllowRedirection
Import-Module (Import-PSSession -Session $exosession -AllowClobber -DisableNameChecking) -Global
$Session:AcceptedDomains = Get-AcceptedDomain
$Session:AllMailboxes = Get-Mailbox -ResultSize Unlimited -Filter {RecipientTypeDetails -eq "UserMailbox"}
Show-UDToast -Message "Found $($Session:AllMailboxes.count) mailboxes and $($Session:AcceptedDomains.Count) acceptible Domains for $($Session:SelectedCustomer.Name)"
}
}
}
}
I’ve tried several different switches for import-pssession and it doesn’t seem to matter, “-AllowClobber, -DisableNameChecking” also tried
Import-Module (Import-PSSession -Session $exosession -AllowClobber -DisableNameChecking) -Global
which is what I had to do for my azure automation ps runbooks but doesn’t work here.
Any assistance and showing me where I’m wrong or not understanding scopes/runspaces would be greatly appreciated.