How to compare the result of cmd in the vbscript? - vbscript

I ask about getting the result of this cmd command netstat -a | find /c "TCP"
and compare it with specific value using VBSCRIPT
thanks

It's difficult to answer question with so less details, but lets try anyway...
Example code below will illustrate 2 ways to store the command-line output to a variable in your vbs. WScript.Echo is used just as evidence (display the result).
'** VAR#1 (using Exec & StdOut) ----------
Dim ObjExec
Dim strFromProc
Set objShell = CreateObject("WScript.Shell")
Set ObjExec = objShell.Exec("%comspec% /c " _
& "netstat -a | find /c " & Chr(34) & "TCP" & Chr(34))
Do Until ObjExec.Stdout.atEndOfStream
strFromProc = strFromProc & ObjExec.StdOut.ReadLine & vbNewLine
Loop
WScript.Echo strFromProc ' display result from variable strFromProc
Set objShell = Nothing
Set ObjExec = Nothing
'** VAR#2 (using Run) --------------------
Const cLogFile = "result.txt"
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c netstat -a | find /c " _
& Chr(34) & "TCP" & Chr(34) & ">" & cLogFile, 0, True
Dim oFile, Result
With CreateObject("Scripting.FileSystemObject")
If .FileExists(cLogFile) Then
Set oFile = .OpenTextFile(cLogFile)
Result = oFile.ReadLine
oFile.Close
Set oFile = .GetFile(cLogFile)
oFile.Delete
End If
End With
WScript.Echo Result ' display result from variable Result
Set oFile = Nothing
Set objShell = Nothing

Related

.vbs GhostScript folder loop PDF to JPEG [duplicate]

