/build/static/layout/Breadcrumb_cap_w.png

Vbscript Function not running.

Hi guys

I need some help with the below code. We need to run a vbscript inside an HTA (have imported the HTA code only) which will detect a registry key (not a value) if present the script will invoke a shutdown and restart. If the key is not present it will write another registry value and exit.

 

Below is what i have so far. The problem seems to be that the logic inside the function is not working, the RegKeyMode value gets written, however the machine never reboots if the RebootRequired key is found. I'm a bit stuck any help would be appreciated.

 

Const HKEY_LOCAL_MACHINE = &H80000002

 

strComputer = "."

RegKeyMoe = "HKLM\Software\Build\MOEPreReq1"

Set objRegistry = GetObject("winmgmts:\\" & _ 

    strComputer & "\root\default:StdRegProv")

Set objShell = CreateObject("WScript.Shell")

'Set WshShell = WScript.CreateObject("Wscript.Shell")

 

'On Error Resume Next

If regValueExists("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") Then

 

objShell.Run "shutdown.exe /R /F /T 60 /C """ & "MOE Update Complete. Rebooting your computer in 60 seconds!"""  

Else

objShell.RegWrite RegKeyMoe, 1, "REG_DWORD"

'Self.close()

 

 

'End If

End If

End Sub

 

Function regValueExists(key)

      'This function checks if a registry value exists and returns True of False

      On Error Resume Next

      'im oShell

      'Set oShell = CreateObject ("WScript.Shell")

      regKeyExists = True

   Err.Clear

      objShell.RegRead(key)

     If Err <> 0 Then regValueExists = False

      Err.Clear

      Set objShell = Nothing

End Function


1 Comment   [ + ] Show comment
  • I think it may too many quotes in the shutdown line.
    objShell.Run "shutdown.exe /R /F /T 60 /C "MOE Update Complete. Rebooting your computer in 60 seconds!"" - SMal.tmcc 11 years ago

Answers (1)

Answer Summary:
Option Explicit On Error Resume Next Dim Value, objShell, RegKeyMoe Set objShell = CreateObject("WScript.Shell") Value=RegKeyExists("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") RegKeyMoe = "HKLM\Software\Build\MOEPreReq1" If Value=0 Then objShell.Run "ShutDown.exe /R /F /T 60 /C """ & "MOE Update Complete. Rebooting your computer in 60 seconds!""" Else objShell.RegWrite RegKeyMoe, 1, "REG_DWORD" End If Set objShell = Nothing Function RegKeyExists(Key) Dim oShell, entry On Error Resume Next Set oShell = CreateObject("WScript.Shell") entry = oShell.RegRead(Key) If Err.Number = 0 Then Err.Clear RegKeyExists = False Else Err.Clear RegKeyExists = True End If Set oShell = Nothing End Function
Posted by: jagadeish 11 years ago
Red Belt
1

Option Explicit
On Error Resume Next
 
Dim Value, objShell, RegKeyMoe
 
Set objShell = CreateObject("WScript.Shell")
Value=RegKeyExists("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired")
RegKeyMoe = "HKLM\Software\Build\MOEPreReq1"
 
If Value=0 Then
objShell.Run "ShutDown.exe /R /F /T 60 /C """ & "MOE Update Complete. Rebooting your computer in 60 seconds!""" 
Else
objShell.RegWrite RegKeyMoe, 1, "REG_DWORD"
End If
 
Set objShell = Nothing


Function RegKeyExists(Key)
 Dim oShell, entry
 On Error Resume Next
 
 Set oShell = CreateObject("WScript.Shell")
 entry = oShell.RegRead(Key)
 If Err.Number = 0 Then
 Err.Clear
 RegKeyExists = False
 Else
 Err.Clear
 RegKeyExists = True
 End If
 Set oShell = Nothing
End Function

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