Task from the Task Scheduler doesn't load localized string

First of all files’ simplified structure.

root\en-US\test.psd1
root\test.ps1
root\test.psd1

root\en-US\test.psd1

ConvertFrom-StringData -StringData @'
CleanUpTaskToast = Cleaning up unused Windows files and updates starts in a minute
'@

root\test.ps1

Clear-Host

Remove-Module -Name test -Force -ErrorAction Ignore
Import-Module -Name .\test.psm1 -PassThru -Force

Import-LocalizedData -BindingVariable Global:Localization -FileName test

CreateCleanUpTask

root\test.psd1

function CreateCleanUpTask
{
    function xml
    {
        $app = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\cleanmgr.exe"

        [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
        $Template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01
        [xml]$ToastTemplate = ([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($Template).GetXml())

        [xml]$ToastTemplate = @"
<toast launch="app-defined-string">
    <visual>
        <binding template="ToastGeneric">
            <text>$($Localization.CleanUpTaskToast)</text>
        </binding>
    </visual>
</toast>
"@

        $ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
        $ToastXml.LoadXml($ToastTemplate.OuterXml)

        [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($app).Show($ToastXml)
    }

    $test = (Get-Command -Name xml).ScriptBlock

    $Action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-noexit $test"
    $Trigger = New-ScheduledTaskTrigger -Daily -DaysInterval 90 -At 9am
    $Settings = New-ScheduledTaskSettingsSet -Compatibility Win8 -StartWhenAvailable
    $Principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -RunLevel Highest
    $Parameters = @{
        "TaskName"      = "test"
        "Principal"     = $Principal
        "Action"        = $Action
        "Settings"      = $Settings
        "Trigger"       = $Trigger
    }
    Register-ScheduledTask @Parameters -Force
}

So the problem is that despite the string CleanUpTaskToast was loaded from the localization file, the variable is not interpreted as a string but as $($Localization.CleanUpTaskToast) whatever I do. It does work but not within running from the Task Scheduler. I know the more rational method is to try to run script for task from a file, but I do want to create totally independent task. I know there are some errors appear after task running but it’s not the problem (it can be solved with tripple double-quotes). It is just the origin version of code.
So can anyone make the script to load localized string while creating this task?

 

 

Rather than using a global variable, it should be passed to the function. Why create a function to get a scriptBlock rather than just defining a variable as a a scriptblock? Would also recommend using more descriptive variables. When reading the code there is $test and task is test and a function named xml with [xml]. It just makes it difficult to read and troubleshoot, just .02. Here is something you can try:

function New-CleanUpTask {
    param (
        $Localization
    )
 
    $sbToastNotification = {
        $app = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\cleanmgr.exe"

        [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
        $Template = [Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01
        [xml]$ToastTemplate = ([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($Template).GetXml())
    
[xml]$ToastTemplate = @"
<toast launch="app-defined-string">
<visual>
    <binding template="ToastGeneric">
        <text>$($Localization.CleanUpTaskToast)</text>
    </binding>
</visual>
</toast>
"@
    
        $ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
        $ToastXml.LoadXml($ToastTemplate.OuterXml)
    
        [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($app).Show($ToastXml)
    }
 
    $Action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-noexit $sbToastNotification"
    $Trigger = New-ScheduledTaskTrigger -Daily -DaysInterval 90 -At 9am
    $Settings = New-ScheduledTaskSettingsSet -Compatibility Win8 -StartWhenAvailable
    $Principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -RunLevel Highest
    $Parameters = @{
        "TaskName"      = "test"
        "Principal"     = $Principal
        "Action"        = $Action
        "Settings"      = $Settings
        "Trigger"       = $Trigger
    }
    Register-ScheduledTask @Parameters -Force
}

Remove-Module -Name test -Force -ErrorAction Ignore
Import-Module -Name .\test.psm1 -PassThru -Force
 
Import-LocalizedData -BindingVariable "Localization" -FileName test
 
New-CleanUpTask -Localization $Localization

I do not think this is a solution. I use

Import-LocalizedData -BindingVariable Global:Localization -FileName Sophia

Also actually i presented this function as simplified example. The main project ishttps://github.com/farag2/Windows-10-Sophia-Script . As you can see I use preset file to users edit it by themselves. So I have no idea how to make script to interpret this variable as a localized string.