Showing posts with label vbscript. Show all posts
Showing posts with label vbscript. Show all posts

Wednesday, June 3, 2009

Adding a printer with vbscript from a network share

rem Add the connection
cscript prnmngr.vbs -ac -p \\server\printer_share


rem Set it as a default
cscript prnmngr.vbs -t -p "\\server\printer name"

rem If you don't know the name
cscript prnmngr.vbs -l

Monday, June 1, 2009

Change a computer name with vbscript

Scripting the changing of computer names in windows is straight-forward with a vbscript.

You can change a computer via the registry, so this is the approach used here.

Set the computer name you want in the sNewName variable.



sNewName = "new_pc_name"

Set oShell = CreateObject ("WSCript.shell")



sCCS = "HKLM\SYSTEM\CurrentControlSet\"

sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\"

sCompNameRegPath = sCCS & "Control\ComputerName\"



With oShell

.RegDelete sTcpipParamsRegPath & "Hostname"

.RegDelete sTcpipParamsRegPath & "NV Hostname"



.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName

.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName

.RegWrite sTcpipParamsRegPath & "Hostname", sNewName

.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName

End With ' oShell

VBScript tricks : no logo, filesystem, control blocks, argument


'When running from the console, here is how to hide the logo

cscript //Nologo myscript.vbs

'How to reference an argument
WScript.Arguments.Item(0)


'Here is how to read a file

set f = fso.opentextfile(source, 1,false)



if fso.fileexists(source) then



while not f.atendofstream



inputline = f.readline



msgbox inputline

wend

end if

Monday, April 20, 2009

Universal scripted uninstall (XP)

Given the displayname value, this vbscript will go and find it in the registry and uninstall the corresponding program.

This is where it searches:

hklm\software\microsoft\windows\currentversion\uninstall



' arg 1 - displayname


if (wscript.arguments.count > 0 ) then

set shell = wscript.createobject("wscript.shell")
strcmd = "reg query hklm\software\microsoft\windows\currentversion\uninstall /s"

set oexec = shell.exec(strcmd)

temp = 0
parent = ""

do while not oexec.stdout.atendofstream

strline = oexec.stdout.readline


if len(strline) > 0 then
a=split(strline,vbtab)
if mid(trim(a(0)),1,4) = "HKEY" then
b = split(trim(a(0)),"\")
parent = b(ubound(b))
temp = 1
end if
alen = ubound(a) + 1
if alen >= 3 then
if trim(a(0)) = "DisplayName" and trim(a(2)) = wscript.arguments(0) and temp = 1 then
wscript.echo "msiexec /x " & parent & " /qn"
temp = 0
shell.exec("msiexec /x " & parent & " /qn")
exit do
end if

end if
end if
loop




end if

Friday, August 1, 2008

Yes, you can script anything I - vbscript: sendkeys, stdout

What happens when you want to automate some configuration and you can't find the command line equivalent, the registry setting eludes you, and the apis to do the task seem too elaborate? Well just do it with it a few key strokes, but have vbscript do the operations for you. It's like doing a macro in excel or your favorite application.

Check out this link for the sendkeys method in wsh.

A lot times I am frustrated by trying to get Windows to act like Unix. When I want to do some string processing, it seems easier just to use vbscript and call the commandline from there. You can collect the information from stdout with the WScript.Shell object.

Check out this example.