Common script code in vbs

Time:2021-5-31

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

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

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(“wscript.shell”)
obshell.run (“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”
Wscript.quit

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
f.Close
set   c=fso.getfile(“%path%”)  ‘ Copy a file
c.copy(“%PATH2%”)  ‘ Copy files to designated location
fso.deletefile(“%PATH%”)  ‘ Delete file
Wscript.quit

Program code

Set fso = Wscript.CreateObject(“Scripting.FileSystemObject”)
Set f=fso.CreateTextFile(“C:Sample.txt”)
WriteLine(“VBS”)
f.close
set e=fso.getfile(C:Sample.txt)
e.copy(“D:Sample.txt”)
fso.deletefile(C:Sample.txt)
Wscript.quit

Output a simple string of instructions to an application
dim   program1  ‘ Declare the variable program1
program1=  “% Path%”  ‘ Application path
set   wshshell=createobject(“wscript.shell”)  ‘ 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(“wscript.shell”)
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}”
Wscript.quit

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”)
f.copy(“D:sample”)
fso.deletefolder(“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
Next

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
objWord.Quit

Program code

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

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 […]