次の方法で共有

PowerShellの実行について

Anonymous
2021-01-27T03:27:10+00:00

PowerShellについては全くの素人です。

あるフォルダを監視して、変更があったら通知するコードをサイトからコピーして改変しました。

VSCode上で実行すると目的通り、フォルダが更新されると通知が来るのですが、ps1ファイルを右クリックで「PowerShellで実行」をすると、エラーは何も出ないのですが、通知も来ません。

なぜだか全くわからないのですが、ご教授頂けますでしょうか。

SHOW TOAST

function ShowToast {

    [CmdletBinding()]

    PARAM (

        [Parameter(Mandatory=$true)][String] $title,

        [Parameter(Mandatory=$true)][String] $message,

        [Parameter(Mandatory=$true)][String] $detail

    )

    [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

    $app_id = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'

    $content = @"

<?xml version="1.0" encoding="utf-8"?>

<toast scenario="reminder">

    <visual>

        <binding template="ToastGeneric">

            <text>$($title)</text>

            <text>$($message)</text>

            <text>$($detail)</text>

            <image placement="appLogoOverride" hint-crop="circle" hint-align="center" src="画像のパス" />

        </binding>

    </visual>

    <actions>

        <input id="snoozeTime" type="selection" defaultInput="15">

            <selection id="1" content="1 minute"/>

            <selection id="15" content="15 minutes"/>

            <selection id="60" content="1 hour"/>

            <selection id="240" content="4 hours"/>

            <selection id="1440" content="1 day"/>

        </input>

        <action activationType="system" arguments="snooze" hint-inputId="snoozeTime" content="" />

        <action activationType="system" arguments="dismiss" content=""/>

    </actions>

</toast>

"@

    $xml = New-Object Windows.Data.Xml.Dom.XmlDocument

    $xml.LoadXml($content)

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

}

SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO

    $watcher = New-Object System.IO.FileSystemWatcher

    $watcher.Path = "監視するフォルダパス"

    $watcher.Filter = "*.*"

    $watcher.IncludeSubdirectories = $true

    $watcher.EnableRaisingEvents = $true  

DEFINE ACTIONS AFTER AN EVENT IS DETECTED

    $action = { 

        $path = $Event.SourceEventArgs.FullPath

        $FileName = $Event.SourceEventArgs.Name

        $changeType = $Event.SourceEventArgs.ChangeType

        $msg = "メッセージ"

        ShowToast -title "フォルダ監視" -message $msg -detail $changeType

        $logline = "$(Get-Date), $changeType, $path"
        Add-content "ログ出力先" -value $logline
    }

DECIDE WHICH EVENTS SHOULD BE WATCHED 

    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 900}

家庭向け Windows | Windows 10 | 設定

ロックされた質問。 この質問は、Microsoft サポート コミュニティから移行されました。 役に立つかどうかに投票することはできますが、コメントの追加、質問への返信やフォローはできません。

0 件のコメント コメントはありません

1 件の回答

並べ替え方法: 最も役に立つ
  1. Anonymous
    2021-01-28T01:38:42+00:00

    ​まさお(偽名) さん、こんにちは。
    マイクロソフト コミュニティをご利用いただき、ありがとうございます。

    PowerShell については一般向けのこのマイクロソフト コミュニティでは情報が集まりにくいかもしれません。

    今回の内容であれば、開発者の方々が多く情報交換をされている MSDN フォーラムの Windows PowerShell のカテゴリに投稿してみるとアドバイスが寄せられやすくなると思います。

    MSDN フォーラム

    よければそちらを利用してみてくださいね。

    この回答は役に立ちましたか?

    0 件のコメント コメントはありません