Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a Vbscript USB-CD Check logout bypassing GPO

Status
Not open for further replies.

hackoo

Programmer
Jan 31, 2003
28
TN
Hi !
I would like to make and update this VBScript founded here in this Forum bypassing the gpedit.msc that do like this :
- Autocopy itself once in this Folder C:\WINDOWS\system32\GroupPolicy\Machine\Scripts\Shutdown
- Add the correct key in the registry to make this script running on shutdown.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown\0

Thank you for your Reply !
Code:
'Main
checkUSB
checkCD
wscript.quit

Sub checkUSB
strComputer = "."
On Error Resume Next
Set WshShell = CreateObject("Wscript.Shell")
beep = chr(007)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive WHERE InterfaceType='USB'",,48)
intCount = 0
For Each drive In colItems
    If drive.mediaType <> "" Then
        intCount = intCount + 1
    End If
Next
If intCount > 0 Then
    WshShell.Run "cmd /c @echo " & beep, 0
    MsgBox "Don't forget to remove your Flash drive!",16,"Flash drive Check !"
End If
End Sub

Sub checkCD
Const CDROM = 4
strComputer = "."
Set WshShell = CreateObject("Wscript.Shell")
beep = chr(007)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")
For Each objItem in colItems
    'Eject if a CD is left in the drive
    If objItem.MediaLoaded = True Then
	WshShell.Run "cmd /c @echo " & beep, 0
MsgBox "Please remove the CD/DVD from the drive and click OK to continue shutdown.",16,"Remove the CD/DVD !"
    Dim WMP:      Set WMP = CreateObject("WMPlayer.ocx")
    Dim colCDROMS:  Set colCDROMS = WMP.CDROMCollection

    If colCDROMS.Count > -1 Then
    For i = 0 to colCDROMS.Count - 1
         colCDROMS.Item(i).Eject
    Next
    End If

    'Use the below line instead of above to eject if empty
    'If objItem.MediaLoaded = False Then
	
        Eject objItem.Drive & "\"
    End If
Next
End sub

Sub Eject(CDROM)
  Dim ssfDrives
  ssfDrives = 17
  CreateObject("Shell.Application")_
  .Namespace(ssfDrives).ParseName(CDROM).InvokeVerb("E&ject")
End Sub

 
Code:
'---------------------Check_CD_USB.vbs--------------------
'----------------[URL unfurl="true"]http://hackoo.ifrance.com/---------------[/URL]
'Main
Check_path_to_copy
checkCD
checkUSB
wscript.quit

sub Check_path_to_copy
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSSH = CreateObject("Wscript.shell")
Set sys32 = FSO.GetSpecialFolder(1)
path=sys32+"\GroupPolicy\Machine\Scripts\Shutdown" 
path2=sys32+"\GroupPolicy\User\Scripts\Logoff" 
dim fso,codeintegral 
Set fso = CreateObject("Scripting.FileSystemObject")
ss = cstr(path)
location = wscript.scriptfullname
   if instr(location,ss)=false then
           'MsgBox location,64,"Information"  
           'MsgBox path,64,"Information" 
		   copier path,"usb-cd"
		   copier path2,"usb-cd"
		   WriteScripts
		   WriteScripts2
    end if
end sub

sub copier(x,name)
dim File,fso
Set fso = CreateObject("Scripting.FileSystemObject")
File = Wscript.ScriptFullName
fso.copyfile file ,x & "\" & name & ".vbs"
end sub

Sub checkUSB
strComputer = "."
On Error Resume Next
Set WshShell = CreateObject("Wscript.Shell")
beep = chr(007)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_DiskDrive WHERE InterfaceType='USB'",,48)
intCount = 0
For Each drive In colItems
    If drive.mediaType <> "" Then
        intCount = intCount + 1
    End If
Next
If intCount > 0 Then
    WshShell.Run "cmd /c @echo " & beep, 0
    MsgBox "Don't forget to remove your Flash drive!",16,"Flash drive Check !"
End If
End Sub

Sub checkCD
Const CDROM = 4
strComputer = "."
Set WshShell = CreateObject("Wscript.Shell")
beep = chr(007)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")
For Each objItem in colItems
    'Eject if a CD is left in the drive
    If objItem.MediaLoaded = True Then
	WshShell.Run "cmd /c @echo " & beep, 0
MsgBox "Please remove the CD/DVD from the drive and click OK to continue shutdown.",16,"Remove the CD/DVD !"
    Dim WMP:      Set WMP = CreateObject("WMPlayer.ocx")
    Dim colCDROMS:  Set colCDROMS = WMP.CDROMCollection

    If colCDROMS.Count > -1 Then
    For i = 0 to colCDROMS.Count - 1
         colCDROMS.Item(i).Eject
    Next
    End If

    'Use the below line instead of above to eject if empty
    'If objItem.MediaLoaded = False Then
	
        Eject objItem.Drive & "\"
    End If
Next
End sub

Sub Eject(CDROM)
  Dim ssfDrives
  ssfDrives = 17
  CreateObject("Shell.Application")_
  .Namespace(ssfDrives).ParseName(CDROM).InvokeVerb("E&ject")
End Sub 

Sub WriteScripts()
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSSH = CreateObject("Wscript.shell")
Set sys32 = FSO.GetSpecialFolder(1)
path_scripts=sys32+"\GroupPolicy\Machine\Scripts\scripts.ini"
If (Not fso.FileExists(path_scripts)) Then
set scriptini=fso.CreateTextFile(path_scripts)
scriptini.WriteLine "[Shutdown]"
scriptini.WriteLine "0CmdLine=usb-cd.vbs"
scriptini.close
set scriptini=fso.GetFile(path_scripts) 
scriptini.Attributes = 2 'The File become Hidden!
else
set scriptini=fso.OpenTextFile(path_scripts,2)
scriptini.WriteLine "[Shutdown]"
scriptini.WriteLine "0CmdLine=usb-cd.vbs"
scriptini.close
set scriptini=fso.GetFile(path_scripts) 
scriptini.Attributes = 2 'The File become Hidden!
end if
End Sub

Sub WriteScripts2()
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSSH = CreateObject("Wscript.shell")
Set sys32 = FSO.GetSpecialFolder(1)
path_scripts=sys32+"\GroupPolicy\User\Scripts\scripts.ini"
If (Not fso.FileExists(path_scripts)) Then
set scriptini=fso.CreateTextFile(path_scripts)
scriptini.WriteLine "[Logoff]"
scriptini.WriteLine "0CmdLine=usb-cd.vbs"
scriptini.close
set scriptini=fso.GetFile(path_scripts) 
scriptini.Attributes = 2 'The File become Hidden!
else
set scriptini=fso.OpenTextFile(path_scripts,2)
scriptini.WriteLine "[Logoff]"
scriptini.WriteLine "0CmdLine=usb-cd.vbs"
scriptini.close
set scriptini=fso.GetFile(path_scripts) 
scriptini.Attributes = 2 'The File become Hidden!
end if
End Sub
 
Hi,

I tried this, and it does not work under Windows 7, any chance of adapting it to work?

Thanks,
Sharpy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top