Событие PowerChell для всплывающих уведомлений onClick для отображения сообщения из базы данных. - PullRequest
0 голосов
/ 20 октября 2019

У меня есть событие onclick для всплывающей подсказки, генерируемой сценарием. после нажатия он помечает конкретную запись в таблице mssql как прочитанную и ссылается на URL-адрес веб-приложения, отображающего сообщение. Как лучше всего показать это сообщение в окне сообщения или окне Windows CMD?

    function showNotification($messageID)
    {
        $notification = New-Object System.Windows.Forms.NotifyIcon
        $notification.Icon = [System.Drawing.SystemIcons]::Information
        $notification.BalloonTipTitle = "***Alert Message***"
        $notification.BalloonTipIcon = "Warning"
        $notification.BalloonTipText = "Click here to view"
        $notification.Visible = $True

        #[console]::beep(2000, 1000)
        ## Register a click event with action to take based on event
        #Balloon message clicked
        $event = "BalloonClicked_event" + $messageID
        $eventClose = "BalloonClosed_event" + $messageID

        #Remove any registered events related to notifications
        Remove-Event $event -ea SilentlyContinue
        Unregister-Event -SourceIdentifier $event -ea silentlycontinue
        Remove-Event $eventClose -ea SilentlyContinue
        Unregister-Event -SourceIdentifier $eventClose -ea silentlycontinue
        Remove-Event Disposed -ea SilentlyContinue
        Unregister-Event -SourceIdentifier Disposed -ea silentlycontinue


        register-objectevent -InputObject $notification BalloonTipClicked $event -MessageData $messageID  {
            #Update message
            $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
            $SqlConnection.ConnectionString = "Server=$SQLServer;Database=$SQLDBName;Integrated Security=True;"

            $Query = "update alerts.dbo.groupmessages set isRead = 1 
                    where pk = @MessageID 
                    and userFK = (select pk from alerts.dbo.users where username = @Username) 
                    and groupFK = (select groupFK from alerts.dbo.users where username = @Username)";

            $SQLUserName = ''
            $SQLPassword = ''
            $SQLDatabase = ''
            $SQLServer = ''

            $ConnectionString = "Data Source=$SQLServer;database=$SQLDBName;User ID=$SQLUserName;Password=$SQLPassword"

            try
            {
                $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString
                $sqlConnection.Open()

                $Command = New-Object System.Data.SqlClient.SqlCommand
                $Command.Connection = $sqlConnection
                $Command.CommandText = $Query
                $Command.Parameters.Add("@MessageID", $Event.MessageData);
                $Command.Parameters.Add("@Username", $env:UserName);

                $Command.ExecuteNonQuery()
            }
            catch
            {
                #Print exception
                Write-Host $_
            }
            finally
            {
                ## Close the connection when we're done
                $sqlConnection.Close()
            }

            $URL = 'http://alertmsg/' + $Event.MessageData

            #Start InternetExplorer with your applicaiton URL
            Start-Process 'c:\Program Files\Internet Explorer\iexplore.exe' -ArgumentList $URL -WindowStyle Maximized -Verb Open

            #Kill current process
            Stop-Process -Id $PID
        } | Out-Null

        register-objectevent -InputObject $notification Click $eventClick -MessageData $messageID  {
            #Update message
            $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
            $SqlConnection.ConnectionString = "Server=$SQLServer;Database=$SQLDBName;Integrated Security=True;"

            $Query1 = "update alerts.dbo.groupmessages set isRead = 1 
                    where pk = @MessageID 
                    and userFK = (select pk from alerts.dbo.users where username = @Username) 
                    and groupFK = (select groupFK from alerts.dbo.users where username = @Username)";

            $Query2 = "select message from alerts.dbo.message where pk = @MessageID
                    where pk = @MessageID";

            $SQLUserName = ''
            $SQLPassword = ''
            $SQLDatabase = ''
            $SQLServer = ''

            $ConnectionString = "Data Source=$SQLServer;database=$SQLDBName;User ID=$SQLUserName;Password=$SQLPassword"

            try
            {
                $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString
                $sqlConnection.Open()

                $Command = New-Object System.Data.SqlClient.SqlCommand
                $Command.Connection = $sqlConnection
                $Command.CommandText = $Query1
                $Command.Parameters.Add("@MessageID", $Event.MessageData);
                $Command.Parameters.Add("@Username", $env:UserName);

                $Command.ExecuteNonQuery()
            }
            catch
            {
                #Print exception
                Write-Host $_
            }
            finally
            {
                ## Close the connection when we're done
                $sqlConnection.Close()
            }
            try {
                $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString
                $sqlConnection.Open()


                $Command = New-Object System.Data.SqlClient.SqlCommand
                $Command.Connection = $sqlConnection
                $Command.CommandText = $Query2
                $Command.Parameters.Add("@MessageID", $Event.MessageData);
                $Command.Parameters.Add("@Username", $Username);

                $DataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $Command
                $DataSet = New-Object System.Data.DataSet
                $RecordCount = $dataAdapter.Fill($dataSet, "data")

                $Data = $DataSet.Tables[0] 
                $Data.ItemArray
                [System.Windows.MessageBox]::Show($Data.ItemArray)
                # foreach ($result in $Data) {
                    #write-output "Data changed: $($result.status)"  
                    #If statement to check if your data has changed             

                    #If (Compare-Object $result.isRead 'False') {
                        #showNotification($result.pk)

                    #}
                #}
            } catch {
                #Print exception
                Write-Host $_
            } finally {
                ## Close the connection when we're done
                $sqlConnection.Close()
            }

            $URL = 'http://:3000/alertmsg/' + $Event.MessageData

            Start InternetExplorer with your applicaiton URL
            Start-Process 'c:\Program Files\Internet Explorer\iexplore.exe' -ArgumentList $URL -WindowStyle Maximized -Verb Open

            #Kill current process
            Stop-Process -Id $PID
        } | Out-Null

        #Balloon message closed
        register-objectevent $notification BalloonTipClosed $eventClose -Action {
            $notification.Dispose()
        } | Out-Null

        #Play system sound
        [System.Media.SystemSounds]::Exclamation.Play()

        #Show notification
        $notification.ShowBalloonTip(1000)
    }


    #First, check if user exists and user group is empty
    checkUserGroup

    #Call check new messages from DB for current user
    checkNewMessages

}
else
{
    "User has been idle for $TimeInactive sec, don't send notification and exit."
    [Environment]::Exit(0)


Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...