Error Handling with Powershell

Home Forums Scripting PowerShell Error Handling with Powershell

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    Dumber
    Member
    #143242

    Ok,
    First time I use powershell…
    I’ve to create about 1200 DHCP scopes on 2 servers.

    So far so good, I created a pretty simple script which reads from a csv file and dumps it into DHCP using the netsh command..

    Code:
    import-csv c:DHCP.csv | foreach-object {

    $scopename = $_.DHCP_Names
    $scope = $_.Networkrange
    $mask = $_.Mask
    $gateway = $_.Gateway
    $lowResStart = $_.Res1_start
    $lowResEnd = $_.Res1_stop
    $DHCP1_start = $_.DHCP1_start
    $DHCP1_stop = $_.DHCP1_stop
    $DHCP2_start = $_.DHCP2_start
    $DHCP2_stop = $_.DHCP2_stop
    $highResStart = $_.Network_start
    $highResStop = $_.network_stop
    $leasetime = $_.Leasetime
    $hname = $env:computername
    $currentServer = “”

    # find out hostname where script is running.
    # this is to decide what scope should be excluded.

    $hname = $env:computername
    $currentserver = “”
    if ($hname -eq “WINDOWS2008DHCP”) {$currentserver = “DHCP1”}
    If ($hname -eq “SERVER1”) {$currentserver = “DHCP2”}

    # Add DHCP scope on the running server
    netsh dhcp server add scope $scope $mask $scopename # servername needs to be added…. still to come..

    # add the IP range

    if ($lowResStart -ne “” -and $highResStop -ne “”){
    netsh dhcp server scope $scope add iprange $lowResStart $highResStop # ff verder nakijken hoe dit te verwerken in Excel.
    }
    else
    {
    if ($currentserver -eq “DHCP1”){
    netsh dhcp server scope $scope add iprange $DHCP1_start $DHCP1_stop
    }
    if ($currentserver -eq “DHCP2”){
    netsh dhcp server scope $scope add iprange $DHCP2_start $DHCP2_stop
    }
    }

    # 3. Connect to (DHCP-01 MyScope), and add IP exclusion range

    if ($LowResStart -ne “” -AND $LowResEnd -ne “” ) {

    netsh dhcp server scope $scope add excluderange $LowResStart $lowResEnd
    }
    if ($highResStart -ne “” -AND $highResStop -ne “” ) {
    netsh dhcp server scope $scope add excluderange $highResStart $highResStop
    }

    # When current server equals to the first DHCP server (where the scrupt is executed) then set the exclusions for DHCP2
    if ($currentserver -eq “DHCP1”){
    if ($DHCP2_start -ne “” -OR $DHCP2_stop -ne “”){
    netsh dhcp server scope $scope add excluderange $DHCP2_start $DHCP2_stop
    }
    }

    # When current server quals to the second DHCP server (where the scrupt is executed) then set the exclusions for DHCP1
    if ($currentserver -eq “DHCP2”){
    if ($DHCP1_start -ne “” -OR $DHCP1_stop -ne “”){
    netsh dhcp server scope $scope add excluderange DHCP1_start $DHCP1_stop
    }
    }

    # 4 Add default Gateway for that scope
    netsh dhcp server scope $scope set optionvalue 003 IPADDRESS $gateway

    # 5 Add lease time…
    netsh dhcp server scope $scope set optionvalue 51 DWORD $leasetime

    }
    [/CODE]

    However is there a way to check if each command succesfully?
    Sure I can simply run script > textfile.txt or something but I’m more looking for an elegant way.

    so what I actually wanting to do is something like:

    [CODE]
    $compare = netsh dhcp server scope $scope set optionvalue 51 DWORD $leasetime
    If ($compare.EndsWith(“successfully.”)) {
    write-host “Successfull….”
    }
    else {
    write-host “NOT Successfull…. starting some log function”
    }

    [/CODE]

    For some reason sometimes it works and sometimes it doesnt.. It especially works when it gives me the else statement… :S

    If it’s true then it actually give me this error message.
    [COLOR=”Red”]Method invocation failed because [System.Object[]] doesn’t contain a method named ‘EndsWith’.[/COLOR]

    Edit:

    this test script simply works, so it has something to do with the results i guess…

    [code]
    $compare = “Changed the current scope context to 192.168.21.0 scope. Command completed successfully.”

    #write-host $compare

    If ($compare.EndsWith(“successfully.”)) {
    write-host “boe”
    }
    else
    {
    write-host “boeboe”
    }

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.