Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Interesting bug (?) using PowerShell, Netsh and Longhorn

Closed Thread
 
Thread Tools Display Modes
Old 05-31-2007   #1 (permalink)
Thomas Lee
Guest


 

Interesting bug (?) using PowerShell, Netsh and Longhorn


Came across a fascinating issue today, trying to write some ps1 scripts
to set IP addresses within my testing/demo Longhorn VMs. With 2k3/XP I
just use netshell scripts. Then I hit a weird issue - some netsh
commands do not seem to work from within a powershell script. Let me
demonstrate with some snippets:

Here is the test-bug.ps1 script:

----
# test-bug.ps1 - test of weird longhorn issue
# set dynamic IP to start
"Setting dynamic IP - In 10.0.0.0/24 DHCP RANGE"

netsh interface ip set address name= "LAN" Source=DHCP
netsh interface ip set dns name= "LAN" Source=DHCP
ipconfig

"now setting static IP"
netsh interface ip set address name="LAN" source=static
addr=192.168.1.100 mask=255.255.255.0
netsh interface ip set address name="LAN" gateway=192.168.1.254
gwmetric=0
netsh interface ip set dns name="LAN" source=static
addr=192.168.1.100 register=Primary
ipconfig
---

When I run this, from within Powershell, The DHCP bit works, but the
static doesn't. Here's the output from my system:

-----
PS C:\FOO> .\test-bug.ps1
Setting dynamic IP

Windows IP Configuration
Ethernet adapter lan:
Connection-specific DNS Suffix . : kapoho.net
Link-local IPv6 Address . . . . . : fe80::f0bd:6169:2767:a71f%10
IPv4 Address. . . . . . . . . . . : 10.10.1.54
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.10.1.100

now setting static IP

Windows IP Configuration
Ethernet adapter lan:

Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::f0bd:6169:2767:a71f%10
Default Gateway . . . . . . . . . : 192.168.1.254
------

Note that the IP address and gateway are missing. If I wait a few
seconds, and re-run the IPConfig here's what I see:

Windows IP Configuration
Ethernet adapter lan:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::f0bd:6169:2767:a71f%10
Autoconfiguration IPv4 Address. . : 169.254.167.31
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : 192.168.1.254
---------

WEIRD!

But weirder - if I cut/paste the two lines from the batch file into the
PowerShell window directly, they work fine.

Am I still in a jet-lag triangle??

Finally - who's going to Teched Next week?
--
Thomas Lee
doctordns@gmail.com
MVP - Admin Frameworks and Security
Old 05-31-2007   #2 (permalink)
RichS
Guest


 

RE: Interesting bug (?) using PowerShell, Netsh and Longhorn

Just a thought. Have you tried netsh from a cmd prompt? and what results
does it give?
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Thomas Lee" wrote:

>
> Came across a fascinating issue today, trying to write some ps1 scripts
> to set IP addresses within my testing/demo Longhorn VMs. With 2k3/XP I
> just use netshell scripts. Then I hit a weird issue - some netsh
> commands do not seem to work from within a powershell script. Let me
> demonstrate with some snippets:
>
> Here is the test-bug.ps1 script:
>
> ----
> # test-bug.ps1 - test of weird longhorn issue
> # set dynamic IP to start
> "Setting dynamic IP - In 10.0.0.0/24 DHCP RANGE"
>
> netsh interface ip set address name= "LAN" Source=DHCP
> netsh interface ip set dns name= "LAN" Source=DHCP
> ipconfig
>
> "now setting static IP"
> netsh interface ip set address name="LAN" source=static
> addr=192.168.1.100 mask=255.255.255.0
> netsh interface ip set address name="LAN" gateway=192.168.1.254
> gwmetric=0
> netsh interface ip set dns name="LAN" source=static
> addr=192.168.1.100 register=Primary
> ipconfig
> ---
>
> When I run this, from within Powershell, The DHCP bit works, but the
> static doesn't. Here's the output from my system:
>
> -----
> PS C:\FOO> .\test-bug.ps1
> Setting dynamic IP
>
> Windows IP Configuration
> Ethernet adapter lan:
> Connection-specific DNS Suffix . : kapoho.net
> Link-local IPv6 Address . . . . . : fe80::f0bd:6169:2767:a71f%10
> IPv4 Address. . . . . . . . . . . : 10.10.1.54
> Subnet Mask . . . . . . . . . . . : 255.255.255.0
> Default Gateway . . . . . . . . . : 10.10.1.100
>
> now setting static IP
>
> Windows IP Configuration
> Ethernet adapter lan:
>
> Connection-specific DNS Suffix . :
> Link-local IPv6 Address . . . . . : fe80::f0bd:6169:2767:a71f%10
> Default Gateway . . . . . . . . . : 192.168.1.254
> ------
>
> Note that the IP address and gateway are missing. If I wait a few
> seconds, and re-run the IPConfig here's what I see:
>
> Windows IP Configuration
> Ethernet adapter lan:
> Connection-specific DNS Suffix . :
> Link-local IPv6 Address . . . . . : fe80::f0bd:6169:2767:a71f%10
> Autoconfiguration IPv4 Address. . : 169.254.167.31
> Subnet Mask . . . . . . . . . . . : 255.255.0.0
> Default Gateway . . . . . . . . . : 192.168.1.254
> ---------
>
> WEIRD!
>
> But weirder - if I cut/paste the two lines from the batch file into the
> PowerShell window directly, they work fine.
>
> Am I still in a jet-lag triangle??
>
> Finally - who's going to Teched Next week?
> --
> Thomas Lee
> doctordns@gmail.com
> MVP - Admin Frameworks and Security
>

