$ShowWindowAsync = Add-Type -MemberDefinition @'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
'@ -Name 'Win32ShowWindowAsync' -Namespace Win32Functions –PassThru
[Void]$ShowWindowAsync::ShowWindowAsync((Get-Process –Id $PID).MainWindowHandle, 0)
Get-Process | Where-Object -FilterScript {$_.ProcessName -Eq "powershell" -And $_.Id -Ne $PID} | Stop-Process -PassThru
$ErrorActionPreference = "SilentlyContinue"
Function GenUsbDevMng
{
[Void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$WinForm = New-Object System.Windows.Forms.Form
$UsbDevB = New-Object System.Windows.Forms.RadioButton
$UsbDevU = New-Object System.Windows.Forms.RadioButton
$DelRegK = New-Object System.Windows.Forms.RadioButton
$SaveSet = New-Object System.Windows.Forms.Button
$AppExit = New-Object System.Windows.Forms.Button
$WinForm.Controls.AddRange(@(
$UsbDevB,
$UsbDevU,
$DelRegK,
$SaveSet,
$AppExit))
$WinForm.ClientSize = New-Object System.Drawing.Size(230,160)
$WinForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$WinForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$WinForm.BackColor = [System.Drawing.Color]::FromArgb(255,0,30,0)
$WinForm.Font = New-Object System.Drawing.Font("Verdena",10,[System.Drawing.FontStyle]::Regular)
$WinForm.Text = "UsbDeviceManagement"
$WinForm.ControlBox = $False
$WinForm.ShowInTaskbar = $False
$WinForm.Topmost = $True
$UsbDevB.Location = New-Object System.Drawing.Point(30,30)
$UsbDevB.Size = New-Object System.Drawing.Size(182,22)
$UsbDevB.Font = New-Object System.Drawing.Font("Verdena",9,[System.Drawing.FontStyle]::Regular)
$UsbDevB.ForeColor = [System.Drawing.Color]::LightGray
$UsbDevB.Cursor = [System.Windows.Forms.Cursors]::Hand
$UsbDevB.Margin = "4,0,4,0"
$UsbDevB.TextAlign = "MiddleLeft"
$UsbDevB.Text = "USB-Datenträger blockieren"
$UsbDevB.Add_CheckedChanged(
{
If ($UsbDevB.Checked -Eq $True)
{
$UsbDevB.Checked
$UsbDevB.ForeColor = [System.Drawing.Color]::Yellow
$UsbDevU.Checked = $False
$UsbDevU.ForeColor = [System.Drawing.Color]::LightGray
$DelRegK.Checked = $False
$DelRegK.ForeColor = [System.Drawing.Color]::LightGray
$SaveSet.Enabled = $True
}
})
$UsbDevU.Location = New-Object System.Drawing.Point(30,55)
$UsbDevU.Size = New-Object System.Drawing.Size(182,22)
$UsbDevU.Font = New-Object System.Drawing.Font("Verdena",9,[System.Drawing.FontStyle]::Regular)
$UsbDevU.ForeColor = [System.Drawing.Color]::LightGray
$UsbDevU.Cursor = [System.Windows.Forms.Cursors]::Hand
$UsbDevU.Margin = "4,0,4,0"
$UsbDevU.TextAlign = "MiddleLeft"
$UsbDevU.Text = "USB-Datenträger freigeben"
$UsbDevU.Add_CheckedChanged(
{
If ($UsbDevU.Checked -Eq $True)
{
$UsbDevU.Checked
$UsbDevU.ForeColor = [System.Drawing.Color]::Yellow
$UsbDevB.Checked = $False
$UsbDevB.ForeColor = [System.Drawing.Color]::LightGray
$DelRegK.Checked = $False
$DelRegK.ForeColor = [System.Drawing.Color]::LightGray
$SaveSet.Enabled = $True
}
})
$DelRegK.Location = New-Object System.Drawing.Point(30,80)
$DelRegK.Size = New-Object System.Drawing.Size(182,22)
$DelRegK.Font = New-Object System.Drawing.Font("Verdena",9,[System.Drawing.FontStyle]::Regular)
$DelRegK.ForeColor = [System.Drawing.Color]::LightGray
$DelRegK.Cursor = [System.Windows.Forms.Cursors]::Hand
$DelRegK.Margin = "4,0,4,0"
$DelRegK.TextAlign = "MiddleLeft"
$DelRegK.Text = "REG-Schlüssel löschen"
$DelRegK.Add_CheckedChanged(
{
If ($DelRegK.Checked -Eq $True)
{
$DelRegK.Checked
$DelRegK.ForeColor = [System.Drawing.Color]::Yellow
$UsbDevB.Checked = $False
$UsbDevB.ForeColor = [System.Drawing.Color]::LightGray
$UsbDevU.Checked = $False
$UsbDevU.ForeColor = [System.Drawing.Color]::LightGray
$SaveSet.Enabled = $True
}
})
$SaveSet.Location = New-Object System.Drawing.Point(15,122)
$SaveSet.Size = New-Object System.Drawing.Size(95,25)
$SaveSet.Font = New-Object System.Drawing.Font("Verdena",10,[System.Drawing.FontStyle]::Regular)
$SaveSet.BackColor = [System.Drawing.Color]::FromArgb(255,30,30,30)
$SaveSet.ForeColor = [System.Drawing.Color]::Lime
$SaveSet.Cursor = [System.Windows.Forms.Cursors]::Hand
$SaveSet.Add_MouseHover({$SaveSet.BackColor = [System.Drawing.Color]::Lime;$SaveSet.ForeColor = [System.Drawing.Color]::FromArgb(255,30,30,30)})
$SaveSet.Add_MouseLeave({$SaveSet.BackColor = [System.Drawing.Color]::FromArgb(255,30,30,30);$SaveSet.ForeColor = [System.Drawing.Color]::Lime})
$SaveSet.Margin = "4,0,4,0"
$SaveSet.TextAlign = "MiddleCenter"
$SaveSet.Text = "Übernehmen"
$SaveSet.Enabled = $False
$SaveSet.Add_Click(
{
If (-Not(Test-Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices"))
{
New-Item -Force "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices"
New-ItemProperty -Force "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices" "Deny_all"
If ($UsbDevB.Checked)
{
Set-ItemProperty -Force "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices" "Deny_all" -Value 1 -Type DWord
}
ElseIf ($UsbDevU.Checked)
{
Set-ItemProperty -Force "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices" "Deny_all" -Value 0 -Type DWord
}
ElseIf ($DelRegK.Checked)
{
Remove-Item -Recurse -Force "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices"
}
}
Else
{
If ($UsbDevB.Checked)
{
Set-ItemProperty -Force "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices" "Deny_all" -Value 1 -Type DWord
}
ElseIf ($UsbDevU.Checked)
{
Set-ItemProperty -Force "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices" "Deny_all" -Value 0 -Type DWord
}
ElseIf ($DelRegK.Checked)
{
Remove-Item -Recurse -Force "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices"
}
}
$WinForm.Dispose()
GenUsbDevMng
})
$AppExit.Location = New-Object System.Drawing.Point(130,122)
$AppExit.Size = New-Object System.Drawing.Size(85,25)
$AppExit.Font = New-Object System.Drawing.Font("Verdena",10,[System.Drawing.FontStyle]::Regular)
$AppExit.BackColor = [System.Drawing.Color]::FromArgb(255,30,30,30)
$AppExit.ForeColor = [System.Drawing.Color]::Lime
$AppExit.Cursor = [System.Windows.Forms.Cursors]::Hand
$AppExit.Add_MouseHover({$AppExit.BackColor = [System.Drawing.Color]::Lime;$AppExit.ForeColor = [System.Drawing.Color]::FromArgb(255,30,30,30)})
$AppExit.Add_MouseLeave({$AppExit.BackColor = [System.Drawing.Color]::FromArgb(255,30,30,30);$AppExit.ForeColor = [System.Drawing.Color]::Lime})
$AppExit.Margin = "4,0,4,0"
$AppExit.TextAlign = "MiddleCenter"
$AppExit.Text = "Abbrechen"
$AppExit.Add_Click({$WinForm.Close()})
New-PSDrive -PSProvider Registry -Root HKEY_LOCAL_MACHINE -Name HKLM
If (-Not(Test-Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices"))
{
$UsbDevB.Checked = $True
$UsbDevB.ForeColor = [System.Drawing.Color]::Yellow
$UsbDevU.Checked = $False
$UsbDevU.Enabled = $False
$DelRegK.Enabled = $False
$SaveSet.Enabled = $True
}
Else
{
$UsbRegKey = (Get-ItemProperty "HKLM:\SOFTWARE\Policies\Microsoft\Windows\RemovableStorageDevices").Deny_all
If ($UsbRegKey -Like '1')
{
$UsbDevB.Checked = $False
$UsbDevB.Enabled = $False
$UsbDevU.Checked = $True
$UsbDevU.ForeColor = [System.Drawing.Color]::Yellow
$SaveSet.Enabled = $True
}
ElseIf ($UsbRegKey -Like '0')
{
$UsbDevB.Checked = $True
$UsbDevB.ForeColor = [System.Drawing.Color]::Yellow
$UsbDevU.Checked = $False
$UsbDevU.Enabled = $False
$SaveSet.Enabled = $True
}
}
[Void]$WinForm.ShowDialog()
}
GenUsbDevMng