PowerShell – die besten Beiträge

Wie kann ich ein PowerShell-Skript mit dem X-Button beenden?

Ich habe eine MessageBox und möchte das Skript beenden, sobald X gedrückt wird.

Kann ich eine Variable hochzählen, wenn ein anderer Button gedrückt wird? Eine Variable, die verhindert, dass mein Skript beendet wird? Mit onClick oder so?

$version = "xy"
Add-Type -AssemblyName System.Windows.Forms

[System.Windows.Forms.Application]::EnableVisualStyles()

$START = New-Object system.Windows.Forms.Form
$START.ClientSize = '400,282'
$START.text = $version
$START.TopMost = $false
$Label1a = New-Object system.Windows.Forms.Label
$Label1a.text = "Inhaltt:"
$Label1a.AutoSize = $true
#$Label1a.width = 25
#$Label1a.height = 10
$Label1a.location = New-Object System.Drawing.Point(9, 24)
$Label1a.Font = 'Microsoft Sans Serif,10'
$Button1a = New-Object system.Windows.Forms.Button
$Button1a.text = "OK"
$Button1a.width = 381
$Button1a.height = 30
$Button1a.location = New-Object System.Drawing.Point(9, 243)
$Button1a.Font = 'Microsoft Sans Serif,10'
$Button1a.add_Click({$START.close()}) #MessageBox wird geschlossen
$TextBox1a = New-Object system.Windows.Forms.TextBox
#$TextBox1a.multiline = $false
$TextBox1a.text = "Inhalt"
$TextBox1a.width = 55
$TextBox1a.height = 20
$TextBox1a.location = New-Object System.Drawing.Point(9, 45)
$TextBox1a.Font = 'Microsoft Sans Serif,10'
$TextBox2a = New-Object system.Windows.Forms.TextBox
#$TextBox2.multiline = $false
$TextBox2a.text = "Inhalt"
$TextBox2a.width = 381
$TextBox2a.height = 20
$TextBox2a.location = New-Object System.Drawing.Point(9, 119)
$TextBox2a.Font = 'Microsoft Sans Serif,10,style=Italic'
$Label2a = New-Object system.Windows.Forms.Label
$Label2a.text = "Inhalt:" #$inputpath #Variable muss noch definiert werden
$Label2a.AutoSize = $true
#$Label2a.width = 25
#$Label2a.height = 10
$Label2a.location = New-Object System.Drawing.Point(9, 98)
$Label2a.Font = 'Microsoft Sans Serif,10'
$Label3a = New-Object system.Windows.Forms.Label
$Label3a.text = "Inhalt:" #$outputpath
$Label3a.AutoSize = $true
#$Label3a.width = 25
#$Label3a.height = 10
$Label3a.location = New-Object System.Drawing.Point(9, 172)
$Label3a.Font = 'Microsoft Sans Serif,10'
$TextBox3a = New-Object system.Windows.Forms.TextBox
#$TextBox3a.multiline = $false
$TextBox3a.text = "Inhalt"
$TextBox3a.width = 381
$TextBox3a.height = 20
$TextBox3a.location = New-Object System.Drawing.Point(9, 192)
$TextBox3a.Font = 'Microsoft Sans Serif,10,style=Italic'
$START.controls.AddRange(@($Label1a, $Button1a, $TextBox1a, $TextBox2a, $Label2a, $Label3a, $TextBox3a, $Label1ab))

[void]$START.ShowDialog()
Computer, Windows, programmieren, Code, Script, PowerShell, Powershell ISE

Wie kann ich im folgendem Powershell Script eine Farbige Ausgabe erzielen?

Habe folgenden Code, welcher auch funktioniert: #_________________________HAUPTPROGRAMM_______________________________________________________________________________________________________________________________________________________________________#

$empfaengerMail = Get-Content "C:\Users\ms\Desktop\empfaenger.txt"
$datum = get-date -uFormat "%d.%m.%Y"
$wochentag = Get-Date -format dddd
$kalenderwoche = [System.Globalization.DateTimeFormatInfo]::CurrentInfo.Calendar.GetWeekOfYear((get-date),2,1)
$servers = Get-Content "C:\Users\ms\Desktop\Servers.txt"

    $ab = gwmi Win32_LogicalDisk -ComputerName $servers -Filter "DriveType='3'" | select Systemname,DeviceID,@{n="Speicherplatz (GB)";e={"{0:F2}" -f ($_.Size / 1GB)}},@{n="Freier Speicherplatz (GB)";e={"{0:F2}" -f ($_.Freespace / 1GB)}}
    $ab | Sort-Object Systemname | Format-List -GroupBy Systemname | Out-File "C:\Speicherplatz CST-Server\Speicherplatz_$datum.txt" -Encoding UTF8



#_________________________VERSENDEN DER LOGFILES_____________________________________________________________________________________________________________________________________________________________#


#_________________________VERSENDEN VIA SMTP_________________________________________________________________________________________________________________________________________________________________#
$emailSmtpServer = "dominica.cst.lokal"
$emailSmtpServerPort = "587"
$emailSmtpUser = "benutzername"
$emailSmtpPass = "passwort"
$attachment = "C:\Speicherplatz CST-Server\Speicherplatz_$datum.txt"

$attach = New-Object Net.Mail.Attachment($attachment)
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.Attachments.Add($attach) 
$emailMessage.From = "Speicherplatz Manager <support@creativesoftware.de>"
$emailMessage.To.Add( "support@creativesoftware.de" )
$emailMessage.Subject = "CST: Speicherplatz Übersicht am $wochentag KW$kalenderwoche"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
<p>Speicherplatz von CST in Übersicht</p>
<p></p>
<p>Anbei der Speicherplatz als Textdatei angehängt!</p>
<p></p>
<p>Stand: $datum</p>
"@
 
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
 
$SMTPClient.Send( $emailMessage )


Dies wird alles in einer schönen HTML Tabelle ausgegeben. Nun möchte ich, dass das Feld mit Freespace rot wird, wenn es unter 15% fällt.... Ich weiß aber nicht wie das geht

HTML, PowerShell

Meistgelesene Beiträge zum Thema PowerShell