Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Hilfe benötigt: Msg-Box als Job ausführen

$
0
0

Hallo zusammen,

ich überwache mehrere Textdateien. Wenn ich nun eine bestimmte Änderung feststelle, soll eine Msg-Box erscheinen. Damit das Script aber im Hintergrund weiter arbeitet möchte ich diese Msg-Box gerne als Job ausführen! Soweit alles kein Problem.

Ich möchte aber verhindern, das die Box mehrfach angezeigt wird! Hat hier einer eine Idee? Hier mal mein Code wie ich mir das dachte, leider wird die $Global:ReturnVariable nicht ersetzt!

Ist mein ansatz so richtig oder gibt es sonst eine Möglichkeit?

Vielen Dank schonmal

Hier mal der Code:

cls

$Global:ReturnVariable = "Ok"

$func = {
	FUNCTION Show-MsgBox {

					#Region Parameter
				 	[CmdletBinding()]
				    param(
						[Parameter(Position = 0, Mandatory = $true)]
						[ValidateNotNullorEmpty()]
						[string]$Title,

				    	[Parameter(Position = 1, Mandatory = $true)]
						[ValidateNotNullorEmpty()]
						[string]$Prompt,

				    	[Parameter(Position = 2, Mandatory = $false)]
						[ValidateSet("Information", "Question", "Critical", "Exclamation")]
						[string]$Icon = "Information",

				    	[Parameter(Position = 3, Mandatory = $false)]
						[ValidateSet("OKOnly", "OKCancel", "AbortRetryIgnore", "YesNoCancel", "YesNo", "RetryCancel")]
						[string]$BoxType = "OkOnly",

				    	[Parameter(Position = 4, Mandatory = $false)]
						[ValidateSet(1,2,3)]
						[int]$DefaultButton = 1,

						[Parameter(Position = 5, Mandatory = $false)]
						[switch] $TopMost

				    )
					#Endregion Parameter

					BEGIN {[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic") | Out-Null}

					PROCESS {
						SWITCH ($Icon) {
					        "Question" 			{$vb_icon = [microsoft.visualbasic.msgboxstyle]::Question }"Critical" 			{$vb_icon = [microsoft.visualbasic.msgboxstyle]::Critical}"Exclamation" 		{$vb_icon = [microsoft.visualbasic.msgboxstyle]::Exclamation}"Information" 		{$vb_icon = [microsoft.visualbasic.msgboxstyle]::Information}
						} #END SWITCH ($Icon)

						SWITCH ($BoxType) {
					        "OKOnly" 			{$vb_box = [microsoft.visualbasic.msgboxstyle]::OKOnly}"OKCancel" 			{$vb_box = [microsoft.visualbasic.msgboxstyle]::OkCancel}"AbortRetryIgnore" 	{$vb_box = [microsoft.visualbasic.msgboxstyle]::AbortRetryIgnore}"YesNoCancel" 		{$vb_box = [microsoft.visualbasic.msgboxstyle]::YesNoCancel}"YesNo" 			{$vb_box = [microsoft.visualbasic.msgboxstyle]::YesNo}"RetryCancel" 		{$vb_box = [microsoft.visualbasic.msgboxstyle]::RetryCancel}
						} #END SWITCH ($BoxType)

						SWITCH ($Defaultbutton) {
					        1 					{$vb_defaultbutton = [microsoft.visualbasic.msgboxstyle]::DefaultButton1}
					        2 					{$vb_defaultbutton = [microsoft.visualbasic.msgboxstyle]::DefaultButton2}
					        3 					{$vb_defaultbutton = [microsoft.visualbasic.msgboxstyle]::DefaultButton3}
						} #END SWITCH ($Defaultbutton)

						SWITCH ($TopMost) {
					        "TopMost" 			{$vb_topmost = [microsoft.visualbasic.msgboxstyle]::MsgBoxSetForeground}
						} #END SWITCH ($TopMost)

						$popuptype = $vb_icon -bor $vb_box -bor $vb_defaultbutton -bor $vb_topmost

						$Global:ReturnVariable	= [Microsoft.VisualBasic.Interaction]::MsgBox($Prompt,$popuptype,$title)
					}

					END {}

				} #END FUNCTION Show-MsgBox
}

FUNCTION TimerStart {

				#REGION Parameters
				[cmdletbinding()]
				param(
				[parameter(Mandatory=$true)]$SourceId,
				[parameter(Mandatory=$true)]$timername,
				[ValidateSet("Elapsed","Tick")][string]$Event = "Elapsed",
				[parameter(Mandatory=$true)][long]$time,
				[parameter(Mandatory=$false)]$action)
				#ENDREGION Parameters

				$timername = new-object timers.timer
				$timername.Interval = ($time*1000)

				#Timer registrierung löschen
				Register-ObjectEvent -InputObject $timername -EventName $Event –SourceIdentIFier $SourceId -Action $action | Out-Null

				#Timer starten
				$timername.start()
			}

TimerStart -SourceId Show -Timername Timer_Show -Event Elapsed -Time 5 -Action {

	IF ($Global:ReturnVariable -eq "Ok"){

		$Global:ReturnVariable = "nOk"

		$Job = Start-Job -InitializationScript $func -scriptblock {

			#Popupfenster anzeigen
			Show-MsgBox `
				-Title "Titel" `
				-Prompt "Text" `
				-Icon "Information" `
				-BoxType "OkOnly" `
				-DefaultButton "1" `
				-TopMost
		}
	}
}


Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>