This question already has answers here:
Run Command Line & Command From VBS
(2 answers)
Closed 10 months ago.
how can I transform this innocent cmd batch code file to work in a ".vbs" file?
#echo off
setlocal
for %%I in (*.pdf) do (
md "%%~nI"
"C:\GS\gswin32c.exe" -dNOPAUSE -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dJPEGQ=100 -r600 -sOutputFile="%%~nI\p%%02d.jpeg" "%%~I")
I got this far, I don't know how to write the GS script parameters
dim sFolder,MyArray
dim FSO,OutPutFile
dim networkInfo
Dim objShell
sFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set networkInfo = CreateObject("WScript.NetWork")
Set objShell = WScript.CreateObject( "WScript.Shell" )
For Each oFile In oFSO.GetFolder(sFolder).Files
If Instr( 1, oFile.Name, ".pdf", vbTextCompare )>0 Then
dirname = Replace(oFile.Name,"." & oFSO.GetExtensionName(oFile.Path),"")
on error resume next
oFSO.CreateFolder dirname
on error goto 0
objShell.Run "C:\GS\gswin32c.exe" '/////// here help!
Set objShell = Nothing
End if
Next
Set oFSO = Nothing
well, I finished the code, works as intended
In the end, I generated a ".bat" and run it because if processed as an individual "objShell.Run" it opened for every pdf a separate cmd window, which was not acceptable.
thanks all for the hints. :)
dim sFolder,MyArray
dim FSO,OutPutFile
dim networkInfo
Dim objShell
dim commmand,commmand2
dim ffso, crit
Dim intAnswer
dim nn
sFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set networkInfo = CreateObject("WScript.NetWork")
Set objShell = WScript.CreateObject("WScript.Shell")
Set ffso = CreateObject("Scripting.FileSystemObject")
tmpfile=ffso.getspecialfolder(2) & "\" & ffso.gettempname
tmpfile=tmpfile & ".bat"
set ffso=createobject("scripting.filesystemobject")
set ots=ffso.opentextfile(tmpfile,2,true)
ots.writeline "#echo off"
ots.writeline "setlocal"
intAnswer = _
Msgbox("Procesare doar cu Rutare?" & vbNewLine & "Yes - doar cele cu #"& vbNewLine & "No - toate fisierele", _
vbYesNo, "Selectie ")
If intAnswer = vbYes Then
crit = "#.pdf"
Else
crit = ".pdf"
End If
nn = 0
For Each oFile In oFSO.GetFolder(sFolder).Files
If Instr( 1, oFile.Name, crit, vbTextCompare )>0 Then
nn=nn+1
dirname = Replace(oFile.Name,"." & oFSO.GetExtensionName(oFile.Path),"")
on error resume next
oFSO.CreateFolder dirname
on error goto 0
if Instr( 1, oFile.Name, "#.pdf", vbTextCompare )>0 then
MyArray = Split(oFile.Name, "#", -1, 1)
rutare = MyArray(UBound(MyArray)-1)
else
rutare = Replace(oFile.Name,"." & oFSO.GetExtensionName(oFile.Path),"")
end if
Set FSO = CreateObject("Scripting.FileSystemObject")
Set OutPutFile = FSO.OpenTextFile( sFolder & "\" & dirname & "\" & rutare & ".txt" ,8 , True)
OutPutFile.WriteLine(FormatDateTime(Now, vbGeneralDate) & vbtab & networkInfo.UserName & vbtab & networkInfo.ComputerName)
OutPutFile.close
Set FSO= Nothing
commmand = "C:\GS\gswin32c.exe -dNOPAUSE -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dJPEGQ=100 -r600 -sOutputFile=" & chr(34) & sFolder & "\" & dirname & "\p%%02d.jpeg" & chr(34) & " " & chr(34) & sFolder & "\" & oFile.Name & chr(34)
ots.writeline "#echo -----------------------------------------------------------------------------"
ots.writeline "#echo Procesare - " & oFile.Name
ots.writeline "#echo -----------------------------------------------------------------------------"
ots.writeline commmand
End if
Next
if nn=0 then
ots.writeline "#echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ots.writeline "#echo Nu exista PDF uri sau cu extensia " & crit
ots.writeline "#echo Pentru toate PDF-urile se selecteaza No!"
ots.writeline "#echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
end if
ots.writeline "timeout 6"
objShell.run "%comspec% /c " & tmpfile,1, True
ots.close
ffso.deletefile tmpfile,true
set ffso=nothing
Set oFSO = Nothing

rename a file using VBScript, launch file, wait, and rename again

I need to create a vbs script (for maintenance purposes) that renames foo.txt to a foo.bat and launch foo.bat and when foo.bat ends, rename foo.bat again to foo.txt
This is my script vbs:
On Error Resume next
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run NewPath, vbHide, true
Fso.MoveFile "foo.bat", "foo.txt"
On Error GoTo 0
the script executes well. Rename foo.txt to foo.bat. Launches foo.bat, but does not expect foo.bat to end and renames it to foo.txt.
I changed this line, nothing happens:
objshell.Run NewPath, vbHide, 1, true
What do I need or what did I do wrong?
Alternative Solution (no VBScript): (By suggestion of #KenWhite)
code:
On Error Resume next
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run NewPath, true
On Error GoTo 0
And at the end of foo.bat:
ren foo.bat foo.txt
exit
Thanks
Here is a possible solution just in case anyone is wondering how to solve this problem without resorting to the alternate proposal mentioned above.
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run "%COMSPEC% /c " & NewPath, 1, true
' Changes start here
'===================================================================
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' Hold execution until cmd.exe process is done
do
' Get cmd.exe processes
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE 'cmd.exe'")
WScript.Sleep 250
Loop while colProcessList.count > 0
Fso.MoveFile "foo.bat", "foo.txt"

If - Else If statement in VBScript

Else If exist "K:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "K:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "F:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "F:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "E:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "E:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "D:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "D:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
Else If exist "C:\ICT project"
(Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Chr(34) & "C:\ICT project" & Chr(34), 0
Set WshShell = Nothing)
What is wrong with the code? And how well the If statement is used? please help me.
How can this code be improved?
To search for a program on different drives and call it (perhaps with arguments and decent quotes), use a loop over the drives:
Option Explicit
Const csPrg = "pipapo"
Function qq(s) : qq = """" & s & """" : End Function
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim sDL, sFSpec, sCmd
For Each sDL In Split("A:\ C:\ K:\ E:\")
sFSpec = goFS.BuildPath(sDL, csPrg)
sCmd = Join(Array("%comspec% /c", qq(sFSpec), "/p:arm", qq("one two three"), 4711))
WScript.Echo sCmd
If goFS.FileExists(sFSpec) Then Wscript.Echo " ==> now run sCmd (and perhaps Exit For)"
Next
output:
cscript 47922850.vbs
%comspec% /c "A:\pipapo" /p:arm "one two three" 4711
%comspec% /c "C:\pipapo" /p:arm "one two three" 4711
%comspec% /c "K:\pipapo" /p:arm "one two three" 4711
%comspec% /c "E:\pipapo" /p:arm "one two three" 4711
==> now run sCmd (and perhaps Exit For)

VBScript error problems: Expected End 800A03F6

I am trying to make a vbs work, the idea is it will remotely install an msi, to a list of machines contained with a txt file.
I am getting multiple errors, the first is:
wrong number of arguments or invalid property assignment: "WshShell.Exec" Line 27, Char 1
WshShell.Exec "%COMSPEC% /C COPY " & StrInstallFile & " \\" & strComputer _
& "\C$\Windows\Temp", 0, TRUE
I seemd to have got round this with:
Set WshExec = WshShell.Exec......
then got:
expected end of statement line 27 cahr 29
adding an &:
Set WshExec = WshShell.Exec & "%COMSPEC%.....
now gets me:
expected end of statement line 27 char 110
which is the penultimate comma
Set WshExec = WshShell.Exec & "%COMSPEC% /C COPY" & StrInstallFile _
& " \\" & strComputer & "\C$\Windows\Temp", 0, TRUE
so i am not sure what is wrong at this point, and whether changing the whole line to a set was the right thing to have done.
You are mixing .Run and .Exec. The prototype for .Exec:
object.Exec(strCommand)
shows that you need someting like:
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrInstallFile & " \" & strComputer & "\C$\Windows\Temp")
If you want .Run instead, try something like:
Dim iRet : iRet = WshShell.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
Dim iRet : iRet = WshShell.Run("%comspec% ...", 0, True)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("MachineList.Txt", 1)
StrInstallFile="install_flash_player_11_active_x.msi"
StrNoUpdateFile="mms.cfg"
StrInstallCMD="msiexec.exe /qn /i "
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
' --------- Check If PC is on -------------
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 1 -w 1000 " & strComputer) 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
' ---------- Successful ping - run remote commands ----------------
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' --------- Copy msi to windows temp folder
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrInstallFile & " \\" & strComputer & "\C$\Windows\Temp")
' --------- execute msi file on remote machine
Set oExec = WshShell.Exec("%COMSPEC% /C psexec \\" & StrComputer & " " & strInstallCMD & "c:\Windows\Temp\" & StrInstallFile)
' --------- Copy no "no update" file to remote machine, first line is for win7, second for xp
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrNoUpdateFile & " \\" & strComputer & "\C$\Windows\SysWOW64\Macromed\Flash")
Set oExec = WshShell.Exec("%COMSPEC% /C COPY " & StrNoUpdateFile & " \\" & strComputer & "\C$\Windows\system32\macromed\flash")
Else
' ---------- Unsuccessful ping - Leave computer name in MachineList.txt and continue ----------------
strNewContents = strNewContents & strComputer & vbCrLf
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("MachineList.txt", 2)
objFile.Write strNewContents
objFile.Close

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.

Resources