VSCRIPT - how to add cmd to Registry RunOnce key...

The following scripts is erroring...can anyone help.

Line 2, ch 1 is a type mismatch error and after that, line 3, <"C:\windows> throws character error message....

having trouble..somebody please help.

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", "IssueDefrag", "REG_SZ"
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", "C:\windows\PREPREP\SCRIPTS\Defrag.cmd", "REG_DWORD"
ActiveInfoSysAsked:
Who is Participating?
 
gilmore27Commented:
I got cheated a little by changing a script in "Windows Scripting" by Microsoft Press (chap 17) but it works and even confirms the write.

Option Explicit
Dim strKeyPath       ' the portion of registry to read
Dim strComputer      ' the target computer
Dim objReg           ' holds connection to registry provider

Dim strData
Dim strNameValue


Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE


strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
strNameValue = "IssueDefrag"
strData = "C:\windows\PREPREP\SCRIPTS\Defrag.cmd"

strComputer = "."

Set objReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

objReg.SetStringValue HKLM, strKeyPath, strNameValue, strData

      WScript.Echo("Created key :" & strKeyPath & strNameValue)
0
 
ActiveInfoSysAuthor Commented:
' Get the Shell object
Dim oShell
Set oShell = CreateObject("WScript.Shell")
If Not oShell Is Nothing Then
      ' Write a string value to the registry
      oShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", "IssueDefrag", "REG_SZ"
      oShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", "%systemroot%\PREPREP\SCRIPTS\Defrag.cmd", "REG_DWORD"
      ' Free object again
      Set oShell = Nothing
End If

This fails as well... doesn't work on the second oshell.regwrite....invalid character..
0
 
ActiveInfoSysAuthor Commented:
Option Explicit
Dim objShell, strRoot, strModify, strDelete
strRoot = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
' Create the Shell object
Set objShell = CreateObject("WScript.Shell")
strModify = objShell.RegWrite(strRoot,"IssueDefrag", "REG_SZ")
strModify = objShell.RegWrite(strRoot,"%systemroot%\PREPREP\SCRIPTS\Defrag.cmd", "REG_DWORD")
WScript.Echo "Error No: " & err.number & " check " & strRoot
strModify = null
WScript.Quit

This doesn't work either..I am really struggling here.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Shift-3Commented:
Try this:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\IssueDefrag", "C:\windows\PREPREP\SCRIPTS\Defrag.cmd", "REG_SZ"
0
 
merowingerCommented:
define your Path not as  HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce\
not as HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

0
 
ActiveInfoSysAuthor Commented:
wshHandle.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\IssueDefrag", "C:\WINDOWS\preprep\scripts\defrag.cmd", "REG_SZ"

This doesn't work either...
0
 
ActiveInfoSysAuthor Commented:
wshHandle.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\IssueDefrag", """C:\WINDOWS\preprep\scripts\defrag.cmd"", "REG_SZ"


Does not like the "REG_SZ" above...
0
 
ActiveInfoSysAuthor Commented:
Set variables
      Option Explicit
' Declare Vaiables
Dim wshHandle, wshSysEnv

' Set Environment
      Set wshHandle = WScript.CreateObject("WScript.Shell")
    Set wshSysEnv = wshHandle.Environment("Process")

'Write to Registry
     wshHandle.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\IssueDefrag", "C:\WINDOWS\preprep\scripts\defrag.cmd", "REG_DWORD"


This gives typemismatch at the wshHandle command..
0
 
ActiveInfoSysAuthor Commented:
This is great.  Is there anyway to get the wshHandle.RegWrite stuff working...just wondering..it would be ideal to suite all other scripts commands in the file.
0
 
gilmore27Commented:
This also worked for me.  Sorry for the ugly code above, I'm still learning. Also, sometimes my fingers are faster than my brain.

Set WshShell = WScript.CreateObject("WScript.Shell")
WSHShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\IssueDefrag", "C:\windows\PREPREP\SCRIPTS\Defrag.cmd"


0
 
merowingerCommented:
I normally only use the wshHandle.RegWrite function without problems!
There's a little bug :)   but doesn't matter ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.