Invoke-WebRequest:无法验证参数“Uri”上的参数 Brocade 交换机 FOS REST API

问题描述 投票:0回答:1
$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

我正在输入brocade交换机的用户名和密码。 ssh 使用相同的凭据。

基本上我正在尝试运行 brocade 命令。我正在使用 Powershell Invoke-WebRequest 调用 api。我是第一次使用 powershell 这样做。

我收到以下错误:

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
powershell rest automation storage san
1个回答
0
投票

我找出了这段代码中的问题。问题出在 https 上。 https 未启用。要在 Brocade 交换机上启用 https,我按照本文档中给出的步骤进行操作:chrome-extension://efaidnbmnnnibpcajpcglclefindmkaj/https://docs.broadcom.com/doc/FOS-82x-AG

先决条件:要使用推荐的 HTTPS 协议,必须在交换机上安装有效的安全证书,并且必须在开始 REST 操作之前在交换机上启用 HTTPS 协议。有关这两项操作的说明,请参阅《Brocade Fabric OS 管理指南》。

© www.soinside.com 2019 - 2024. All rights reserved.