$Auth = [System.Text.Encoding]::UTF8.GetBytes("<username>:<password>")
$Basic = [System.Convert]::ToBase64String($Auth)
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} ;
$Header = @{
'Accept' = 'application/yang-data+json'
'Content-Type' = 'application/yang-data+json'
'Authorization' = "Basic $Basic"
}
$InvokeParam = @{
'Uri' = 'https://10.xx.xx.xx/rest/login'
'Method' = 'POST'
'Headers' = $Header
}
$Response = Invoke-WebRequest @InvokeParam | ConvertTo-Json
$Authkey = $Response.headers.Authorization
I am entering the username and password of the brocade switch. The same credentials are used for ssh.
Basically I am trying to run a brocade command. I am calling the api using Powershell Invoke-WebRequest. I am doing this for the first time using powershell.
I am getting below error:
Invoke-WebRequest : Unable to connect to the remote server
At C:\Users\kashyupa\Documents\Untitled2.ps1:37 char:13
+ $Response = Invoke-WebRequest @InvokeParam | ConvertTo-Json
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [In
voke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.Invo
keWebRequestCommand
The variable '$Response' cannot be retrieved because it has not been set.
At C:\Users\kashyupa\Documents\Untitled2.ps1:40 char:12
+ $Authkey = $Response.headers.Authorization
+ ~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Response:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
sw0:FADA28:testuser> mgmtapp --show
REST Interface State: Enabled
REST Session Count: 3
REST Throttling Configurations:
Sample Requests : 30
Sample Time (in sec) : 30
Idle Time (in sec) : 3
KeepAlive : Disabled
KeepAliveTimeout : 15sec
I figured out the problem in this code. The issue is with https. https is not enabled. To enable https on brocade switch I followed the steps given on this document: chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://docs.broadcom.com/doc/FOS-82x-AG
Pre-Reqs: To use the recommended HTTPS protocol, a valid security certificate must be installed on the switch and the HTTPS protocol must be enabled on the switch before beginning REST operations. Refer to the Brocade Fabric OS Administration Guide for instructions on both these actions.