Jump to content

VBS program to auto IT


Recommended Posts

Tried to convert following vbs script to auto it, but can't have any luck.

Can anybody see, and tell me what I'm doing wrong?

Original code

Option Explicit

'Declare variables

Dim WSHShell, rr, rr2, MyBox, val, val2, ttl, toggle

Dim jobfunc, itemtype

On Error Resume Next

Set WSHShell = WScript.CreateObject("WScript.Shell")

val = "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System\DisableRegistryTools"

val2 = "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\DisableRegistryTools"

itemtype = "REG_DWORD"

jobfunc = "Registry Editing Tools are now "

ttl = "Result"

'reads the registry key value.

rr = WSHShell.RegRead (val)

rr2 = WSHShell.RegRead (val2)

toggle=1

If (rr=1 or rr2=1) Then toggle=0

If toggle = 1 Then

WSHShell.RegWrite val, 1, itemtype

WSHShell.RegWrite val2, 1, itemtype

Mybox = MsgBox(jobfunc & "disabled.", 4096, ttl)

Else

WSHShell.RegDelete val

WSHShell.RegDelete val2

Mybox = MsgBox(jobfunc & "enabled.", 4096, ttl)

End If

Code I created in Auto it

;variables

dim $wshshell,$rr, $rr2, $MyBox, $val, $val2, $ttl, $toggle, $jobfunc, $itemtype

$WshShell = ObjCreate("Wscript.Shell")

$val="HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System\DisableRegistryTools"

$val2="HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\DisableRegistryTools"

$itemtype="REG_DWORD"

$ttl="Result"

;On Error Resume Next

;reads the registry key value.

$rr = $WSHShell.RegRead ($val)

$rr2 = $WSHShell.RegRead ($val2)

$toggle=1

If ($rr=1 or $rr2=1) Then $toggle=0

If $toggle = 1 Then

$WSHShell.RegWrite $val, 1, $itemtype

$WSHShell.RegWrite $val2, 1, $itemtype

$Mybox = MsgBox(0,$jobfunc & "disabled."& $ttl)

Else

$WSHShell.RegDelete $val

$WSHShell.RegDelete $val2

$Mybox = MsgBox(0,$jobfunc & "enabled."& $ttl)

EndIf

EndIf

"You cannot solve a problem with the mind that created it" (Albert Einstein)

Link to comment
Share on other sites

Don't use Dim to declare the variables. Dim is to declare arrays with custom dimensions. Just declare them as Global.

MsgBox takes three parameters. MsgBox (Flag, Title, Text)

When you call functions, use the parentesis: $WSHShell.RegWrite ($val, 1, $itemtype)

And there's an extra EndIf down there.

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Link to comment
Share on other sites

Tried to convert following vbs script to auto it, but can't have any luck.

Can anybody see, and tell me what I'm doing wrong?

Original code

Code I created in Auto it

In AutoIt, you need to place parentheses around COM method parameters, like this:

$WSHShell.RegWrite ($val, 1, $itemtype)

-S

