2019年8月15日木曜日

PowerShell Windows10 トースト通知でプログレスバーを表示。|PowerShell Windows10 Toast notification with progress bar.



[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null;
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null;
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null;

Function ToastShow {
Param(
$AppId
,$tag
,$group
,$text
,$title
,$status
,$progressValue
,$progressValueString
)

<#duration="long" 25sec|"short 7sec"#>
$template = @"
<toast duration="long" >
    <visual>
        <binding template="ToastGeneric">
            <text>{text}</text>
            <progress
                title="{title}"
                value="{progressValue}"
                valueStringOverride="{progressValueString}"
                status="{status}"/>
        </binding>
    </visual>
</toast>
"@;

$template=$template.Replace("{text}",$text );
 
    $xml = New-Object Windows.Data.Xml.Dom.XmlDocument;
    $xml.LoadXml($template);

$toast = New-Object Windows.UI.Notifications.ToastNotification $xml;

$toast.Tag = $tag;
$toast.Group = $group;


$DataDictionary = New-Object 'system.collections.generic.dictionary[string,string]';
$DataDictionary.Add("title", $title);
$DataDictionary.Add("status", $status);
$DataDictionary.Add("progressValue", $progressValue);
$DataDictionary.Add("progressValueString", $progressValueString);
$data = [Windows.UI.Notifications.NotificationData]::new( $DataDictionary );

$toast.Data=$data;



    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId).Show($toast);
}

Function ToastUpdate {
Param(
$AppId
,$tag
,$group
,$progressValue
,$progressValueString
)
$DataDictionary = New-Object 'system.collections.generic.dictionary[string,string]';
$DataDictionary.Add("progressValue", $progressValue );
$DataDictionary.Add("progressValueString", $progressValueString);

$data = [Windows.UI.Notifications.NotificationData]::new( $DataDictionary );

    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppId).Update($data, $tag,$group);
}

Function ToastRemove {
Param(
$AppId
,$tag
,$group
)
[Windows.UI.Notifications.ToastNotificationManager]::History.Remove($tag,$group,$AppId);
}


$AppId = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe";
$tag = "tag001";
$group = "group001";
$text = "プログレスバー";
$title = "タイトル";
$status = "ステータス";
$progressValue = 0.0;
$Val=0;
$progressValueString="$Val/10";

ToastShow $AppId $tag $group $text $title $status $progressValue $progressValueString;


for($n=0; $n -lt 10; $n++) {
Start-Sleep -m 1000;
$progressValue += 0.1;
$Val++;
$progressValueString="$Val/10";
ToastUpdate $AppId $tag $group $progressValue $progressValueString;
        }

Start-Sleep -m 1000;
ToastRemove $AppId $tag $group  ;

0 件のコメント:

コメントを投稿