Pausing vbs code until shell script finishes - shell

I'm trying to create a script that will auto install a printer.
I found some code that I modified to give the user some prompts. I think the original was a batch file. There are 4 processes that have to shell out to run a cscript command. These are the ones that I need to have paused until they are done. I thought the WaitOnReturn option in the shell command would have made them wait but it doesn't I marked them in the code with 'Need to wait here...
Here is the code.
Dim fso
Dim Folder
Dim ProgramPath
Dim WshShell
Dim ProgramArgs
Dim WaitOnReturn
Dim intWindowStyle
'Dim objShell
'strInput = UserInput( "Enter some input:" )
strInput = MsgBox("This will install the default HP Print driver?",1,"Windows 7 Print Driver Install")
'WScript.Echo "You entered: " & strIpAddress
If strInput = 2 Then
WScript.Echo "Please run again when you are ready"
Else '=1 Prompt for IP Address
'WScript.Echo "You entered: " & strInput
strIpCheck = MsgBox("Do you have the Printers IP Address?",1,"Choose options")
If strIpCheck = 2 Then 'Does not have IP Address
WScript.Echo "Please run again when have the IP Address"
Else 'Start install routine
strIpAddress = InputBox("Enter the IP Address", "IP Address")
Set WshShell = CreateObject("WScript.Shell")
'Create directories
Set FSO = CreateObject("Scripting.FileSystemObject")
If NOT (fso.FolderExists("C:\DRIVERS")) Then
fso.CreateFolder("C:\DRIVERS")
End If
If NOT (fso.FolderExists("C:\SCRIPTS")) Then
fso.CreateFolder("C:\SCRIPTS")
End If
'Location of Windows 7 HP print drivers
strSourceDriver = "C:\Windows\System32\DriverStore\FileRepository\hpoa1so.inf_amd64_neutral_4f1a3f1015001339"
'Location of Win7 built in printer scripts
strSourceScripts = "C:\Windows\System32\Printing_Admin_Scripts\en-US"
If (fso.FolderExists(strSourceDriver)) Then
fso.copyFolder strSourceDriver, "C:\DRIVERS"
End If
If (fso.FolderExists(strSourceScripts)) Then
fso.copyFolder strSourceScripts, "C:\SCRIPTS"
End If
'Delete existing printer named HP Printer
ProgramPath = "C:\SCRIPTS\prnmngr.vbs"
ProgramArgs = "-d -p " & Chr(34) & "HP Printer" & Chr(34) & ""
intWindowStyle = 1
WaitOnReturn = true
WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
ProgramPath = "C:\SCRIPTS\Prnport.vbs"
ProgramArgs = "-a -r " & strIpAddress & "Port -h " & strIpAddress & " -o raw -n 9100"
intWindowStyle = 1
WaitOnReturn = true
WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
ProgramPath = "C:\SCRIPTS\Prndrvr.vbs"
ProgramArgs = "-a -m " & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-i C:\DRIVERS\hpoa1so.inf -h C:\DRIVERS"
intWindowStyle = 1
WaitOnReturn = true
WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
ProgramPath = "C:\SCRIPTS\Prnmngr.vbs"
ProgramArgs = "-a -p " & Chr(34) & "HP Printer" & Chr(34) & "-m" & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-r " & strIpAddress
intWindowStyle = 1
WaitOnReturn = true
WshShell.Run "cscript.exe " & Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs, intWindowStyle, WaitOnReturn
'Need to wait here until the above shell process is done
End If
End If