Old 05-31-2007   #3 (permalink)
Thomas Lee
Guest


 

Re: Interesting bug (?) using PowerShell, Netsh and Longhorn

In message <FA956BE4-DEDD-43C1-8237-DAE6018829D8@microsoft.com>, RichS
<RichS@discussions.microsoft.com> writes
>Just a thought. Have you tried netsh from a cmd prompt? and what
>results does it give?


Good question.

It fails in the same way.

:-(

--
Thomas Lee
doctordns@gmail.com
MVP - Admin Frameworks and Security
Old 05-31-2007   #4 (permalink)
Oisin Grehan
Guest


 

Re: Interesting bug (?) using PowerShell, Netsh and Longhorn

On May 31, 9:14 am, Thomas Lee <t...@psp.co.uk> wrote:
> In message <FA956BE4-DEDD-43C1-8237-DAE601882...@microsoft.com>, RichS
> <R...@discussions.microsoft.com> writes
>
> >Just a thought. Have you tried netsh from a cmd prompt? and what
> >results does it give?

>
> Good question.
>
> It fails in the same way.
>
> :-(
>
> --
> Thomas Lee
> doctor...@gmail.com
> MVP - Admin Frameworks and Security


This particular text here:

>Note that the IP address and gateway are missing. If I wait a few
>seconds, and re-run the IPConfig here's what I see:


and this bit here:

> Autoconfiguration IPv4 Address. . : 169.254.167.31


Tell me that your attempt to set the source as static is failing. It's
still using DHCP to get an address. The "autoconfiguration" text and
the non-routable address you've been assigned should alert you to
this.

I find the best way to get correct NETSH scripts is to use interactive
mode (e.g. just run netsh<enter> and use imperative commands to set
everything) and then just run "dump <filename>" to get the right
script, once you've verified the state of the network.

Hope this helps,

- Oisin

Old 05-31-2007   #5 (permalink)
Thomas Lee
Guest


 

Re: Interesting bug (?) using PowerShell, Netsh and Longhorn

In message <1180620401.518788.312140@p77g2000hsh.googlegroups.com>,
Oisin Grehan <oising@gmail.com> writes
>On May 31, 9:14 am, Thomas Lee <t...@psp.co.uk> wrote:
>> In message <FA956BE4-DEDD-43C1-8237-DAE601882...@microsoft.com>, RichS
>> <R...@discussions.microsoft.com> writes
>>
>> >Just a thought. Have you tried netsh from a cmd prompt? and what
>> >results does it give?

>>
>> Good question.
>>
>> It fails in the same way.
>>
>> :-(
>>
>> --
>> Thomas Lee
>> doctor...@gmail.com
>> MVP - Admin Frameworks and Security

>
>This particular text here:
>
>>Note that the IP address and gateway are missing. If I wait a few
>>seconds, and re-run the IPConfig here's what I see:

>
>and this bit here:
>
>> Autoconfiguration IPv4 Address. . : 169.254.167.31


Indeed.

The two lines in the netsh script fail to 'work' when run from a batch
or powershell script, but work interactively.

>Tell me that your attempt to set the source as static is failing.


Correct.

> It's
>still using DHCP to get an address. The "autoconfiguration" text and
>the non-routable address you've been assigned should alert you to
>this.


Yes - I know about DHCP.

But I don't know why a netsh script to set an address when run form a
scrip, yet it runs from the commandline.

>I find the best way to get correct NETSH scripts is to use interactive
>mode (e.g. just run netsh<enter> and use imperative commands to set
>everything) and then just run "dump <filename>" to get the right
>script, once you've verified the state of the network.


That rather defeats the idea.

I have a set of VMs that I generally want to run with static IP
addresses to simulate a private network. But from time to time I want to
use DHCP so I can access the interent and get stuf (e.g. ms updates)
into the VM. The batch script works fine on XP and 2003, but is failing
in Longhorn.
--
Thomas Lee
doctordns@gmail.com
MVP - Admin Frameworks and Security
Old 05-31-2007   #6 (permalink)
Oisin Grehan
Guest


 

Re: Interesting bug (?) using PowerShell, Netsh and Longhorn

On May 31, 11:59 am, Thomas Lee <t...@psp.co.uk> wrote:
> In message <1180620401.518788.312...@p77g2000hsh.googlegroups.com>,
> Oisin Grehan <ois...@gmail.com> writes
>
>
>
>
>
> >On May 31, 9:14 am, Thomas Lee <t...@psp.co.uk> wrote:
> >> In message <FA956BE4-DEDD-43C1-8237-DAE601882...@microsoft.com>, RichS
> >> <R...@discussions.microsoft.com> writes

>
> >> >Just a thought. Have you tried netsh from a cmd prompt? and what
> >> >results does it give?

>
> >> Good question.

>
> >> It fails in the same way.

>
> >> :-(

>
> >> --
> >> Thomas Lee
> >> doctor...@gmail.com
> >> MVP - Admin Frameworks and Security

>
> >This particular text here:

>
> >>Note that the IP address and gateway are missing. If I wait a few
> >>seconds, and re-run the IPConfig here's what I see:

>
> >and this bit here:

>
> >> Autoconfiguration IPv4 Address. . : 169.254.167.31

>
> Indeed.
>
> The two lines in the netsh script fail to 'work' when run from a batch
> or powershell script, but work interactively.
>
> >Tell me that your attempt to set the source as static is failing.

>
> Correct.
>
> > It's
> >still using DHCP to get an address. The "autoconfiguration" text and
> >the non-routable address you've been assigned should alert you to
> >this.

>
> Yes - I know about DHCP.
>
> But I don't know why a netsh script to set an address when run form a
> scrip, yet it runs from the commandline.
>
> >I find the best way to get correct NETSH scripts is to use interactive
> >mode (e.g. just run netsh<enter> and use imperative commands to set
> >everything) and then just run "dump <filename>" to get the right
> >script, once you've verified the state of the network.

>
> That rather defeats the idea.
>
> I have a set of VMs that I generally want to run with static IP
> addresses to simulate a private network. But from time to time I want to
> use DHCP so I can access the interent and get stuf (e.g. ms updates)
> into the VM. The batch script works fine on XP and 2003, but is failing
> in Longhorn.
> --
> Thomas Lee
> doctor...@gmail.com
> MVP - Admin Frameworks and Security- Hide quoted text -
>
> - Show quoted text -


Ok, it wasn't really clear that it "works on XP and 2003, and fails on
longhorn" from your initial post. The implication was that a plain
netsh script works on xp/2003, but a powershell script doesn't work;
the fact it was on longhorn appeared incidental.

Anyway, the conclusion (which i'm sure you've come to) is that if both
the batch and powershell versions fail only on longhorn, then it's a
problem with netsh on longhorn, most likely with how it's interfacing
at a system level. Have you tried adding a "sleep 3000" command after
the initial attempt to switch your "LAN" i/f into static
configuration? Perhaps it's threading/race condition of some sort...
also, read back the source property after the wait to verify. This
kind of weirdness always descends into voodoo anway, so try wringing
the necks of few ceremonial tech-chickens...

Yours somewhat redundantly,

- Oisin

Old 06-04-2007   #7 (permalink)
Thomas Lee
Guest


 

Re: Interesting bug (?) using PowerShell, Netsh and Longhorn

In message <1180641474.509791.219940@g4g2000hsf.googlegroups.com>, Oisin
Grehan <oising@gmail.com> writes
>Ok, it wasn't really clear that it "works on XP and 2003, and fails on
>longhorn" from your initial post.


Sorry for the lack of clarity!

>The implication was that a plain netsh script works on xp/2003, but a
>powershell script doesn't work; the fact it was on longhorn appeared
>incidental.


Running netsh commands seems to fail when using Longhorn. I first tried
it as a powershell script.


>Anyway, the conclusion (which i'm sure you've come to) is that if both
>the batch and powershell versions fail only on longhorn, then it's a
>problem with netsh on longhorn, most likely with how it's interfacing
>at a system level.


Yes - that seems to be the case. I'll try to file a bug.


> Have you tried adding a "sleep 3000" command after the initial attempt
>to switch your "LAN" i/f into static configuration?


I have not tried this, but it should not be necessary.

> Perhaps it's threading/race condition of some sort... also, read back
>the source property after the wait to verify. This kind of weirdness
>always descends into voodoo anway, so try wringing the necks of few
>ceremonial tech-chickens...


Good idea - will try later when I get a moment.

Thomas
--
Thomas Lee
doctordns@gmail.com
MVP - Admin Frameworks and Security
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Will Longhorn Server be shipped with Powershell 2.0 ? Yann PowerShell 2 01-15-2008 08:14 AM
interesting code crashes powershell klumsy@xtra.co.nz PowerShell 3 04-15-2007 04:32 PM
powershell and Longhorn admin PowerShell 1 04-03-2007 12:15 PM
Powershell on Longhorn Server CTP Feb 2007 Joop Idema PowerShell 1 02-22-2007 08:49 AM
Powershell for Longhorn IDX Build 6001 rfalken PowerShell 0 02-14-2007 03:08 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50