Developer

 View Only
Expand all | Collapse all

Set Vlan with Powershell

This thread has been viewed 7 times
simon.rudlstorfer

simon.rudlstorferOct 28, 2019 10:51 AM

  • 1.  Set Vlan with Powershell

    Posted Oct 28, 2019 10:10 AM

    Hi, 

     

    i am trying to find a way to set the vlan on a AOS switch with rest. 

     

    First of all the login on the switch with my rest method works fine and i get a loogin success 

    $Cookie = Invoke-RestMethod -Uri $loginUrl -Method POST -Body $payload_login
    $payload_login contains User and password. 

    0006:00:00:00.15 rest mrest_ctrl:Username =admin
    0006:00:00:00.20 rest mrest_ctrl:role =2
    0006:00:00:00.25 rest RestSrv-2:Method = POST, URI = /rest/v3/login-sessions
    0006:00:00:00.33 rest RestSrv-2:Login successful for user admin

     

     

    Now i´m facing a problem with the secound post. 

    When i try to post the new configuration i always get this message:

    rest mrest_ctrl: PARSE header Cookie value : (null)

     

    From my point of few there is a problem with the header in the post. 

     

    Can someone show me how to bind the values from the authentication cookie correkt to the header cookie? 

     

    I have tried it like this 

    $Headers = @{
    "name" = $Cookie.name
    "uri" = $Cookie.uri
    "value" = $Cookie.cookie
     
    }

    but without sucess. 

     

    Thank you
     

     

     



  • 2.  RE: Set Vlan with Powershell

    Posted Oct 28, 2019 10:51 AM


  • 3.  RE: Set Vlan with Powershell

    Posted Oct 28, 2019 10:52 AM

    my post look like this 

     

    {"vlan_id": $Vlan,"port_id": "$Switchports", "port_mode": "POM_UNTAGGED"}
    "@

    Invoke-RestMethod -Uri http://10.13.37.10/rest/v3/vlans-ports -Method POST -Headers $Headers -Body $body



  • 4.  RE: Set Vlan with Powershell

    Posted Oct 28, 2019 12:22 PM

    Hi Simon,

     

    the cookie is not the same name (need to be call Session-Id), you can look https://github.com/PowerAruba/PowerArubaSW/blob/master/PowerArubaSW/Public/Connection.ps1#L144

     

    But also i have already write a PowerShell Module for Aruba Switch (and configure vlan on Port), you can look -> https://community.arubanetworks.com/t5/Wired-Intelligent-Edge-Campus/PowerArubaSW-a-powershell-module-for-use-ArubaOS-Switch-REST-API/m-p/462976



  • 5.  RE: Set Vlan with Powershell

    Posted Oct 29, 2019 03:41 AM

    Hi, 

     

    thank you, 

     

    but can you explain this? 

    the cookie is not the same name (need to be call Session-Id), you can look https://github.com/PowerAruba/PowerArubaSW/blob/master/PowerArubaSW/Public/Connection.ps1#L144

     

    How should a change my code to fix the issue? 

    I already have seen your PS Modules, but i like to do this with onboard featuers.

     

    thank you 



  • 6.  RE: Set Vlan with Powershell

    Posted Oct 29, 2019 05:17 AM

    @simon.rudlstorfer wrote:

    Hi, 

     

    thank you, 

     

    but can you explain this? 

    the cookie is not the same name (need to be call Session-Id), you can look https://github.com/PowerAruba/PowerArubaSW/blob/master/PowerArubaSW/Public/Connection.ps1#L144

     

    How should a change my code to fix the issue? 

    I already have seen your PS Modules, but i like to do this with onboard featuers.

     

    thank you 


     

    Hi Simon

    Look https://support.hpe.com/hpsc/doc/public/display?docId=a00091319en_us

    No sure it is possible to set directly Cookie with -header on Invoke-RestMethod

     

    What switch version do you are using ? (the cookie is not set on the same method following release...)

     



  • 7.  RE: Set Vlan with Powershell

    Posted Oct 29, 2019 10:04 AM

    Hi, 

     

    I´m using YA_16_10_0001.swi



  • 8.  RE: Set Vlan with Powershell

    Posted Oct 29, 2019 10:38 AM

    My code work with 16.10.0001

     



  • 9.  RE: Set Vlan with Powershell

    Posted Oct 29, 2019 10:40 AM

    can you post your code? 



  • 10.  RE: Set Vlan with Powershell



  • 11.  RE: Set Vlan with Powershell

    Posted Oct 29, 2019 10:49 AM

    I´m looking for a way with the standard featueres. 

    Because I do not like to install the extansions on every client wich will use this script. 



  • 12.  RE: Set Vlan with Powershell

    Posted Oct 29, 2019 01:05 PM

     


    @simon.rudlstorfer wrote:

    I´m looking for a way with the standard featueres. 

    Because I do not like to install the extansions on every client wich will use this script. 


    sure ! no problem, you can look how to make the connection from my script...



  • 13.  RE: Set Vlan with Powershell

    Posted Nov 07, 2019 04:25 PM

    Hi Simon,

     

    it is working now ?

     

    A solution, if you don't want a external module, it is to merge all files on the module on one big files ;-)



  • 14.  RE: Set Vlan with Powershell

    Posted Nov 18, 2019 04:22 AM

    Sorry for the late reply. 

     

    I have a problem with the Set-Cookie Command.

     

    Is this a function which I can find in your powershell module? 

     

    I get a message like "function not found" 



  • 15.  RE: Set Vlan with Powershell



  • 16.  RE: Set Vlan with Powershell

    Posted Nov 18, 2019 06:59 AM

    Thank you, 

    but I´m still facing the same issue. 

     

    When i try to post the new port settings i get Invoke-RestMethod : 400 Bad RequestAccess is unauthorized.

     

    $invokeParams = @{DisableKeepAlive = $true}

    $response = Invoke-WebRequest -uri $loginUrl -Method POST -Body ($postParams ) -SessionVariable arubasw @invokeParams
     
    $connection = @{server = ""; session = ""; cookie = ""; invokeParams = ""; }

    $cookie = ($response.content | ConvertFrom-Json).cookie
    $smallcookie = $cookie.split("=")[1]
    $arubasw.Cookies.Add((Set-Cookie -name "sessionId" -value $smallcookie -domain $server));

    $connection.server = $server
    $connection.cookie = $cookie
    $connection.session = $arubasw

    $connection.invokeParams = $invokeParams
     

    Invoke-RestMethod -Uri http://10.13.37.10/rest/v4/vlans-ports -Method Post -Body $body -Headers $connection
     
    The Switch reports:
    0026:22:08:21.11 rest tHttpd:tHttpd is unblocked with necessary cleanup
    0026:22:08:21.18 rest mrest_ctrl:Request for parse header
    0026:22:08:21.24 rest mrest_ctrl:Request for parse header after method POST
    0026:22:08:21.32 rest mrest_ctrl:uri len = 20
    0026:22:08:21.37 rest mrest_ctrl: PARSE header Cookie value : (null)
    0026:22:08:21.45 rest mrest_ctrl:Validate operation: operation not allowed 0
    only system user exists
    0026:22:08:21.56 rest mrest_ctrl:bytes_pending 0, nsent 206
    0026:22:08:21.62 rest mrest_ctrl:Rest invalid Operation.
    0026:22:08:21.68 rest mrest_ctrl:Request validation failed
     
     
    any idea?


  • 17.  RE: Set Vlan with Powershell
    Best Answer

    Posted Nov 18, 2019 07:05 AM
    Invoke-RestMethod -Uri http://10.13.37.10/rest/v4/vlans-ports -Method Post -Body $body -Headers $connection

    It is no -headers must be passed by WebSession  ($connection.session)



  • 18.  RE: Set Vlan with Powershell

    Posted Nov 18, 2019 08:53 AM

    Thank you. 

    Now it works...

     

    Invoke-WebRequest -Uri http://10.13.37.10/rest/v4/vlans-ports -Method Post -Body $body -WebSession $connection.session


  • 19.  RE: Set Vlan with Powershell

    Posted Oct 28, 2019 12:53 PM

    @simon.rudlstorfer wrote:

    my post look like this 

     

    {"vlan_id": $Vlan,"port_id": "$Switchports", "port_mode": "POM_UNTAGGED"}
    "@

    Invoke-RestMethod -Uri http://10.13.37.10/rest/v3/vlans-ports -Method POST -Headers $Headers -Body $body


    avec PowerArubaSW, il suffit de faire

     

    Connect-ArubaSW X.X.X.X
    
    Get-ArubaSWVlansPorts -port_id X | Set_arubaSWVlansPorts -mode untagged -vlan_id Y