?
Solved

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

Posted on 2007-07-24
11
Medium Priority
?
2,324 Views
Last Modified: 2008-01-09
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"
0
Comment
Question by:ActiveInfoSys
  • 6
  • 2
  • 2
  • +1
11 Comments
 

Author Comment

by:ActiveInfoSys
ID: 19556400
' 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
 

Author Comment

by:ActiveInfoSys
ID: 19556564
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
 
LVL 38

Assisted Solution

by:Shift-3
Shift-3 earned 400 total points
ID: 19556789
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 31

Assisted Solution

by:merowinger
merowinger earned 400 total points
ID: 19556817
define your Path not as  HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce\
not as HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

0
 

Author Comment

by:ActiveInfoSys
ID: 19556912
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
 

Author Comment

by:ActiveInfoSys
ID: 19557146
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
 

Author Comment

by:ActiveInfoSys
ID: 19557248
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
 
LVL 1

Accepted Solution

by:
gilmore27 earned 1200 total points
ID: 19557864
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
 

Author Comment

by:ActiveInfoSys
ID: 19558316
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
 
LVL 1

Expert Comment

by:gilmore27
ID: 19559184
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
 
LVL 31

Expert Comment

by:merowinger
ID: 19566543
I normally only use the wshHandle.RegWrite function without problems!
There's a little bug :)   but doesn't matter ;)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question