「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.02.07
XML
カテゴリ: PowerShell


PowerShellからトースト通知を表示し、同時に音楽を再生する方法は、以下の手順に従って実行できます。


まず、トースト通知を表示するために PowerShell スクリプトを作成します。以下は、トースト通知を表示する基本的なスクリプトの例です。

powershell

Copy code

# トースト通知を表示する関数

function Show-ToastNotification {

    [CmdletBinding()]

    param (

        [string]$Title,

        [string]$Message

    )


    # トースト通知の XML テンプレートを定義

    $toastXmlTemplate = @"

<toast activationType="foreground">

    <visual>

        <binding template="ToastGeneric">

            <text>$Title</text>

            <text>$Message</text>

        </binding>

    </visual>

</toast>

"@


    # XML を Windows の通知システムに送信してトースト通知を表示

    $toastXml = [Windows.Data.Xml.Dom.XmlDocument]::new()

    $toastXml.LoadXml($toastXmlTemplate)

    $toastNotifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("AppName")

    $toastNotification = [Windows.UI.Notifications.ToastNotification]::new($toastXml)

    $toastNotifier.Show($toastNotification)

}


# トースト通知を表示

Show-ToastNotification -Title "タイトル" -Message "メッセージ"

音楽を再生するために、System.Media.SoundPlayer クラスを使用します。以下は、トースト通知と同時に音楽を再生する PowerShell スクリプトの例です。

powershell

Copy code

# 必要な名前空間を読み込む

Add-Type -AssemblyName PresentationCore,PresentationFramework


# トースト通知を表示する関数

function Show-ToastNotificationWithSound {

    [CmdletBinding()]

    param (

        [string]$Title,

        [string]$Message,

        [string]$SoundFilePath

    )


    # トースト通知を表示

    Show-ToastNotification -Title $Title -Message $Message


    # 音楽を再生

    $soundPlayer = New-Object System.Media.SoundPlayer

    $soundPlayer.SoundLocation = $SoundFilePath

    $soundPlayer.PlaySync()

}


# トースト通知と音楽を表示

Show-ToastNotificationWithSound -Title "タイトル" -Message "メッセージ" -SoundFilePath "C:\Path\To\Your\SoundFile.wav"

このスクリプトでは、Show-ToastNotificationWithSound 関数を使用してトースト通知を表示し、同時に指定された音楽ファイルを再生します。-SoundFilePath パラメータには、再生したい音楽ファイルのパスを指定してください。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.02.07 07:03:11


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: