1

I am looking to create a script where I will accept the input from the user, pass it to command line as parameter and display the result.

Something like:

Dim WshShell, Return,sctask,arg

arg=inputbox("Enter the computername")
Set arg = Wscript.Arguments
Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run("C:\schtasks /query /s &arg")
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user2586205
  • 11
  • 1
  • 2

2 Answers2

4

In addition to what Ekkehard.Horner said: you can't append a collection (WScript.Arguments) like a simple string:

Set arg = Wscript.Arguments
...
WshShell.Run "C:\schtasks /query /s " & arg

You need to re-build the argument string like this:

ReDim args(WScript.Arguments.Count-1)
For i = 0 To WScript.Arguments.Count-1
  If InStr(WScript.Arguments(i), " ") > 0 Then
    args(i) = Chr(34) & WScript.Arguments(i) & Chr(34)
  Else
    args(i) = WScript.Arguments(i)
  End If
Next

WshShell.Run "C:\schtasks /query /s " & Join(args, " ")

Also, there usually isn't a schtasks executable in C:\.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

Re:

WshShell.Run("C:\schtasks /query /s &arg")

(1) VBScript does not interpolate variables, you have to concatenate the content of arg (properly):

WshShell.Run "C:\schtasks /query /s " & arg

(WRT @Ansgar's posting: I assume arg holds the computer name (hopefully) optained by

arg=inputbox("Enter the computername")

and that your next line:

Set arg = Wscript.Arguments

was added by the copy-&-paste-goblin)

(2) You must not use param list () when calling a sub/not receiving a return value. So either use the above line or

iRet = WshShell.Run("C:\schtasks /query /s " & arg)

Re: display the result

As .Run just executes the command and returns a status, the least cost/most gain method to get the output of schtasks is .Exec:

Option Explicit

Dim oWSH : Set oWSH = CreateObject("WScript.Shell")
Dim sCmd : sCmd     = "schtasks /Query /S winxpsp3"
Dim sRes : sRes     = oWSH.Exec(sCmd).Stdout.ReadAll()
WScript.Echo sRes

Use the docs to learn how to improve my demo snippet - a production ready version should do some error handling.

If you don't want to use .Exec, see this answer for sample code that uses .Run and re-direction to get the command's output.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96