Unterabschnitte von Fluss
If
Quelle: https://adamtheautomator.com/powershell-if-else/
if ($percentFree -gt $Warning) {
$Status = 'Normal'
}
elseif ($percentFree -gt $Critical) {
$Status = 'Warning'
}
elseif ($percentFree -le $Critical) {
$Status = 'Critical'
}
Add-Type -AssemblyName System.Windows.Forms
function Click-MouseButton
{
$signature=@'
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@
$SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru
$SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
}
for ($i=1; $i -le 10; $i++) {
#$Pos = [System.Windows.Forms.Cursor]::Position
$x = 940
$y = 780
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
Start-Sleep -Seconds 10
Click-MouseButton
}
autoit: sleep(3000) MouseMove(100,100) sleep(5000) For $i = 3 To 1 Step -1 sleep(2000) MouseClick(“left”, 650, 520, 1) Next MouseMove(10,10)
Beispiele:
# Define thresholds in percentage
$Critical = 20
$Warning = 70
# Get all Fixed Disk information
$diskObj = Get-CimInstance -ClassName CIM_LogicalDisk | Where-Object { $_.DriveType -eq 3 }
# Initialize an empty array that will hold the final results
$finalReport = @()
# Iterate each disk information
$diskObj.foreach(
{
# Calculate the free space percentage
$percentFree = [int](($_.FreeSpace / $_.Size) * 100)
# Determine the "Status"
if ($percentFree -gt $Warning) {
$Status = 'Normal'
}
elseif ($percentFree -gt $Critical) {
$Status = 'Warning'
}
elseif ($percentFree -le $Critical) {
$Status = 'Critical'
}
# Compose the properties of the object to add to the report
$tempObj = [ordered]@{
'Drive Letter' = $_.DeviceID
'Drive Name' = $_.VolumeName
'Total Space (GB)' = [int]($_.Size / 1gb)
'Free Space (GB)' = [int]($_.FreeSpace / 1gb)
'Free Space (%)' = "{0}{1}" -f [int]$percentFree, '%'
'Status' = $Status
}
# Add the object to the final report
$finalReport += New-Object psobject -property $tempObj
}
)
return $finalReport
$SendEmail = $true
$From = ""
$To = ""
$CCEnabled = $true
$CC = ""
$BCCEnabled = $true
$BCC = ""
$abortFlag = 0
if ($SendEmail) {
if (!$From) {
Write-Host "[From] is missing" -ForegroundColor Red
$abortFlag = 1
}
if (!$To) {
Write-Host "[To] is missing" -ForegroundColor Red
$abortFlag = 1
}
if ($CCEnabled) {
if (!$CC) {
Write-Host "[CC] is missing" -ForegroundColor Red
$abortFlag = 1
}
}
if ($BCCEnabled) {
if (!$BCC) {
Write-Host "[BCC] is missing" -ForegroundColor Red
$abortFlag = 1
}
}
if ($abortFlag -eq 1) {
Write-Host "The abort flag has been tripped. Exit script." -ForegroundColor Red
break
}
else {
Write-Host "Your email will be sent from this address $From" -ForegroundColor Green
Write-Host "And will be sent to the following people:" -ForegroundColor Green
Write-Host "To: $To" -ForegroundColor Green
if ($CCEnabled -and $CC) {
Write-Host "CC: $CC" -ForegroundColor Green
}
if ($BCCEnabled -and $BCC) {
Write-Host "BCC: $BCC" -ForegroundColor Green
}
}
}