Ask a questionAsk a question
 

AnswerWhat am I doing wrong?

  • Friday, October 30, 2009 7:37 PMRedi311 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I'm trying to use powershell to read an excel spreadsheet with 5 columns and build a netsh command to add reserved ip addresses.

    I've got the code working where it builds the command string the way I think it should, but it never adds the reservation.

    I've tinkered with Invoke-Expression and some thing where it says I can enter command parsing mode with &...

    Neither works.  I've pasted code in below.


    function add-dhcpReservation
    {
     param([string]$dhcpServer, [string]$device, [string]$scope, [string]$ipAddress, [string]$macAddress, [string]$description)
     $netshCmd = [string]"netsh dhcp server "
     $reservedIpCmd = [string]" add reservedip "
     $device = $device + ".stormontvail.org"
     $addReservationCmd = $netshCmd + $dhcpServer + " scope " + $scope + $reservedIpCmd + $ipAddress + " " + $macAddress + " " + $device + " " + "`"$description`""
     Write-Host $addReservationCmd
     Invoke-Expression $addReservationCmd
     #& "netsh dhcp server $dhcpServer scope $scope add reservedip $ipAddress $macAddress $device"
    }

    $dhcpServer = [string]"10.101.2.2"
    $excelFile = [string]"D:\RDATA\Reservations.xlsx"

    $excel = New-Object -comobject Excel.Application
    $excel.Visible = $True
    $workBooks = $excel.workbooks
    $workBook = $workBooks.Open($excelFile)
    $sheets = $workBook.worksheets
    $activeSheet = $sheets.item("Reservations")
    Write-Host "Excel Rows found: " $activeSheet.UsedRange.Rows.Count
    for ($count = [int]2;$count -lt $activeSheet.UsedRange.Rows.Count +1; $count++)
    {
     $deviceRange = $activeSheet.Range("A$count")
     $subnetRange = $activeSheet.Range("B$count")
     $ipRange = $activeSheet.Range("C$count")
     $macRange = $activeSheet.Range("D$count")
     $descriptionRange = $activeSheet.Range("E$count")
     Write-Host $deviceRange.value2 $subnetRange.value2 $ipRange.value2 $ipRange.value2 $macRange.value2 $descriptionRange.value2
     add-dhcpReservation $dhcpServer $deviceRange.value2.Trim() $subnetRange.value2.Trim() $ipRange.value2.Trim() $macRange.value2.Trim() $descriptionRange.value2.Trim()
    }
    Write-Host "Finished Loading Reservations in the specified workbook!"

    The output is below:

    Excel Rows found: 2
    Test1 10.100.0.0 10.100.224.224 10.100.224.224 0000aa123456 Test Description
    netsh dhcp server 10.101.2.2 scope 10.100.0.0 add reservedip 10.100.224.224 0000aa123456 Test1.stormontvail.org "Test Description"
    Finished Loading Reservations in the specified workbook!


    I can see the command window pop up for just a second, then it disappears and i get no reservation.  Entering the netsh command that my script echoes back to me works just fine from either a command window or in a powershell window, so I'm stumped as to how to get invoke-expression or anything else to really execute my command string.

    Can someone help me figure out what I'm doing wrong?

    Thanks! 

    - Redi

Answers

  • Wednesday, November 04, 2009 6:28 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Redi,

    Please create a PS script to run the following commands:

    $netsh="netsh dhcp server 10.101.2.2 scope 10.100.0.0 add reservedip 10.100.224.224 0000aa123456 Test1.stormontvail.org 'Test Description'"
    Invoke-Expression $netsh


    If the error doesn’t occur, it may be caused by the "`"$description`"" in:

    $addReservationCmd = $netshCmd + $dhcpServer + " scope " + $scope + $reservedIpCmd + $ipAddress + " " + $macAddress + " " + $device + " " + "`"$description`""

    Try to remove `" or replace it with `’ to test.

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.

All Replies

  • Friday, October 30, 2009 9:49 PMMarco ShawMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sorry, I haven't tried the full solution.  I'm pretty sure you don't need to use the invokation operator if you're using invoke-expression.

    This works, for example:
    PS>$ipconfig="ipconfig"
    PS>invoke-expression $ipconfig
  • Monday, November 02, 2009 6:35 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Redi,

    The problem may not be caused by the script itself. What’s the version of your OS? If it’s Vista or later and UAC is enabled, please run the script as Administrator to test.

    Also, please try to run the script on the DHCP server to test.

    To narrow down the cause, as Marco suggested, replace "netsh dhcp server" with "ipconfig" to check if "Invoke-Expression $addReservationCmd" works.

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Monday, November 02, 2009 3:50 PMRedi311 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    It doesn't work on vista or on xp.

    If I copy and past the string that is contained in $addReservationCmd into either a powershell window or a regular cmd.exe window - it works fine as the account running the script.

    I added in > C:\temp\test.txt after the invoke-expression $addReservationCmd see what it was returning to the window and it says this in the .txt file:

    The following command was not found: dhcp server 10.101.2.2 scope 10.100.0.0 add reservedip 10.100.224.224 0000aa123456 Test1.stormontvail.org "Test Description".

    Why isn't it putting netsh at the front of the command string? 

    When i made a new variable called $ipconfig and ran invoke-expression $ipconfig > C:\temp\text.txt  - it has the ip information in the txt file.

    So for some reason, when I call invoke-expression, it doesn't seem to process the string correctly. 

    It picks up everything after the first space, but not the original netsh command, or something to that extent.

    Replacing the full netsh command line with ipconfig or ipconfig /all returns as expected.

    So it looks like Invoke-Expression doesn't like hte command line.

  • Tuesday, November 03, 2009 3:10 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Redi,

    Please note that Windows XP or Vista or any other non-DHCP server system, has not "dhcp server" context. So the "Netsh DHCP server scope" is invalid on these machines.

    Your script only works on DHCP server.

    Thanks.
    Mervyn
    TechNet Subscriber Support in forum
    If you have any feedback on our support, please contact tngfb@microsoft.com 

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Tuesday, November 03, 2009 2:50 PMRedi311 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Redi,

    Please note that Windows XP or Vista or any other non-DHCP server system, has not "dhcp server" context. So the "Netsh DHCP server scope" is invalid on these machines.

    Your script only works on DHCP server.

    Thanks.
    Mervyn
    TechNet Subscriber Support in forum
    If you have any feedback on our support, please contact tngfb@microsoft.com 

    This posting is provided "AS IS" with no warranties, and confers no rights.

    This statement is incorrect though.  This doesn't work on the dhcp server either.

    I do have the dhcp server context on my client in netsh(and dhcp server), and if I copy and paste the command string my script builds into my powershell window, it correctly creates the reservation as expected.  If I ask Invoke-Expression to run the command, or if I invoke & to try to run it - that's where it fails. 

    I have RSAT installed on my x64 Vista Business SP2 system and typing the command manually works as expected.  If I let the script echo out all the commands it builds, and then paste them into either powershell.exe window, or into cmd.exe window, the reservation is created as expected.  For some reason though powershell will not run the command correctly.

    I think it has something to do with the context menu in netsh, as these aren't the normal /parameter type parameters that lots of the other utilities (such as ipconfig) use.

    It seems to be a pretty trivial thing to pass a command line and expect it to run.  The syntax is correct, but powershell does not parse it correctly.

  • Tuesday, November 03, 2009 8:35 PMmjolinor Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
  • Wednesday, November 04, 2009 6:28 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Redi,

    Please create a PS script to run the following commands:

    $netsh="netsh dhcp server 10.101.2.2 scope 10.100.0.0 add reservedip 10.100.224.224 0000aa123456 Test1.stormontvail.org 'Test Description'"
    Invoke-Expression $netsh


    If the error doesn’t occur, it may be caused by the "`"$description`"" in:

    $addReservationCmd = $netshCmd + $dhcpServer + " scope " + $scope + $reservedIpCmd + $ipAddress + " " + $macAddress + " " + $device + " " + "`"$description`""

    Try to remove `" or replace it with `’ to test.

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Thursday, November 05, 2009 9:26 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Is the suggestion helpful? If not, please let us know more information for research. Any update is welcomed.

    Thanks.

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Thursday, November 05, 2009 4:05 PMRedi311 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Now I'm getting more trying to run the command.

    I used

    $netsh="netsh dhcp server 10.101.2.2 scope 10.100.0.0 add reservedip 10.100.224.224 0000aa123456 Test1.stormontvail.org 'Test Description'"
    Invoke-Expression $netsh

    and got

    The following helper DLL cannot be loaded: DHCPMON.DLL.
    The following helper DLL cannot be loaded: DHCPMONITOR.DLL.
    The following command was not found: dhcp server 10.101.2.2 scope 10.100.0.0 add reservedip 10.100.224.224 0000aa123456 Test1.stormontvail.org "Test Description".

    I'm executing this script from inside primalscript 2009.  It does this if I execute it normally, or use the "execute elevated" button.

    I copied and pasted the line from above directly into powershell and it worked as expected.  I think I'm having an issue when executing in primalscript at this point.

    I'm going to do some more testing just calling the script from powershell and see if it works any better outside the scripting IDE im using.  I'll let you know what happens.  May have to post this in their support forum as well if it turns out to be the case.

  • Thursday, November 05, 2009 4:21 PMRedi311 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It works as originally posted on my system!

    It does not work when executed from within my primalscript 2009 scripting IDE.

    I'm going to contact their support for help on resolving that issue.

    The good news is that the script works as expected when calling normally (I.E. powershell -noexit "pathtoscripthere")

    Its populating and running invoke-expression as expected.

    Never encountered this before, very interesting.


    Just for reference, this system is a Windows Vista x64 Business with SP2 and RSAT installed.  I'm going to load up powershell 2.0 today and get to crackin on the new stuff.

    Thank you all for your help, I was beating myself over the head as it looked like it should have been working to me!

    -Redi
  • Friday, November 06, 2009 5:21 AMMervyn ZhangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Redi,

    Thank you for update and clarify the situation. If you have more questions in the future, you’re welcomed to this forum.

    Regards

    This posting is provided "AS IS" with no warranties, and confers no rights.