Script for running a batch file as Administrator without prompting for password - vbscript

The below script works on Windows XP as expected. The script abc.bat would run as Administrator without a prompt for password(password is supplied automatically). But the same script is not working on our Windows 7 Box. Are there any changes required? I have no experience with VB Scripting. Thanks!
Option Explicit
Const USER = "administrator"
Const PASS = "*********"
Const WSNAME_COMMANDLINE = "C:\support\abc.bat"
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim WSHShell : Set WshShell = CreateObject("WScript.Shell")
Dim WshNetwork : Set WshNetwork = CreateObject("WScript.Network")
Dim oDic : Set oDic = CreateObject("Scripting.Dictionary")
Dim objArgs : Set objArgs = WScript.Arguments
Dim oProcessEnv : Set oProcessEnv = WshShell.Environment("PROCESS")
Dim sPathToRunAs, iReturnCode
sPathToRunAs = oProcessEnv("SystemRoot")&"\System32\runas.exe"
''msgbox sPathtorunas
if Not fso.FileExists(sPathToRunAs) Then : WScript.Quit(1) 'Can't find RunAs
'''msgbox "runas /user:" & USER & " " & CHR(34) & WSNAME_COMMANDLINE & CHR(34)
iReturnCode=WshShell.Run("runas /user:" & USER & " " & CHR(34) & WSNAME_COMMANDLINE & CHR(34), 2, FALSE)
Wscript.Sleep 40 ' Time for window to open.
WshShell.AppActivate(sPathToRunAs)' Activate the Window
Wscript.Sleep 3
WSHShell.SendKeys PASS & "~" ' Send the password
Wscript.Sleep 3
''msgbox "done"

Did you try the script on Windows 7 with UAC (User Account Control) disabled?
You can find a howto on disabling UAC here: http://windows.microsoft.com/en-US/windows-vista/Turn-User-Account-Control-on-or-off
If UAC is the reason for your issue, you may use windows task scheduler to avoid this like it is explained here: http://poundcomment.wordpress.com/2011/03/18/how-to-create-a-whitelist-uac-for-windows-7/

Related

WScript doesn't work with HPALM workflow. But works fine when executed in command Line

I'm setting up a new HP ALM workflow script to invoke firefox and open two tabs in the same.
The script given works like a charm in command line. However, the same script doesn't work well flow HP-ALM Workflow. I'm certain the script is being invoked though, but the firefox browser is not opening. Placed alerts to debug the script and the alerts from the script are displayed.
Dim wshshell
Set wshshell = WScript.CreateObject("Wscript.Shell")
wshshell.run """firefox.exe"" www.google.com",1,False
wshshell.run """firefox.exe"" www.yahoo.com",2,False
Set wshshell = Nothing
wscript.quit
On a Button click in HP ALM client, firefox should be opened with two tabs. One with google & other with yahoo homepage. However, No page is opening.
Finally, the following solution helped;
ActionCanExecute()
Function ActionCanExecute()
On Error Resume Next
ActionCanExecute = DefaultRes
browserName = "Mozilla Firefox"
browserExeName = "Firefox.EXE"
cmdLineArgPlatform = " -new-window "
cmdLineArgIdc = " -new-tab "
sURLPlatform = "www.google.com"
sURLIdc = "www.yahoo.com"
Set WSHShell = CreateObject("WScript.Shell")
exePath = WSHShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\" & _
"CurrentVersion\App Paths\" & browserExeName & "\")
'Open the URL
sShellCmdPlatform = """" & exePath & """" & "" & cmdLineArgIdc & """" & sURLPlatform & """"
MsgBox sShellCmdPlatform
sShellCmdIdc = """" & exePath & """" & "" & cmdLineArgIdc & """" & sURLIdc & """"
MsgBox sShellCmdIdc
'MsgBox sFFExe
WSHShell.Run sShellCmdPlatform, vbHide
WSHShell.Run sShellCmdIdc
On Error Resume Next
Set WSHShell = Nothing
ActionCanExecute = DefaultRes
On Error GoTo 0
End Function

Writing a VBS via another VBS

I'm trying to create a VBS via another VBS, but cant handle the quotation marks, and would love to know if it's even possible.
That's the 1 line I need in my new VBS:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Main VBS code that doesn't work:
Dim oFSO, vbFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set vbFile = oFSO.CreateTextFile("try.vbs", True)
vbFile.WriteLine ""CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False""
vbFile.Close
Thanks to Sorceri, I managed to make it work by writing the following code:
Dim oFSO, vbFile
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set vbFile = oFSO.CreateTextFile("try.vbs", True)
vbFile.WriteLine "CreateObject(" & Chr(34) & "Wscript.Shell" & Chr(34) & ").Run """""""" & WScript.Arguments(0) & """""""", 0, False"
vbFile.Close
This is what you need to do.
Copy and paste this code
do
msgbox("haha you cant close this")
CreateObject ("WScript.Shell").Run(".\Duplicate.vbs")
loop

Listing printers on remote machines. Not seeing the same results as I would if I were logged on locally as the user

My script is supposed to list all the printers installed on a remote machine and write that data to a text file while designating if the printer is Local or Network. When I run the script against my local machine with my profile logged on I get the following results:
Local
Microsoft XPS Document Writer
Network
\\PrintServer\PT-NJ-CPR-B-CORPIT-1
Network
\\PrintServer\PT-NJ-CPR-B-ITTEMP-1
Network
\\PrintServer\CPR5A26D1A
These results are exactly what I want however when I run the same script against a remote machine I still get results but they seem to be for a more generic user
Local
Send To OneNote 2010
Local
Microsoft XPS Document Writer
Local
Fax
My question is how do I customize my script to truly impersonate the logged on user thus returning me the full results even from a remote machine?
Const ForAppending = 8
Const ForReading = 1
Dim WshNetwork, objPrinter, intDrive, intNetLetter, fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("C:\xVBS Scripts\Printer Scripts\Computers.txt", 1)
Do Until InputFile.AtEndOfStream
strComputer = InputFile.ReadLine
Set WshNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objItem in colItems
UserName = objItem.UserName
arrUserName = Split(UserName, "\", -1, 1)
varUserName = arrUserName(1)
Next
filOutput = varUserName & ".txt"
If objFSO.FileExists(filOutput) Then
objFSO.DeleteFile(filOutput)
End If
Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True)
For Each objPrinter in colInstalledPrinters
If objPrinter.Attributes And 64 Then
strPrinterType = "Local"
strTest = Left(objPrinter.Name, 2)
objOutputFile.WriteLine(strPrinterType)
objOutputFile.WriteLine(objPrinter.Name)
objOutputFile.WriteLine(vbNewLine)
Else
strPrinterType = "Network"
strTest = Left(objPrinter.Name, 2)
objOutputFile.WriteLine(strPrinterType)
objOutputFile.WriteLine(objPrinter.Name)
objOutputFile.WriteLine(vbNewLine)
End If
Next
Wscript.Sleep 1500
MsgBox "Printer mapping report is located" & vbNewLine & "in the following directory: " & filOutput , vbInformation, "Report Located At"
WshShell.Run "Notepad " & filOutput,1,False
Loop
InputFile.Close
Wscript.Quit
I dont think there is an actual answer to this. The more I learn about VB Script and Powershell it appears as if WMI is most useful when run interactively. It doesn't know how to process users who are not currently logged in. I bypass this problem by running the script as a GPO Link/Enforced that calls the script as a log on script. – JRN just now edit

How do I get the computer name of a system and output it to a file in VBScript

I am trying to get the computer name from the registry and write it to a file. At this point, my function call for obtaining the computer name from registry isn't working. Any advice would be appreciated.
Option Explicit
On Error Resume Next
Dim regComputerName, ComputerName
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
regComputerName = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\Computername"
ComputerName = obj.shell.RegRead(regComputerName)
oWrite.WriteLine(ComputerName,C:\text)
Reading registry values is error prone and may require elevated privileges in Windows 7. There's another way of getting the computer name, very similar to what you are doing right now:
Set objNetwork = WScript.CreateObject("WScript.Network")
ComputerName = objNetwork.ComputerName
MsgBox ComputerName
Also, the last line in your script: oWrite.WriteLine(ComputerName,C:\text) will not work for 2 reasons:
C:\text has to be in quotes, like this: "C:\text.txt"
In VB, only a function that results a value can be called with parenthesis. Call WriteLine like this instead: oWrite.WriteLine ComputerName, "C:\text.txt"
Finally, are you sure you are not referring to VBScript instead of VB in your question?
Your code is not working because of an error in this line:
ComputerName = obj.shell.RegRead(regComputerName)
Instead of obj.shell you should be referencing objShell. It should look like this:
Set objShell = WScript.CreateObject("WScript.Shell")
strRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\Computername"
strComputerName = objShell.RegRead(strRegKey)
WScript.Echo strComputerName
However, there are much more reliable ways of getting the computer name without having to deal with the registry.
From WSH (as suggested above)
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputerName = WshNetwork.ComputerName
WScript.Echo "Computer Name: " & strComputerName
From an environmental variable...
Set wshShell = WScript.CreateObject("WScript.Shell")
strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
WScript.Echo "Computer Name: " & strComputerName
From WMI...
strcomputer = "."
Set objWMISvc = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMISvc.ExecQuery("Select * from Win32_ComputerSystem",, 48)
For Each objItem in colItems
strComputerName = objItem.Name
WScript.Echo "Computer Name: " & strComputerName
Next
From ADSI...
Set objSysInfo = CreateObject("WinNTSystemInfo")
strComputerName = objSysInfo.ComputerName
WScript.Echo "Computer Name: " & strComputerName
From ADSI (only works for domain members)...
Set objSysInfo = CreateObject("ADSystemInfo")
strComputerName = objSysInfo.ComputerName
WScript.Echo "Computer Name: " & strComputerName
...and one last way for Windows XP users only...
Set objPC = CreateObject("Shell.LocalMachine")
strComputerName = objPC.MachineName
WScript.Echo "Computer Name: " & strComputerName

How to create a user account in Windows Vista using VBScript?

How to create a user account in Windows Vista using VBScript?
I'm using the following script. It's working fine on Windows XP, but gives me an error on Windows Vista:
strUserName = "username"
strPassword = "password"
strComputer = "."
set objSystem = GetObject("WinNT://" & strComputer)
set objUser = objSystem.Create("user", strUserName)
objUser.SetPassword strPassword
objUser.SetInfo
I am able to run this script on my Vista box just fine, and it creates the user.
I suspect you might be having a UAC issue. This article provides some options for elevating the permissions of your script.
Option 1 – the code relaunches itself with elevated permissions:
If WScript.Arguments.length = 0 Then
Set objShell = CreateObject("Shell.Application")
'Pass a bogus argument, say [ uac]
objShell.ShellExecute "wscript.exe", Chr(34) & _
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
'Add your code here
End If
Option 2 – a separate launcher script:
Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = FSO.GetParentFolderName (WScript.ScriptFullName)
If FSO.FileExists(strPath & "\MAIN.VBS") Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\MAIN.VBS" & Chr(34), "", "runas", 1
Else
MsgBox "Script file MAIN.VBS not found"
End If

Resources