![]() |
Tipp 0364
|
Windows herunterfahren und neu starten (WMI)
|
 |
|
Autor/Einsender: Datum: |
|
Jürgen Beil 07.10.2003 |
|
Entwicklungsumgebung: |
|
VB 6 |
|
|
Mit Hilfe der Windows Management Instrumentation (WMI) lässt sich auf
einfachste Weise Windows herunterfahren und neu starten, besonders unter Windows 2000 und Windows XP!
|
|
|
Option Explicit
Private Sub Form_Load()
Dim objWMI As Object
On Error Resume Next
Set objWMI = GetObject("WinMgmts:")
If Err.Number <> 0 Then
Label1.Visible = True
Err.Clear
End If
End Sub
Private Sub Command1_Click()
'Einfache Variante
On Error Resume Next
Dim objWMI As Object
Dim colItems As Object
Dim objItem As Object
'WMI-Objekt erstellen und Abfragen ausführen
Set objWMI = GetObject( _
"WinMgmts:{impersonationLevel=impersonate, " & _
"(Shutdown)}!/root/cimv2")
Set colItems = objWMI.ExecQuery( _
"SELECT * FROM Win32_OperatingSystem")
If Option1(0).Value = True Then
'Windows neu starten
For Each objItem In colItems
objItem.Reboot
Next objItem
Else
'Windows herunterfahren
For Each objItem In colItems
objItem.Shutdown
Next objItem
End If
End Sub
Private Sub Command2_Click()
'Komplexe Variante
On Error Resume Next
Dim objWMI As Object
Dim colItems As Object
Dim objItem As Object
Dim bytShutdownFlag As Byte
'WMI-Objekt erstellen und Abfragen ausführen
Set objWMI = GetObject( _
"WinMgmts:{impersonationLevel=impersonate, " & _
"(Shutdown)}!/root/cimv2")
Set colItems = objWMI.ExecQuery( _
"SELECT * FROM Win32_OperatingSystem")
If Option2(0).Value = True Then
'Windows neu starten
bytShutdownFlag = 0
End If
If Option2(1).Value = True Then
'Windows herunterfahren
bytShutdownFlag = 1
End If
If Option2(2).Value = True Then
'Windows herunterfahren - PC ausschalten
bytShutdownFlag = 8
End If
If Option2(0).Value = True Then
'Aktuellen Benutzer abmelden
bytShutdownFlag = 2
End If
'Funktion erzwingen
If Check1.Value = 1 Then
bytShutdownFlag = bytShutdownFlag + 4
End If
'Auswahl ausführen
For Each objItem In colItems
objItem.Win32Shutdown (bytShutdownFlag)
Next objItem
End Sub
|
|
|
|
|
|
Um diesen Tipp ausführen zu können, muss WMI installiert sein. Dies ist bei Windows 2000,
Windows XP und Windows ME (Millenium Edition) standardmäßig der Fall. Für Windows NT 4.0 kann
hier
ein entsprechendes Installationspaket heruntergeladen werden, das eine vergleichbare Funktionalität bietet.
|
|
Windows-Version |
95 |
 |
|
98/SE |
 |
|
ME |
 |
|
NT |
 |
|
2000 |
 |
|
XP |
 |
|
Vista |
 |
|
Win
7 |
 |
|
|
VB-Version |
VBA 5 |
 |
|
VBA 6 |
 |
|
VB 4/16 |
 |
|
VB 4/32 |
 |
|
VB 5 |
 |
|
VB 6 |
 |
|
|
|
Download (3,4 kB)
|
Downloads bisher: [ 2366 ]
|
|
|