Edited by Locodarwin
(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

So I've eddited this a lit

but still no works.

this is what I have now

;variables
Global $wshshell,$rr, $rr2, $MyBox, $val, $val2, $ttl, $toggle, $jobfunc, $itemtype

$WshShell = ObjCreate("Wscript.Shell")
$val="HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System\DisableRegistryTools"
$val2="HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\DisableRegistryTools"
$itemtype="REG_DWORD"
$jobfunc = "Registry Editing Tools are now "
$ttl="Result"

;On Error Resume Next

;reads the registry key value.
$rr = $WSHShell.RegRead($val)
$rr2 = $WSHShell.RegRead($val2)

$toggle=1

If ($rr=1 or $rr2=1) Then $toggle=0


If $toggle = 1 Then
$WSHShell.RegWrite($val, 1, $itemtype)
$WSHShell.RegWrite($val2, 1, $itemtype)
$Mybox = MsgBox(0,"Status",$jobfunc & "disabled."& $ttl)
Else
$WSHShell.RegDelete($val)
$WSHShell.RegDelete($val2)
$Mybox = MsgBox(0,"Status",$jobfunc & "enabled."& $ttl)
EndIf

"You cannot solve a problem with the mind that created it" (Albert Einstein)

Link to comment
Share on other sites

In autoit you don't need to use ("Wscript.Shell") to read/write/delete into the registry.

See help file for examples :

$var = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion", "ProgramFilesDir")

D2charkeeper = No more 'expired characters' in D2.File Date Changer = Change the file date(s), attributes and the filename case of multiple files @ once.Updater_full = Copy/Update your autoitscripts, pictures, .mp3, .avi etc ... subdirs from your PC to your memory stick or to your external harddisk. Now with scheduling and logging.Questmapper
Link to comment
Share on other sites

I know you can just use regread and regwrite, but what if the register is locked by the system admin?

In our company the use of regedit is locked for our users.

So, I did a search on the internet, and via the vbs script I can unlock and lock the register.

I'm writing a script for our users, and for that I need modifie a few register settings. (for the places bar => see other post of mine)

If you have another idea for unlocking register, please share it with me.

"You cannot solve a problem with the mind that created it" (Albert Einstein)

Link to comment
Share on other sites

Ok Steven, sorry

Maybe if you know the admin password, you can use the second logon function

and temporarly logon as admin in your script before you start the registry stuff:

(runs only if compiled - because of the input parameter)

AutoItSetOption ( "RunErrorsFatal", 0) 
; variable definition
$username="ADMIN"                  ; local admin account name
$Password="PASSWORD"              ; local admin account psw
$domain="DOMAIN"
$run = 0                          ; run indicator 1=first cycle uninstall   2=install new
; retrieve commandline parameter
If $CmdLine[0] = 1 then $run = $CmdLine[1]
; 
If $run = 0 then
  if isadmin() then
     $run = 1
  else
   ; *** Changing credentials..
   RunAsSet ($Username, $domain, $Password)
    ; start the script program (itself) again but now in Adminmode...so all done tasks will run in Adminmode
     Run('"' & @ScriptFullPath & '" " 1"' ) 
     ; if fails then exits because username & password is not correct
     if @error = 1 then
        msgbox(48,"Error","cannot start the installation because you are not in Admin mode ")    
        exit
     endif
     exit
  endif
EndIf
;do your stuff here
;RunWait("regedit.exe")
RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE", "TestKey", "REG_SZ", "Hello this is a test")
D2charkeeper = No more 'expired characters' in D2.File Date Changer = Change the file date(s), attributes and the filename case of multiple files @ once.Updater_full = Copy/Update your autoitscripts, pictures, .mp3, .avi etc ... subdirs from your PC to your memory stick or to your external harddisk. Now with scheduling and logging.Questmapper
Link to comment
Share on other sites

@steven,

There are 2 alternative ways of manipulating the Registry.

One is using the WMI object to read an write to the REG.

VBscript Example :

Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\System Admin Scripting Guide"
strValueName = "String Value Name"
strValue = "string value"
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
 
strValueName = "DWORD Value Name"
dwValue = 82
oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue

This way you can also provide access authority if needed.

The other way is using the

ObjCreate("VBScript.RegExp")

But not sure if the last option is able to write the REG.

If you need more info let me know in a PM.

Regards / Tot ziens,

ptrex

Link to comment
Share on other sites

@steven,

This is the VBcriot examole translated into AutotIT :

VBscript
Const HKEY_LOCAL_MACHINE = &H80000002

strComputer = "."
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SYSTEM\CurrentControlSet\Services"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
 
For Each subkey In arrSubKeys
    Wscript.Echo subkey
Next

;AutoIT script
Dim $arrSubKeys
Const $HKEY_LOCAL_MACHINE = 0x80000002

$strComputer = "."
 
 $oReg=ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    $strComputer & "\root\default:StdRegProv")
 
$strKeyPath = "SYSTEM\CurrentControlSet\Services"
$oReg.EnumKey ($HKEY_LOCAL_MACHINE, $strKeyPath, $arrSubKeys)
 
For $subkey In $arrSubKeys
    ConsoleWrite ($subkey&@CR)
Next

I hope this can help you get started.

Otherwise send me a PM.

regards,

ptrex

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...