Pssession variables in new-udgrid

Hi

i create a new-pssession to connect to exchange and run commands to get mailbox info and set the results as variables

im having issues getting the variables to output to a new-udgrid

from research it seems to be caused by the pssession being in a different endpoint than the new-udgrid

ive tried using Invoke-Command -Session $pssession -ScriptBlock as a work around but then i receive the error Assignment statements are not allowed in restricted language mode or a Data section. and seems the fix for that requires making a change on the exchange server which i dont have rights to do

is the only way to use pssession variables in a new-udgrid is by using invoke-command?

sample code before trying the invoke command below

any help much appreciated

$session:pssession = New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri http://exchangeServer1.msg.net/powershell

Import-PSSession $session:pssession -AllowClobber

$session:mbszinfo = Get-MailboxStatistics -Identity $session:userinput | Select-Object DisplayName, Itemcount, TotalItemSize, StorageLimitStatus

$session:mbioutput= @(
[PSCustomObject]@{Name=“Display Name”; Value=$session:mbszinfo.displayname}
[PSCustomObject]@{Name=“Item Count”; Value = $session:mbszinfo.itemcount}
[PSCustomObject]@{Name=“Total Item Size”; Value = $session:mbszinfo.totalitemsize}
[PSCustomObject]@{Name=“Storage Limit Status”; Value = $session:mbszinfo.storagelimitstatus}

)

New-UDGrid -id "results" -Title "Results" -NoPaging -Headers @("Name", "Value") -Properties @("Name", "Value")  -Endpoint {
         $session:mbioutput | out-udgriddata
}

Hi @Srichman0128

I’ve done this, in order to workaround mulitilingual folder-names in Exchange Online.

Easiest way to archieve this is to construct the scriptblock first as a string, then convert it into a scriptblock to in turn use invoke-command.

#workaround to pass variables to the "no language" mode in Exchange Online, preconstructed scriptblocks
        $Norwegian = ($Mailbox +":\Kalender")
        $English = ($Mailbox + ":\Calendar")
        $NorwegianScriptBlock = [scriptblock]::create("Get-MailboxFolderPermission -identity $Norwegian")
        $EnglishScriptBlock = [scriptblock]::create("Get-MailboxFolderPermission -identity $English")

        if ([bool](Invoke-Command -session $Session:ScriptO365Session -ErrorAction SilentlyContinue -scriptblock $NorwegianScriptBlock)) {
            #Calendar is Norwegian
            $CurrLang = "Norwegian"
            $current = (Invoke-Command -session $Session:ScriptO365Session -scriptblock $NorwegianScriptBlock) | Select-Object User, AccessRights, SharingPermissionFlags
        } 
        if ([bool](Invoke-Command -session $Session:ScriptO365Session -ErrorAction SilentlyContinue -scriptblock $EnglishScriptBlock)) {
            #Calendar is English
            $CurrLang = "English"
            $current = (Invoke-Command -session $Session:ScriptO365Session -scriptblock $EnglishScriptBlock) | Select-Object User, AccessRights, SharingPermissionFlags
        }

Give me a shout if you need any further guidance!

1 Like

thanks for the reply
this allowed the command to complete successfully without the language error on the ise
but the variables still do not display on the udgrid on my dashboard any ideas?

sample script below

$session:pssession = New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri http://exserver1.msg.net/powershell

$mbszinfoScriptBlock = [scriptblock]::create(“Get-MailboxStatistics -Identity $session:userinput”)

$mbszinfo = (Invoke-Command -session $session:pssession -scriptblock $mbszinfoScriptBlock) | Select-Object DisplayName, Itemcount, TotalItemSize, StorageLimitStatus

$session:ambioutput= @(
[PSCustomObject]@{Name=“Display Name”; Value=$mbszinfo.displayname}
[PSCustomObject]@{Name=“Item Count”; Value = $mbszinfo.itemcount}
[PSCustomObject]@{Name=“Total Item Size”; Value = $mbszinfo.totalitemsize}
[PSCustomObject]@{Name=“Storage Limit Status”; Value = $mbszinfo.storagelimitstatus}
)

Sync-UDElement -id "results"

set-UDElement -Id outputbox -content {
New-UDGrid -id “results” -Title “Results” -NoPaging -Headers @(“Name”, “Value”) -Properties @(“Name”, “Value”) -Endpoint {

      $session:ambioutput  | out-udgriddata

}

i had | fl | out-string on my variables from when i was outputting as raw text before outputting to grid, once removed all is well thanks

1 Like