- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试创建一个脚本,该脚本将使用 powershell 和 invoke-webrequest 自动升级 NSX,以利用 NSX Manager 的 API 调用。我对脚本很满意,但脚本的某些部分我需要检查并匹配响应中的某些数据。事实上,NSX API 响应的 90% 都是 xml 格式,但有时也可以是 json。如果响应以 json 格式出现,我的匹配将不起作用,并且脚本的某些部分也将不起作用,因为它需要 xml 并且无法将 json 转换为 xml。
据我了解,从 powershell v4 开始,您可以添加接受 header ,但问题是我已经在使用一个 header 进行授权。
是否可以在 powershell 中的 invoke-webrequest 中添加多个 header ,如果可以,会怎样。
下面是我拥有的脚本的一部分,它将检查已部署的 NSX 的当前版本。因此它将匹配响应中的一个值,该值将显示版本。
#Variables to be used within the script.
[CmdletBinding()]
$NSXUsername = "admin"
$NSXPassword = "VMware1!"
$uriP = "https://HQ-NSX-01a.nsx.gss"
# Start time.
$startclock = (Get-Date)
Write-Host -BackgroundColor:Black -ForegroundColor:Green "Hello"
Write-Host -BackgroundColor:Black -ForegroundColor:Green "This script will help you to automate a full NSX environment deployment"
Write-Host -BackgroundColor:Black -ForegroundColor:Green "FULL NSX Tier Deployment for Single-VC is starting, This Deployment proccess will take an average of 60 min
========================================================================================
"
# Create NSX authorization string and store in $head and used in API Calls
# $nsxcreds = New-Object System.Management.Automation.PSCredential $NSXUsername,$NSXPassword
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($NSXUsername + ":" + $NSXPassword))
$head = @{"Authorization"="Basic $auth"}
# Allow untrusted SSL certs else will error out
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;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
################
#Start of Script
################
###########################################
# Checking the current deployed NSX Version
###########################################
Write-Host -BackgroundColor:Black -ForegroundColor:Green " Checking the current deployed version of the NSX
"
$r = Invoke-WebRequest -Uri "$uriP/api/1.0/appliance-management/global/info" -Method:Get -Headers $head -ContentType "application/xml" -ErrorAction:Stop -TimeoutSec 60
$nsxbn = ([xml]$r.Content).globalInfo.versionInfo.buildNumber
if ($nsxbn -match "2691049") {$nsxcv = "6.1.4"}
elseif ($nsxbn -match "3102213") {$nsxcv = "6.1.5"}
elseif ($nsxbn -match "3615148") {$nsxcv = "6.1.6"}
elseif ($nsxbn -match "3949567") {$nsxcv = "6.1.7"}
elseif ($nsxbn -match "2986609") {$nsxcv = "6.2.0"}
elseif ($nsxbn -match "3300239") {$nsxcv = "6.2.1"}
elseif ($nsxbn -match "3496286") {$nsxcv = "6.2.1a"}
elseif ($nsxbn -match "3604087") {$nsxcv = "6.2.2"}
elseif ($nsxbn -match "3638734") {$nsxcv = "6.2.2a"}
elseif ($nsxbn -match "3755950") {$nsxcv = "6.2.2b"}
elseif ($nsxbn -match "3979471") {$nsxcv = "6.2.3"}
elseif ($nsxbn -match "4167369") {$nsxcv = "6.2.3a"}
elseif ($nsxbn -match "4287432") {$nsxcv = "6.2.3b"}
elseif ($nsxbn -match "4292526") {$nsxcv = "6.2.4"}
elseif ($nsxbn -match "4818372") {$nsxcv = "6.2.5"}
elseif ($nsxbn -match "4977495") {$nsxcv = "6.2.6"}
elseif ($nsxbn -match "5007049") {$nsxcv = "6.3.0"}
elseif ($nsxbn -match "5124716") {$nsxcv = "6.3.1"}
else {
Write-host -BackgroundColor:Black -ForegroundColor:Red " Unable to retrieve the NSX version, This is either due to the current version is unknown to this script or the NSX Manager is powered off. Please check and try again."
exit
}
Write-Host -BackgroundColor:Black -ForegroundColor:Green " Current version of NSX deployed and to be upgraded is $nsxcv
"
如果是 xml 格式的响应如下,我将匹配版本。
<?xml version="1.0" encoding="UTF-8"?>
<globalInfo>
<currentLoggedInUser>admin</currentLoggedInUser>
<versionInfo>
<majorVersion>6</majorVersion>
<minorVersion>1</minorVersion>
<patchVersion>5</patchVersion>
<buildNumber>3102213</buildNumber> <<<--- this is my match
</versionInfo>
</globalInfo>
如果响应是 xml 格式(90% 的时间),上面的脚本就可以正常工作,但如果是 json,我将得到以下错误并且脚本将退出。
Cannot convert value "{"currentLoggedInUser":"admin","versionInfo":{"majorVersion":"6","minorVersion":"1","patchVersion":"5","buildNumber":"3102213"}}"
to type "System.Xml.XmlDocument". Error: "The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong
type."
At C:\Users\Administrator\Desktop\Scripts Folder\NSX-Auto-Upgrade-Single.ps1:68 char:2
+ $nsxbn = ([xml]$r.Content).globalInfo.versionInfo.buildNumber
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastToXmlDocument
非常感谢您的帮助。
谢谢。
最佳答案
我了解到您打算通过 powershell 向 REST API 调用添加多个 header 。而不是像这样添加标题,
$head = @{"Authorization"="Basic $auth"}
Invoke-WebRequest -Uri "$uriP/api/1.0/appliance-management/global/info" -Method:Get -Headers $head -ContentType "application/xml" -ErrorAction:Stop -TimeoutSec 60
你可以这样添加它们
Invoke-WebRequest -Uri "$uriP/api/1.0/appliance-management/global/info" -Method:Get -Headers @{"Authorization"="Basic $auth"; "Accept"="application/xml"} -ContentType "application/xml" -ErrorAction:Stop -TimeoutSec 60
关于xml - PowerShell-V5 Invoke-Webrequest 添加 2 个 header 授权 header 和接受 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42683385/
我正在寻找实现 PowerShell 提供程序 在 电源外壳。 我一直在想,如果我只是定义类型,然后将它们导入我的 session (导入模块),我应该能够让它们可用。 例如,这个 不工作但它沿着我想
我创建的脚本使用了组件,这些组件仅在32位版本的Powershell中可用。 默认情况下,Windows使用Powershell x64执行脚本,这会导致一些错误。 是一种在脚本开头设置值以强制Win
是否可以从 Powershell 中检测它是否是嵌套 shell? 如果我打开 Powershell 或 cmd.exe 窗口,然后输入 powershell 在那里,是否有一个神奇的 $host.s
随着 PowerShell Core 的发布,应用程序在使用托管自动化库 (system.management.automation) 时如何选择调用哪个版本的 Powershell(Powershe
最近,我加入了我企业的 Windows 团队,凭借我的开发人员背景(一般是 Java、.NET 和 Web),我很快就对 PowerShell 产生了兴趣。我可以看到它比普通的旧批处理文件、VB 更有
假设我有一个 powershell 脚本,它在我当前路径的相对路径中包含一个 Powershell 哈希。让我们称之为“name.ps1”,它包含: $names = @{ "bob" = "b
我想为我正在构建的自定义 Powershell Commandlet 使用 SqlServerCmdletSnapin。如果我将以下代码添加到 PSM1 的开头: if ( (Get-PSSnapin
如何调用从 PowerShell 脚本中获取命名参数的 PowerShell 脚本? foo.ps1: param( [Parameter(Mandatory=$true)][String]$a=''
我即将为 Windows 管理员编写一个 PowerShell 脚本,以帮助他们完成与部署 Web 应用程序相关的某些任务。 有什么理由让我应该赞成或排除开发 PowerShell 模块 (.psm1
我的 powershell 模块有一个函数,我希望它返回一个非零退出代码。但是,作为一个模块函数,当我运行 Import-Module 时,它会加载到 powershell 控制台的上下文中。所以,当
我在这个问题上花了最后 4 个小时,非常感谢您提供的任何意见。 我需要使用不同的凭据调用 powershell 脚本并将参数传递给该脚本。 安装 WISEScript 中包装的程序后,此脚本开始收集机
我有一个场景,我需要将 powershell 命令的命令和输出转发到另一个进程以进行日志记录和处理。 我希望这个控制台尽可能接近 powershell,因此不希望将它简单地托管在另一个控制台程序中。
我正在尝试让一个主 PowerShell 脚本运行所有其他脚本,同时等待 30-60 秒以确保完成任务。我尝试过的所有其他操作都不会停止/等待第一个脚本及其进程完成,然后才能同时完成所有其他脚本,并且
我正在编写一个脚本来使用多个 plink (PuTTY) session 作为 Windows 版本的 clustersh。然而,我陷入困境,因为我想从 powershell 打开多个 Powersh
我读了这个答案:How to Open Powershell from Powershell start powershell 这将打开基础的大分辨率 PS 实例。如何打开 PS(x86)? 最佳答案
我很想知道我们是否可以在 Powershell 中做到这一点。 使用 Out-File 命令,我们可以通过管道将其输出写入文件。这样我就可以将我所有的历史命令发送到一个文本文件中。 问题是我可以在每次
我在 about_Pipelines 阅读了有关 PowerShell 中的管道工作原理的信息,并了解到管道一次传送一个对象。 所以,这个 Get-Service | Format-Table -Pr
我正在尝试像这样从 powershell 启动一个进程:- $proc = (start-process $myExe -argumentList '/myArg True' -windowStyle
## To run the script # .\get_status.ps1 -Hostname -Service_Action -Service_Name #$Hostname = "hos
让我们使用 powershell 命令 Write-Host "red text"-Fore red这会在红色前景中显示“红色文本”。 但是,您希望文本以稍微亮一点的方式显示字体颜色,浅红色。 有没有
我是一名优秀的程序员,十分优秀!