All WshShell.Run "cscript.exe " & …, intWindowStyle, WaitOnReturn should wait until the called shell process is done providing WaitOnReturn = true. However, output from the following simplified script (where assignments of ProgramArgs are Copied&Pasted from the original code) shows some missing spaces in supplied parameters:
strIpAddress = "10.10.10.10"
ProgramArgs = "-d -p " & Chr(34) & "HP Printer" & Chr(34) & ""
Wscript.Echo ProgramArgs
ProgramArgs = "-a -r " & strIpAddress & "Port -h " & strIpAddress & " -o raw -n 9100"
Wscript.Echo ProgramArgs
ProgramArgs = "-a -m " & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-i C:\DRIVERS\hpoa1so.inf -h C:\DRIVERS"
Wscript.Echo ProgramArgs
ProgramArgs = "-a -p " & Chr(34) & "HP Printer" & Chr(34) & "-m" & Chr(34) & "HP Photosmart C8100" & Chr(34) & "-r " & strIpAddress
Wscript.Echo ProgramArgs
Output: cscript D:\bat\SO\55303301.vbs
-d -p "HP Printer"
-a -r 10.10.10.10Port -h 10.10.10.10 -o raw -n 9100
-a -m "HP Photosmart C8100"-i C:\DRIVERS\hpoa1so.inf -h C:\DRIVERS
-a -p "HP Printer"-m"HP Photosmart C8100"-r 10.10.10.10
Moreover, the Run method returns an integer. You could grab a process return code to a variable RetCode and then check if its value is zero (when all went OK) using
RetCode = WshShell.Run ( "cscript.exe " & _
Chr(34) & ProgramPath & Chr(34) & Space(1) & ProgramArgs _
, intWindowStyle, WaitOnReturn )

Related

Can't find where "Expected End of Statement is"

