Results 1 to 5 of 5

Thread: How to add a variable to a shell command in a vbscript

  1. #1
    Dee Guest

    How to add a variable to a shell command in a vbscript

    Hello

    I'm trying to add a variable to a shell command in a vb scirpt.
    (Newish to scripting)


    I want to get the IP address, MasK and Gateway inputs and add them to the
    shell command as in the below command.

    CODE


    Sub Manual_IP
    ipAnswer = window.prompt("Please enter your IP address")
    maskAnswer = window.prompt("Please enter the mask")
    gatewayAnswer = window.prompt("Please enter the gateway IP address")
    strIPHost=ipAnswer
    strmaskHost=strAnswer
    strgateway=gatewayAnswer
    Set WShell = CreateObject("WScript.Shell")
    WShell.exec("netsh interface ip set address local static strIPHost
    strmaskHost strgateway 1")

    end sub


    My question is can this be done, or am I missing thre syntax for putting the
    variables into the command.

    any suggestions?

    Thanks
    D

    --
    Dee

  2. #2
    Pegasus \(MVP\) Guest

    Re: How to add a variable to a shell command in a vbscript


    "Dee" <Dee@discussions.microsoft.com> wrote in message
    news:95294E76-9BDA-4E5C-A235-A15BE9AFE8A6@microsoft.com...
    > Hello
    >
    > I'm trying to add a variable to a shell command in a vb scirpt.
    > (Newish to scripting)
    >
    >
    > I want to get the IP address, MasK and Gateway inputs and add them to the
    > shell command as in the below command.
    >
    > CODE
    >
    >
    > Sub Manual_IP
    > ipAnswer = window.prompt("Please enter your IP address")
    > maskAnswer = window.prompt("Please enter the mask")
    > gatewayAnswer = window.prompt("Please enter the gateway IP address")
    > strIPHost=ipAnswer
    > strmaskHost=strAnswer
    > strgateway=gatewayAnswer
    > Set WShell = CreateObject("WScript.Shell")
    > WShell.exec("netsh interface ip set address local static strIPHost
    > strmaskHost strgateway 1")
    >
    > end sub
    >
    >
    > My question is can this be done, or am I missing thre syntax for putting
    > the
    > variables into the command.
    >
    > any suggestions?
    >
    > Thanks
    > D
    >
    > --
    > Dee


    In VB Script you can obtain user input like so:

    strIP = InputBox("Please enter your IP address")

    The user's input is now stored in strIP. Might be a good
    idea to download script56.chm for the basic stuff.



  3. #3
    Tom Lavedas Guest

    Re: How to add a variable to a shell command in a vbscript

    On Apr 28, 10:52 am, Dee <D...@discussions.microsoft.com> wrote:
    > Hello
    >
    > I'm trying to add a variable to a shell command in a vb scirpt.
    > (Newish to scripting)
    >
    > I want to get the IP address, MasK and Gateway inputs and add them to the
    > shell command as in the below command.
    >
    > CODE
    >
    > Sub Manual_IP
    > ipAnswer = window.prompt("Please enter your IP address")
    > maskAnswer = window.prompt("Please enter the mask")
    > gatewayAnswer = window.prompt("Please enter the gateway IP address")
    > strIPHost=ipAnswer
    > strmaskHost=strAnswer
    > strgateway=gatewayAnswer
    > Set WShell = CreateObject("WScript.Shell")
    > WShell.exec("netsh interface ip set address local static strIPHost
    > strmaskHost strgateway 1")
    >
    > end sub
    >
    > My question is can this be done, or am I missing thre syntax for putting the
    > variables into the command.
    >
    > any suggestions?
    >
    > Thanks
    > D
    >
    > --
    > Dee


    As shown, the problem with the Exec statement is that the command is a
    string literal. It needs to be constructed as a mixture of literal
    strings and the contents of your string variables, something like
    this ...

    WShell.exec "netsh interface ip set address local static " _
    & strIPHost & " " & strmaskHost & " " & strgateway & " 1"

    Personally, I would use the Run method in place of the Exec, as you
    can avoid the command console window flashing on the screen. Since
    you are not trying to actively control underlying application, it
    seems like a little cleaner approach ...

    WShell.Run "netsh interface ip set address local static " _
    & strIPHost & " " & strmaskHost & " " & strgateway & " 1", 0, True

    Tom Lavedas
    ===========
    http://members.cox.net/tglbatch/wsh/

  4. #4
    Dee Guest

    Re: How to add a variable to a shell command in a vbscript

    Thats great, many thanks for your help

    D
    --
    Dee


    "Tom Lavedas" wrote:

    > On Apr 28, 10:52 am, Dee <D...@discussions.microsoft.com> wrote:
    > > Hello
    > >
    > > I'm trying to add a variable to a shell command in a vb scirpt.
    > > (Newish to scripting)
    > >
    > > I want to get the IP address, MasK and Gateway inputs and add them to the
    > > shell command as in the below command.
    > >
    > > CODE
    > >
    > > Sub Manual_IP
    > > ipAnswer = window.prompt("Please enter your IP address")
    > > maskAnswer = window.prompt("Please enter the mask")
    > > gatewayAnswer = window.prompt("Please enter the gateway IP address")
    > > strIPHost=ipAnswer
    > > strmaskHost=strAnswer
    > > strgateway=gatewayAnswer
    > > Set WShell = CreateObject("WScript.Shell")
    > > WShell.exec("netsh interface ip set address local static strIPHost
    > > strmaskHost strgateway 1")
    > >
    > > end sub
    > >
    > > My question is can this be done, or am I missing thre syntax for putting the
    > > variables into the command.
    > >
    > > any suggestions?
    > >
    > > Thanks
    > > D
    > >
    > > --
    > > Dee

    >
    > As shown, the problem with the Exec statement is that the command is a
    > string literal. It needs to be constructed as a mixture of literal
    > strings and the contents of your string variables, something like
    > this ...
    >
    > WShell.exec "netsh interface ip set address local static " _
    > & strIPHost & " " & strmaskHost & " " & strgateway & " 1"
    >
    > Personally, I would use the Run method in place of the Exec, as you
    > can avoid the command console window flashing on the screen. Since
    > you are not trying to actively control underlying application, it
    > seems like a little cleaner approach ...
    >
    > WShell.Run "netsh interface ip set address local static " _
    > & strIPHost & " " & strmaskHost & " " & strgateway & " 1", 0, True
    >
    > Tom Lavedas
    > ===========
    > http://members.cox.net/tglbatch/wsh/
    >


  5. #5
    Dee Guest

    Re: How to add a variable to a shell command in a vbscript

    Many thanks
    D
    --
    Dee


    "Pegasus (MVP)" wrote:

    >
    > "Dee" <Dee@discussions.microsoft.com> wrote in message
    > news:95294E76-9BDA-4E5C-A235-A15BE9AFE8A6@microsoft.com...
    > > Hello
    > >
    > > I'm trying to add a variable to a shell command in a vb scirpt.
    > > (Newish to scripting)
    > >
    > >
    > > I want to get the IP address, MasK and Gateway inputs and add them to the
    > > shell command as in the below command.
    > >
    > > CODE
    > >
    > >
    > > Sub Manual_IP
    > > ipAnswer = window.prompt("Please enter your IP address")
    > > maskAnswer = window.prompt("Please enter the mask")
    > > gatewayAnswer = window.prompt("Please enter the gateway IP address")
    > > strIPHost=ipAnswer
    > > strmaskHost=strAnswer
    > > strgateway=gatewayAnswer
    > > Set WShell = CreateObject("WScript.Shell")
    > > WShell.exec("netsh interface ip set address local static strIPHost
    > > strmaskHost strgateway 1")
    > >
    > > end sub
    > >
    > >
    > > My question is can this be done, or am I missing thre syntax for putting
    > > the
    > > variables into the command.
    > >
    > > any suggestions?
    > >
    > > Thanks
    > > D
    > >
    > > --
    > > Dee

    >
    > In VB Script you can obtain user input like so:
    >
    > strIP = InputBox("Please enter your IP address")
    >
    > The user's input is now stored in strIP. Might be a good
    > idea to download script56.chm for the basic stuff.
    >
    >
    >


Similar Threads

  1. Change shell variable on Linux mint
    By Athreya in forum Operating Systems
    Replies: 3
    Last Post: 11-01-2011, 07:55 PM
  2. Retrieve the result of a DOS command in a variable
    By Kieran in forum Operating Systems
    Replies: 3
    Last Post: 30-01-2010, 06:53 AM
  3. Affected shell variable in awk
    By John Wilson in forum Software Development
    Replies: 5
    Last Post: 23-12-2009, 02:04 PM
  4. How to suppress command prompt in vbscript
    By vivekmohan in forum Software Development
    Replies: 2
    Last Post: 14-07-2009, 10:30 AM
  5. Replies: 1
    Last Post: 18-05-2007, 01:24 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,403,532.08661 seconds with 17 queries