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
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.
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/
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/
>
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.
>
>
>