Common script code in vbs


Add a domain user or user to the local group
Set objGroup = GetObject(“WinNT://./Administrators”)
Set objUser = GetObject(“WinNT://testnet/Engineers”)

Change local administrator password
Set objcnlar = GetObject(“WinNT://./administrator, user”)
objcnla.SetPassword ” [email protected] ”

eject   YES   or   NO   Dialog box, different options execute different code
intAnswer = Msgbox(“Do you want to delete these files?”, vbYesNo,
“Delete Files”)
If intAnswer = vbYes Then
Msgbox “You answered yes.”
Else Msgbox “You answered no.”
End If

Run the CMD command line command
set obshell=wscript.createobject(“”) (“ipconfig”),,true
If the command you want to run contains double quotes, use & Chr (34) & instead

Ignore code errors and continue
On Error Resume Next
It is placed at the beginning of the code. When the code runs in error, it does not stop jumping out, but continues to execute the next one. Appropriate application will be very effective.

Modify / read / delete / create registry
Set   wso  =  CreateObject(“WScript.Shell”)  ‘ statement
wso.RegWrite  “% Path% “‘create subkey
wso.RegWrite  “% Path%, “% value%”‘modify “default” key value
wso.RegWrite  “% Path%”,%Value%,%RegType%  ‘ Modify a specific type of key value
‘(string value)   REG_ SZ   Extensible string value   REG_ EXPAND_ SZ   DWORD value   REG_ DWORD   Binary value   REG_ BINARY)

Set WSHShell= Wscript.CreateObject(“Wscript.Shell”)
WSHShell.RegRead  (% Path%)  ‘ Read registry subkeys or key values (generally used to determine whether an event is executed)

Set wso = CreateObject(“WScript.Shell”)
wso.RegDelete  “% Path%”  ‘ Delete subkey or key value
(root key abbreviation HKEY)_ CLASSES_ ROOT   HKCR   HKEY_ CURRENT_ USER   HKCU   HKEY_ LOCAL_ MACHINE   HKLM, others none)

Program code

Set wso = CreateObject(“Wscript.Shell”)
wso.RegWrite “HKLMSOFTWAREMicrosftWindows NT#1”
wso.RegWrite “HKLMSOFTWAREMicrosftWindows NT#1″,”0”
wso.RegWrite “HKLMSOFTWAREMicrosftWindows NT#1#2”,0,REG_BINARY
wso.RegDelete “HKLMSOFTWAREMicrosftWindows NT#1”

File copy / delete / create / write simply
Set   fso  =  Wscript.CreateObject(“Scripting.FileSystemObject”)  ‘ statement
Set   f  =  fso.CreateTextFile(“%PATH%”)  ‘ Create a file, where f can be arbitrary, including the abbreviation
f.WriteLine(“VBS”)  ‘ Write file content, this command function is too simple, at present it seems that it can only be used for TXT file
set   c=fso.getfile(“%path%”)  ‘ Copy a file
c.copy(“%PATH2%”)  ‘ Copy files to designated location
fso.deletefile(“%PATH%”)  ‘ Delete file

Program code

Set fso = Wscript.CreateObject(“Scripting.FileSystemObject”)
Set f=fso.CreateTextFile(“C:Sample.txt”)
set e=fso.getfile(C:Sample.txt)

Output a simple string of instructions to an application
dim   program1  ‘ Declare the variable program1
program1=  “% Path%”  ‘ Application path
set   wshshell=createobject(“”)  ‘ Declaration function
set   oexec=wshshell.exec(program1)  ‘ Run the program
wscript.sleep   two thousand  ‘( The function of this command is unknown. It is estimated that the delay is set. Please give some advice.)
wshshell.appactivate  “% WindowsName%”  ‘ Activate application window
wshshell.sendkeys  “+{% KeyBoardName%}”  ‘ Before the first output of keyboard key command, add+
wshshell.sendkeys  ” 555555″  ‘ In order to input and use the series of commands in the program input field, we must first make sure that the program can implement a series of keyboard operations, which is most applicable in QQ login, as shown in the following example.

Program code

dim program1
program1=”D:Program FilesTencentcoralQQ.exe”
set wshshell=CreateObject(“”)
set oexec=wshshell.exec(program1)
wscript.sleep 2000
wshshell.appactivate  ” QQ login“
wshshell.sendkeys “+{TAB}”
wshshell.sendkeys “250481892”
wscript.sleep 2000
wshshell.sendkeys “{TAB}”
wshshell.sendkeys “****************”
wscript.sleep 2000
wshshell.sendkeys “{ENTER}”

Simple operation of folder
Set   fso  =  Wscript.CreateObject(“Scripting.FileSystemObject”)  ‘ statement
Set   f  =  fso.CreateFolder(“%PATH%”)   create folder
Set   e  =  getFolder(%PATH%)   Similar to bind target
e.copy(“%PATH2%”)   Copy folder
fso.deletefolder(%PATH%)   remove folders

Program code

Set fso = Wscript.CreateObject(“Scripting.FileSystemObject”)
Set f = fso.CreateObject(“C:sample”)

(it can be seen from the above example that many folder operations are interlinked with file operations, so VBS files have a lot of command uniformity.)

Converts all read-only files in a specified folder into readable files
Const   ReadOnly  =  one  ‘ Set the corresponding value of read-only property to 1

Set   FSO  =  CreateObject(“Scripting.FileSystemObject”)  ‘ statement
Set   Folder  =  FSO.GetFolder(“%PATH%”)  ‘ Binding folder
Set   colFiles  =  Folder.Files  ‘ Folder all files

For   Each   objFile   in   colFiles  ‘ The following statement applies to all files in the folder
If   File.Attributes   AND   ReadOnly   Then  ‘ This is the key point. If judgment statement is applied here to detect whether the file attribute is read-only
File.Attributes = File.Attributes XOR ReadOnly
‘XOR logic operation is performed on the judgment result of ture (default is true)’ to make it readable
End   If  ‘ End judgment

Save word file as text file
Const   wdFormatText  =  two  ‘ Set constant value
(when the value is 8, it is saved as HTML document, when it is 11, it is saved as XML document)
Set   objWord  =  CreateObject(“Word.Application”)  ‘ Declare calling function
Set   objDoc  =  objWord.Documents.Open(“%Path%”)  ‘ Open a doc file
objDoc.SaveAs  “% PATH2%”,   wdFormatText   Save as

Program code

Const wdFormatText = 2
Set objWord = CreateObject(“Word.Application”)
Set objDoc = objWord.Documents.Open(“d:doc1.doc”)
objDoc.SaveAs “g:doc1.txt”, wdFormatText

Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]