So I am creating a code in VBS to automate setting up computers. Step 1 of my code works well, Step 2 not implemented in this example but works good, Step 3 throws me a "Expected end of Statement" error at Command_3A.
The string I am trying to set is start "Lightspeed" /wait /i "Programs\Lightspeed\UserAgentx64 V2.1.14.msi"
I have tried these ways but I am missing something.
Command_3A = "start "Lightspeed" /wait /i "Programs\Lightspeed\UserAgentx64 V2.1.14.msi" "`
Command_3A = "start " ""Lightspeed"" " /wait /i " ""Programs\Lightspeed\UserAgentx64 V2.1.14.msi"" "`
Command_3A = (start "Lightspeed" /wait /i "C:\Users\ccollins\Desktop\ThumbDrive\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" )`
Here is my code:
'Dim objShell
Set objShell = WScript.CreateObject ("WScript.Shell")
Drive_Letter = "C:"
File_Path = "C:\Users\ccollins\Desktop\ThumbDrive\"
' Step 1 - Set Power Settings
Command_1A = "powercfg /change standby-timeout-ac 0"
Command_1B = "powercfg /change standby-timeout-dc 15"
Command_1C = "powercfg /change monitor-timeout-ac 0"
Command_1D = "powercfg /change monitor-timeout-dc 15"
Command_1E = "powercfg /change hibernate-timeout-ac 0"
Command_1F = "powercfg /change hibernate-timeout-dc 15"
objShell.Run "cmd /k " & Command_1A & "&" & Command_1B & "&" & Command_1C & "&" & Command_1D & "&" & Command_1E & "&" & Command_1F & "& exit"
' Step 2 - Remove Bloatware (Win10Apps)
' Step 3 - Install wanted programs
Command_3A = (start "Lightspeed" /wait /i "C:\Users\ccollins\Desktop\ThumbDrive\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" )
Command_3B = "start "Acrobat" /wait "Programs\AcroRdrDC1801120058_en_US.exe" /sAll "
Command_3C = "start "AZMerit" /wait /I "Programs\AzMERITSecureBrowser10.4-2018-08-02.msi" /passive "
Command_3D = "start "Java" /wait "Programs\jre-8u201-windows-x64.exe" /s "
Command_3E = "start "Chrome" /wait "Programs\ChromeStandaloneSetup64.exe" /silent /install "
Command_3F = "start "Eset" /wait "Programs\ESet Rip and Replace.exe" "
objShell.Run "cmd /k " & Drive_Letter & "&" & Command_3A & "&" & Command_3B & "&" & Command_3C & "&" & Command_3D & "&" & Command_3E & "&" & Command_3F & "& exit"
Set oShell = Nothing'
I am definitely missing something and just need another set of eyes to look at my code.
So this is what I came up with. I had to use Chr(34) for the " in the code that wanted inserted as part of the command. I also inserted an input box for sPath and UAC control.
Set wshShell = WScript.CreateObject("WScript.Shell")
dim sPath
' ----------------------------------------------------------'
' UAC Control
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", , "runas", 1
WScript.Quit
End If
'Step 0 - Set Drive Letter or File Path
sPath = InputBox("Enter the Path to the Drive. If entering just drive letter then add :, if file path remove the end \")
' Step 1 - Set Power Settings
' Step 2 - Remove Bloatware (Win10Apps)
' Step 3 - Install wanted programs
Function GetOsBits()
Set shell = CreateObject("WScript.Shell")
If shell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") = "AMD64" Then
GetOsBits = 64
Else
GetOsBits = 32
End If
End Function
Command_3A = Chr(34) & sPath & "\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" & Chr(34)
Command_3B = Chr(34) & sPath & "\Programs\Lightspeed\UserAgentx86 V2.1.14.msi" & Chr(34)
Command_3C = Chr(34) & sPath & "\Programs\AcroRdrDC1801120058_en_US.exe" & Chr(34) & "& /sAll "
Command_3D = Chr(34) & sPath & "\Programs\Java\jre-8u201-windows-x64.exe" & Chr(34) & "& /s "
Command_3E = Chr(34) & sPath & "\Programs\Java\jre-8u201-windows-i586.exe" & Chr(34) & "& /s "
Command_3F = Chr(34) & sPath & "\Programs\Chrome\ChromeStandaloneSetup64.exe" & Chr(34) & "& /silent /install "
Command_3G = Chr(34) & sPath & "\Programs\Chrome\ChromeStandaloneSetupi586.exe" & Chr(34) & "& /silent /install "
Command_3H = Chr(34) & sPath & "\Programs\ESet Rip and Replace.exe" & Chr(34)
If GetOsBits = 64 Then
wshShell.Run(Command_3A) 'LightSpeed
wscript.Sleep 4000
wshShell.Run(Command_3C) 'Adobe Reader
wscript.Sleep 30000
wshShell.Run(Command_3D) 'Java
wscript.Sleep 30000
wshShell.Run(Command_3F) 'Chrome
wscript.Sleep 30000
wshShell.Run(Command_3H) 'Eset
wscript.Sleep 30000
Else
wshShell.Run(Command_3B) 'LightSpeed x86
wscript.Sleep 4000
wshShell.Run(Command_3C) 'Adobe Reader
wscript.Sleep 4000
wshShell.Run(Command_3E) 'Java x86
wscript.Sleep 30000
wshShell.Run(Command_3G) 'Chrome x86
wscript.Sleep 30000
wshShell.Run(Command_3H) 'Eset
wscript.Sleep 30000
End If
Set oShell = Nothing

Specified File Can't be Found

I have been creating this to do auto-attend of setting up of computers with minimum attention needed. Been working on this and made some breakthroughs.
I want to be able to run this as admin from the start of the script. The problem is is down at the wshShell.Run(Command_3A) 'Lightspeed. The program runs great to that point but then gives me a Specified file can't be found.
The entire thing worked fine until I inserted the Runas admin command. So I am figuring it is somewhere in there. Here is the portion to Runas admin.
Set wshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", , "runas", 1
WScript.Quit
End If
Here is all the code.
Set wshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " RunAsAdministrator", , "runas", 1
WScript.Quit
End If
spath = "C:\ThumbDrive"
' Step 1 - Set Power Settings
Command_1A = "powercfg /change standby-timeout-ac 0"
Command_1B = "powercfg /change standby-timeout-dc 15"
Command_1C = "powercfg /change monitor-timeout-ac 0"
Command_1D = "powercfg /change monitor-timeout-dc 15"
Command_1E = "powercfg /change hibernate-timeout-ac 0"
Command_1F = "powercfg /change hibernate-timeout-dc 15"
wshShell.Run "cmd /k " & Command_1A & "&" & Command_1B & "&" & Command_1C & "&" & Command_1D & "&" & Command_1E & "&" & Command_1F & "& exit"
WScript.Sleep 3000
' Step 2 - Remove Bloatware (Win10Apps)
' Step 3 - Install wanted programs
Command_3A = Chr(34) & spath & "\Programs\Lightspeed\UserAgentx64 V2.1.14.msi" & Chr(34)
Command_3B = Chr(34) & spath & "\Programs\AcroRdrDC1801120058_en_US.exe" & Chr(34) & "& /sAll "
Command_3C = Chr(34) & spath & "\Programs\AzMERITSecureBrowser10.4-2018-08-02.msi" & Chr(34) & "& /passive "
Command_3D = Chr(34) & spath & "\Programs\jre-8u201-windows-x64.exe" & Chr(34) & "& /s "
Command_3E = Chr(34) & spath & "\Programs\ChromeStandaloneSetup64.exe" & Chr(34) & "& /silent /install "
Command_3F = Chr(34) & spath & "\Programs\ESet Rip and Replace.exe" & Chr(34)
wshShell.Run(Command_3A) 'LightSpeed
WScript.Sleep 4000
wshShell.Run(Command_3B) 'Adobe Reader
WScript.Sleep 30000
wshShell.Run(Command_3C) 'AzMerit
WScript.Sleep 4000
wshShell.Run(Command_3D) 'Java
WScript.Sleep 30000
wshShell.Run(Command_3E) 'Chrome
WScript.Sleep 30000
wshShell.Run(Command_3F) 'Eset
So I know the UAC works and all the code works without the UAC Control. Can anyone help me figure out why the UAC control breaks everything.
The CurrentDirectory property returns a string that contains the fully qualified path of the current working directory of the active process.
It points to C:\WINDOWS\system32 for an elevated script. You can either
supply the current working directory from unelevated process to the elevated one, or
elicit it from the Wscript.ScriptFullName property.
Both methods are illustrated in the following code snippet:
Set wshShell = WScript.CreateObject("WScript.Shell")
spath = wshShell.CurrentDirectory
If WScript.Arguments.Length = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) _
& " RunAsAdministrator " & Chr(34) & spath & Chr(34), , "runas", 1
WScript.Quit
End If
'' take out the supplied value:
spath = WScript.Arguments(1)
'' find out where the script resides:
shtap = Left( WScript.ScriptFullName, _
Len( WScript.ScriptFullName) - Len( WScript.ScriptName) -1 )
'' debugging output
Wscript.Echo CStr( WScript.Arguments.Length) & _
vbNewLine & Chr(34) & WScript.Arguments(0) & Chr(34) & _
vbNewLine & Chr(34) & spath & Chr(34) & _
vbNewLine & Chr(34) & shtap & Chr(34) & _
vbNewLine & Chr(34) & wshShell.CurrentDirectory & Chr(34)
' Step 1 - Set Power Settings
Output (using cscript.exe Windows Script Host):
2
"RunAsAdministrator"
"D:\bat\SO"
"D:\bat\SO"
"C:\WINDOWS\system32"
I had to create a input box to specify what the sPath was, and left the UAC reading from C:\WINDOWS\system32. It works wonderfully, just wish I could get it to run without having the input box.

vbscript - object required error

For some reason when I run my vbscript, I am getting an object required at Line 4 Char 1 for the InstallLog. Any idea why this might be occurring?
Dim wshShell, FSO, strDexcomFolder, strDexcom, SysRoot, intRunError, strGroup, strDomain, InstallLog
Const ForWriting = 2
Set InstallLog = FSO.OpenTextFile("Install_Log.txt", ForWriting)
Set wshShell = CreateObject("WScript.Shell")
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
Set FSO = CreateObject("Scripting.FileSystemObject")
strDexcomFolder = "c:\Program Files (x86)\Bioex"
strDomain = "xxxxxxxx"
strGroup = "domain users"
msgbox strDexcomFolder
If FSO.FolderExists(strDexcomFolder) Then
msgbox"start"
intRunError = WshShell.Run("icacls """ & strDexcomFolder & """ /grant " & strDomain & "\" & strGroup & ":(OI)(CI)(M) ", 2, True)
msgbox intRunError
If Err.number <> 0 Then
InstallLog.WriteLine("")
InstallLog.WriteLine("Error Assigning Permissions!")
InstallLog.WriteLine("Error #: "&Err.Number&", "&Err.Description&"")
InstallLog.WriteLine("")
MsgBox"Error assigning permissions!"
InstallLog.close
End If
Else
Wscript.Echo "Error: folder " & strDexcomFolder & " does not exist"
End If
WScript.Quit
Here. This should get you going. The icacls command is now being echoed into the log so you can confirm your syntax is being passed correctly. Edit - Some command line programs do not pass arguments correctly without preceding them with "cmd.exe /C". I added that also along with full path to icacls.exe in case you are running from a location that is not in the system path.
Option Explicit
Dim wshShell, objFSO, strDexcomFolder, strDexcom, SysRoot, intRunError, strGroup, strDomain, InstallLog, strWinDir
Set wshShell = CreateObject("WScript.Shell")
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
strWinDir = WshShell.ExpandEnvironmentStrings("%windir%")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const ReadOnly = 1
strDexcomFolder = "c:\Program Files (x86)\Bioex"
strDomain = "xxxxxxxx"
strGroup = "domain users"
Set InstallLog = objFSO.CreateTextFile("Install_Log.txt", True)
MsgBox strDexcomFolder
If objFSO.FolderExists(strDexcomFolder) Then
MsgBox "Start"
InstallLog.WriteLine("Running Command - " & strWinDir & "\System32\cmd.exe /C " & strWinDir & "\System32\icacls.exe " & Chr(34) & strDexcomFolder & Chr(34) & " /grant " & Chr(34) & strDomain & "\" & strGroup & chr(34) & ":(OI)(CI)(M)")
intRunError = WshShell.Run(strWinDir & "\System32\cmd.exe /C " & strWinDir & "\System32\icacls.exe " & Chr(34) & strDexcomFolder & Chr(34) & " /grant " & Chr(34) & strDomain & "\" & strGroup & chr(34) & ":(OI)(CI)(M)", 2, True)
MsgBox intRunError
If intRunError <> 0 Then
InstallLog.WriteLine("")
InstallLog.WriteLine("Error Assigning Permissions!")
InstallLog.WriteLine("Error #: " & Err.Number & ", " & Err.Description)
InstallLog.WriteLine("")
MsgBox "Error assigning permissions!"
End If
Else
InstallLog.WriteLine("Error: folder " & strDexcomFolder & " does not exist")
WScript.Echo "Error: folder " & strDexcomFolder & " does not exist"
End If
InstallLog.close
WScript.Quit
You will definitely need to have this line of code PRECEDES those which are using the FSO object or calling a function like FSO.OpenTextFile
Set FSO = CreateObject("Scripting.FileSystemObject")

Passing variable arguments to ShellExecute in vbscript

I am trying to learn how to run a vbscript in elevated mode. I can't quite get this to work if there are arguments to the vbscript I am trying to run elevated.
Here is a simple example:
OSVersion.vbs
' Return a string indicating the operating system
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
'Wscript.echo Wscript.Arguments(0)
For Each objOperatingSystem in colOperatingSystems
Wscript.StdOut.Write objOperatingSystem.Caption
Wscript.StdOut.WriteBlankLines(1)
Wscript.StdOut.Write "Version " & objOperatingSystem.Version
Next
RunElevated.vbs
' Run the script in elevated mode
'
' This will be needed to install programs into Program Files
prgName = Wscript.Arguments.Item(0)
prgArgs = ""
If Wscript.Arguments.Count > 1 Then
For i = 1 To Wscript.Arguments.Count - 1
prgArgs = prgArgs & " " & Wscript.Arguments.Item(i)
Next
End If
Wscript.echo "Running: " & prgName & prgArgs
Set objShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetParentFolderName (WScript.ScriptFullName)
If fso.FileExists(strPath & "\" & prgName) Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\" & prgName & Chr(34), _
"", "runas", 1
Else
Wscript.echo "Script file not found"
End If
The OSVersion.vbs script runs fine:
cscript OSVersion.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Microsoft Windows 7 Home Premium
Version 6.1.7601
Problem #1 When I try to run this elevated, nothing appears on stdout
C:\Users\ChemModeling\Documents\FreeThink\Java\install>cscript RunElevated.vbs OSVersion.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Running: OSVersion.vbs
Problem #2 I cannot figure out how to include an argument to the script I am passing to RunElevated. I read that you should use a syntax like '"my parameters here"' so I tried this:
' Run the script in elevated mode
'
' This will be needed to install programs into Program Files
prgName = Wscript.Arguments.Item(0)
prgArgs = ""
If Wscript.Arguments.Count > 1 Then
For i = 1 To Wscript.Arguments.Count - 1
prgArgs = prgArgs & " " & Wscript.Arguments.Item(i)
Next
End If
Wscript.echo "Running: " & prgName & prgArgs
Set objShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetParentFolderName (WScript.ScriptFullName)
If fso.FileExists(strPath & "\" & prgName) Then
objShell.ShellExecute "wscript.exe", _
Chr(39) & Chr(34) & strPath & "\" & prgName & prgArgs & Chr(34) & Chr(39), _
"", "runas", 1
Else
Wscript.echo "Script file not found"
End If
If I run:
cscript RunElevated.vbs OSVersion.vbs Test
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Running: OSVersion.vbs Test
I then get an error "There is no engine for file extension ".vbs Test".
Can anyone suggest what I need to change?
I have made some headway in resolving this problem-
I changed the first script to:
OSVersion.vbs
' Return a string indicating the operating system
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
'Wscript.echo Wscript.Arguments(0)
For Each objOperatingSystem in colOperatingSystems
Wscript.Echo objOperatingSystem.Caption
Wscript.Echo "Version " & objOperatingSystem.Version
Next
so I could actually see if something happened.
I changed the second script to:
' Run the script in elevated mode
'
' This will be needed to install programs into Program Files
prgName = Wscript.Arguments.Item(0)
prgArgs = ""
If Wscript.Arguments.Count > 1 Then
For i = 1 To Wscript.Arguments.Count - 1
prgArgs = prgArgs & " " & Wscript.Arguments.Item(i)
Next
End If
Wscript.echo "Running: " & prgName & prgArgs
Set objShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetParentFolderName (WScript.ScriptFullName)
If fso.FileExists(strPath & "\" & prgName) Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\" & prgName & Chr(34) & prgArgs, _
"", "runas", 1
Else
Wscript.echo "Script file not found"
End If
and used: wscript RunElevated.vbs OSVersion.vbs Test
And now I see the information I am echoing in popups when the script runs.
So I am back to problem #1, I am starting a new shell to run in administrator mode, so if I switch to cscript and try to write the information to stdout or use Wscript.StdOut.Write, it will appear in the new shell and I will never see it, or at least that's what I believe the problem is.
Is there a standard way to address this issue?
Problem #1:
You're not seeing any output when you run your script elevated, because the RunElevated.vbs script re-launches your OSVersion.vbs script using WScript.exe. OSVersion.vbs uses Standard Input and Output which are only available from the console. Changing the command line in RunElevated.vbs to use cscript.exe instead of wscript.exe solves this problem. For more, check out my article Error Trapping and Capturing Third-Party Output in WSH.
Problem #2
It looks like your problem here is that you're mixing different kinds of quotes. A command should look like this:
"C:\path with spaces\command.exe" "parameter with spaces" "and another"
Try out these scripts.
RunElevated.vbs
' Run a script in Elevated Mode
strName = WScript.Arguments.Item(0)
strArgs = ""
If WScript.Arguments.Count > 1 Then
For i = 1 To WScript.Arguments.Count - 1
strArgs = strArgs & " " & WScript.Arguments.Item(i)
Next
End If
WScript.echo "Running: ", strName, strArgs
Set objShell = CreateObject("Shell.Application")
Set objFso = CreateObject("Scripting.FileSystemObject")
strPath = objFso.GetParentFolderName(WScript.ScriptFullName)
strParams = Chr(34) & strPath & "\" & strName & Chr(34)
If strArgs <> "" Then
strParams = strParams & " " & Chr(34) & strArgs & Chr(34)
End If
If objFso.FileExists(strPath & "\" & strName) Then
objShell.ShellExecute "cscript.exe", strParams, "", "runas", 1
Else
WScript.echo "Script file not found"
End If
OSVersion.vbs
'Test passing arguments to launched script
If WScript.Arguments.Count > 0 Then
For i = 0 To WScript.Arguments.Count - 1
strArgs = Trim(strArgs & " " & WScript.Arguments.Item(i))
Next
End If
' Return a string indicating the operating system
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
WScript.StdOut.Write objOperatingSystem.Caption
WScript.StdOut.WriteBlankLines(1)
WScript.StdOut.Write "Version " & objOperatingSystem.Version
WScript.StdOut.WriteBlankLines(2)
Next
' Print out any passed arguments
WScript.StdOut.WriteBlankLines(2)
WScript.StdOut.Write strArgs
WScript.StdOut.WriteBlankLines(2)
' Keep the window from closing before you get a chance to read it
WScript.StdOut.Write "Press any key to continue "
strInput = WScript.StdIn.Read(1)
In order to capture the output, I would probably try a different approach using the WshShell Execute method. There are command line tools available such as Elevate or HStart that can launch command lines with elevated privileges. Here's how I would do it using Elevate.
' Run a script in Elevated Mode
strName = WScript.Arguments.Item(0)
strArgs = ""
If WScript.Arguments.Count > 1 Then
For i = 1 To WScript.Arguments.Count - 1
strArgs = strArgs & " " & WScript.Arguments.Item(i)
Next
End If
WScript.echo "Running: ", strName, strArgs
Set WshShell = CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")
strPath = objFso.GetParentFolderName(WScript.ScriptFullName)
strParams = Chr(34) & strPath & "\" & strName & Chr(34)
If strArgs <> "" Then
strParams = strParams & " " & Chr(34) & strArgs & Chr(34)
End If
If objFso.FileExists(strPath & "\" & strName) Then
Set WshShellExec = WshShell.Exec("elevate cscript.exe " & strParams)
Select Case WshShellExec.Status
Case WshFinished
strOutput = WshShellExec.StdOut.ReadAll
Case WshFailed
strOutput = WshShellExec.StdErr.ReadAll
End Select
Else
WScript.echo "Script file not found"
End If
WScript.echo strOutput
I discovered two interesting things; first, I had to get the proper number of quotations and spaces, second, I had to actually call an interim script first and then call the final script elevated.
Intial Calling Script snippet:
If objFSO.FileExists("C:\Windows\System32\elevate.vbs") Then
objWSHShell.ShellExecute "wscript.exe", Chr(34) & Chr(34) & "C:\Windows\System32\elevate.vbs" & Chr(32) & strCampus & Chr(32) & strLocation & Chr(32) & strAssetTag & Chr(34) & Chr(34), "", "", 1
Else
MsgBox "Regbrand script file not found",0,"Error Message"
End If
Notice the Chr() statements and the 3 variables being passed from the original script to the interim script.
Interim Script snippet:
If Args.Count = 0 Then
MsgBox "Missing arguments to UAC permissions script.",0,"Missing Parameters"
Else
strCampus = Args.Item(0)
strLocation = Args.Item(1)
strAssetTag = Args.Item(2)
strFilePath = objFSO.GetParentFolderName (WScript.ScriptFullName)
If objFSO.FileExists("C:\Windows\System32\regbrand.vbs") Then
objWSHShell.ShellExecute "wscript.exe", Chr(34) & Chr(34) & "C:\Windows\System32\regbrand.vbs" & Chr(32) & strCampus & Chr(32) & strLocation & Chr(32) & strAssetTag & Chr(34) & Chr(34), "", "runas", 1
Else
MsgBox "Script file not found",0,"Error Message"
End If
End If
In the interim script we pass the same arguments to the final script. Again, note the Chr() statements.
The first script passes 3 arguments and calls the interim script, which in turn calls the 3rd script "regbrand.vbs" with elevated permissions. It works just like it should. If you remove the interim script, it did not work. I have not figured that out yet, but believe it has something to do with calling the original script from a network location. However, I hope the syntax for calling and passing the parameters is helpful.

script to pull info from windows Drive

I need a script that could ran and pull information from any drive on a windows operating system (Server 2003), listing all files and folders which contain the following fields:
Full file path (e.g. C:\Documents and Settings\user\My Documents\testPage.doc)
File type (e.g. word document, spreadsheet, database etc)
Size
When Created
When last modified
When last accessed
Any help is appreciated.
Thanks
try this vbscript
Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
If objDIR <> "\System Volume Information" Then
For Each eFolder in objDIR.SubFolders
Go eFolder
Next
End If
For Each strFile In objDIR.Files
WScript.Echo "Full Path: " & strFile.Path
WScript.Echo "File Size(bytes): " & strFile.Size
WScript.Echo "File Date modified: " & strFile.DateLastModified
WScript.Echo "File Date Created: " & strFile.DateCreated
WScript.Echo "File Date accessed: " & strFile.DateLastAccessed
Next
End Sub
on command line
c:\test> cscript //nologo myscript.vbs c:\
To save the results to a csv file modify ghostdog74's code as follows.
Set objFS=CreateObject("Scripting.FileSystemObject")
WScript.Echo Chr(34) & "Full Path" &_
Chr(34) & "," & Chr(34) & "File Size" &_
Chr(34) & "," & Chr(34) & "File Date modified" &_
Chr(34) & "," & Chr(34) & "File Date Created" &_
Chr(34) & "," & Chr(34) & "File Date Accessed" & Chr(34)
Set objArgs = WScript.Arguments
strFolder = objArgs(0)
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
If objDIR <> "\System Volume Information" Then
For Each eFolder in objDIR.SubFolders
Go eFolder
Next
End If
For Each strFile In objDIR.Files
WScript.Echo Chr(34) & strFile.Path & Chr(34) & "," &_
Chr(34) & strFile.Size & Chr(34) & "," &_
Chr(34) & strFile.DateLastModified & Chr(34) & "," &_
Chr(34) & strFile.DateCreated & Chr(34) & "," &_
Chr(34) & strFile.DateLastAccessed & Chr(34)
Next
End Sub
Then call it from the command line like this.
c:\test> cscript //nologo myscript.vbs "c:\" > "C:\test\Output.csv"

Resources