1

I am trying to pull ipconfig /all output and put it into a text file. I have created a small VBScript that runs ipconfig without issues. Then I call it in another VBScript. All of this runs, but the output text file remains empty and the primary VBScript doesn't seem to write anything after the ipconfig.vbs runs.

Here is the sample from the primary .vbs script:

' Pulling network config
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\Users\dsadmin\Desktop\LogNet\network_config.txt", 8)
set objFile = objFSO.OpenTextFile("C:\Users\dsadmin\Desktop\LogNet\network_config.txt")
objShell.Run("cscript //nologo C:\Users\dsadmin\Downloads\ipconfig.vbs >C:\Users\dsadmin\Desktop\LogNet\network_config.txt")

Here is the script it calls (ipconfig.vbs):

Set objShell = CreateObject("WScript.Shell")
objShell.Run("ipconfig /all")

I'm out of ideas when it comes to shuffling things around.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
wyatt8919
  • 13
  • 1
  • 4
  • Possible duplicate of [Redirect output processed via vbscript (cscript) to file](http://stackoverflow.com/questions/16572252/redirect-output-processed-via-vbscript-cscript-to-file) – user692942 Feb 08 '17 at 11:50
  • You not passing anything back to the Standard Output Stream, use `.Exec()` to access the `.StdOut` stream. – user692942 Feb 08 '17 at 11:53
  • @Lankymart You mean use "objShell.Exec" instead of "objShell.Run"? When I do that, it still fails to output. – wyatt8919 Feb 08 '17 at 12:03

2 Answers2

1

Redirection (>) is a CMD builtin feature. You need to run the statement in CMD to be able to use it:

objShell.Run "%COMSPEC% /c cscript //NoLogo C:\ipconfig.vbs >C:\network_config.txt"

Of course you need to ensure that the second script writes to STDOUT in the first place, as @Lankymart pointed out.

If all your second script does is running ipconfig /all there's not much point in wrapping that in a separate script, though. Just run it directly:

objShell.Run "%COMSPEC% /c ipconfig /all >C:\network_config.txt"
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Alright, I replaced `objShell.run("cscript //nologo C:\Users\dsadmin\Downloads\ipconfig.vbs >C:\Users\dsadmin\Desktop\LogNet\network_config.txt")` with `objShell.run("cscript //nologo C:\Users\dsadmin\Downloads\ipconfig.vbs >C:\Users\dsadmin\Desktop\LogNet\network_config.txt")` and I now get a "invalid character" error. Do I need parentheses somewhere? – wyatt8919 Feb 08 '17 at 11:59
  • The first example will not work without also redirecting the Standard Output Stream in `ipconfig.vbs`. – user692942 Feb 08 '17 at 12:14
  • @wyatt8919 You replaced a statement with the exact same statement, and now you're getting an error? I somehow doubt that. – Ansgar Wiechers Feb 08 '17 at 12:24
  • @AnsgarWiechers I'm sorry but that does seem to be what's happening. I'm using this statement exactly: `objShell.run "%COMSPEC% /c ipconfig /all > C:\Users\dsadmin\Desktop\LogNet\network_config.txt"` and I keep getting the handle error – wyatt8919 Feb 08 '17 at 12:35
  • @wyatt8919 as I've said three or four times now, it's because where the primary script is running you have a file called `ipconfig.vbs` so it runs that instead of the in-built `ipconfig` command *(assumes because they have the same name they are the same, ignores extension)*. Just rename the `ipconfig.vbs` to something else or move it out of the primary script directory to stop the script being executed by default. – user692942 Feb 08 '17 at 12:37
  • 1
    @Lankymart Alright alright alright. I'm dumb. I see what everyone is trying to tell me now. I deleted the `ipconfig.vbs` and it worked. Didn't realize that CMD assuming things would get me into trouble. Thanks again, much appreciated. – wyatt8919 Feb 08 '17 at 12:42
  • @wyatt8919 we got there in the end. Glad it's now working for you. – user692942 Feb 08 '17 at 12:44
1

You have two problems with that approach

  1. As @Ansgar-Wiechers points out the > redirection is part of CMD.

  2. Once the redirection is working you have to retrieve the Standard Output from the executed command and redirect it to the cscript.exe output. Unfortunately .Run() doesn't provide access to the Standard Output Stream you have to use .Exec() instead.

Here is an example (assumes all files in same direction, but can be modified);

' Pulling network config
Set objShell = CreateObject("WScript.Shell")
Call objShell.Run("%COMSPEC% /c cscript //nologo ipconfig.vbs > network_config.txt")

in the ipconfig.vbs

Set objShell = CreateObject("WScript.Shell")
Set exec = objShell.Exec("ipconfig /all")
'Redirect output from executed command to the script output.
Call WScript.StdOut.Write(exec.StdOut.ReadAll)

Output in network_config.txt

Windows IP Configuration

   Host Name . . . . . . . . . . . . : 
   Primary Dns Suffix  . . . . . . . : 
   Node Type . . . . . . . . . . . . : 
   IP Routing Enabled. . . . . . . . : No
   WINS Proxy Enabled. . . . . . . . : No
   DNS Suffix Search List. . . . . . : 

Wireless LAN adapter Local Area Connection* 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : 
   Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter
   Physical Address. . . . . . . . . : 
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
...

... Truncated for readability and sensitive data removed

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • 1
    This works! Excellent. Thank you so much! Now, if you wouldn't mind pass on a bit of knowledge to a beginner, I have a couple questions that will hopefully help me avoid this problem in the future. It's not clear to me what the "%COMSPEC%" string does. Is there a simple explanation to be had? Secondly - with regards to the "ipconfig.vbs" - how does "set exec" allow output as opposed to "obj.ShellRun" ? – wyatt8919 Feb 08 '17 at 12:14
  • @wyatt8919 to be perfectly honest it's a lot of work for not a lot of gain, Ansgars suggestion to run it directly should work with less complexity. **Edit:** Although I've just tested it and it's failing for me, will investigate...thought to be honest that should work. – user692942 Feb 08 '17 at 12:16
  • I did try, but doing it his way gives me a "handle is invalid" error. – wyatt8919 Feb 08 '17 at 12:20
  • @wyatt8919 I got the same but realised it's still calling `ipconfig.vbs` because it exists in the current directory even though we call `ipconfig` without the `.vbs` extension, just rename your `ipconfig.vbs` file and it will work. – user692942 Feb 08 '17 at 12:21
  • 1
    @wyatt8919 `%COMSPEC%` is a Windows environment variable holding the full path to `cmd.exe`. The `Exec()` method provides you with an object that gives you access to the standard file descriptors of the spawned process. The `Run` method doesn't do that. – Ansgar Wiechers Feb 08 '17 at 12:22
  • @AnsgarWiechers still, `objShell.Run "%COMSPEC% /c ipconfig /all > network_config.txt"` is far simplier then this. – user692942 Feb 08 '17 at 12:25
  • @AnsgarWiechers Got it. I think I understand. Thank you for the explanation. I am trying to follow Lankymart's explanation, but I'm not sure I'm getting it fully. Is there a way to avoid using two different scripts here by using your method? – wyatt8919 Feb 08 '17 at 12:29
  • 1
    By running `objShell.Run "%COMSPEC% /c ipconfig /all > network_config.txt"` from your primary script you won't need a second script. I thought that was rather obvious. – Ansgar Wiechers Feb 08 '17 at 12:31
  • @wyatt8919 What part are you not following? When testing Ansgars second method *(which I'd recommend by the way)* the command `objShell.Run "%COMSPEC% /c ipconfig /all > network_config.txt"` fails because the previous VBS file you are using called `ipconfig.vbs` is still in the directory, so the CMD assumes you want to execute that not the built-in `ipconfig` command. Just rename `ipconfig.vbs` to `ipconfig1.vbs` and it will work. – user692942 Feb 08 '17 at 12:32
  • @Lankymart I apologize but you're going to have to really break it down for me because this is all new. How does renaming this help? Isn't the idea to avoid running a second script? Why is the CMD assuming that ipconfig is actually "ipconfig.vbs" when it's no longer in the script? – wyatt8919 Feb 08 '17 at 12:38
  • @wyatt8919 this is just happening because of the order in which you have tried things. You had a script called `ipconfig.vbs` when you were first testing the method, correct? You don't need it now but it's still there, so when you execute your primary script and call `ipconfig /all` because there is a file in that directory with the same name *(but different extension)* it assumes you want to run that and because the `ipconfig.vbs` doesn't understand the `>` redirection it throws an error `Handle is Invalid`. By renaming the file you stop it being called accidently. – user692942 Feb 08 '17 at 12:42