方法调用失败,因为 [System.String] 不包含名为 'SelectNodes' 的方法

Method invocation failed because [System.String] does not contain a method named 'SelectNodes'

我有一个脚本,我正在尝试 运行 针对 Azure 订阅。该脚本获取已发布的配置文件设置,例如用户名、密码和 url 到 FTP 该目录下的文件。我直接从 Microsoft 文档中获取了语法,该文档概述了具体的操作方法。请参阅此 link 以了解将 xml 与空输出文件一起使用:Msft.Doc

不幸的是,我收到一条错误消息:

Method invocation failed because [System.String] does not contain a method named 'SelectNodes'.

我不确定为什么会这样。谢谢!

$appdirectory="<app directory>"
$webappname="<webapp name>" ##"mywebapp$(Get-Random)"
$location="East US"
$ResourceGroupName="<resource group name>"

# Get publishing profile for the web app
$xml = (Get-AzureRmWebAppPublishingProfile -Name $webappname `
-ResourceGroupName $ResourceGroupName `
-OutputFile null)

# Extracts connection information from publishing profile
$username = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userName").value
$password = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userPWD").value
$url = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@publishUrl").value


#ftp test 2
$request = [Net.WebRequest]::Create("$url")
$request.Credentials = New-Object System.Net.NetworkCredential("$username", "$password")
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile 

# make sure to create the path or change the URL to file. 
$fileStream = [System.IO.File]::OpenRead("C:\tmp\test.txt")
$ftpStream = $request.GetRequestStream()

$buffer = New-Object Byte[] 10240
while (($read = $fileStream.Read($buffer, 0, $buffer.Length)) -gt 0)
{
    $ftpStream.Write($buffer, 0, $read)
    $pct = ($fileStream.Position / $fileStream.Length)
    Write-Progress `
        -Activity "Uploading" -Status ("{0:P0} complete:" -f $pct) `
        -PercentComplete ($pct * 100)
}

$fileStream.CopyTo($ftpStream)

$ftpStream.Dispose()
$fileStream.Dispose()

我知道有一个类似的问题,但它略有不同,因为我正在按照 Microsoft 文档给出的示例进行操作,其中输出文件在我的案例中为空。

我找不到官方文档来支持这一点,但答案是 。错误是正确的。您正在处理 xml 这样的字符串。如果该字符串确实是 xml 格式的字符串,您需要首先将该字符串显式转换为 xml 。

$xml = [xml](Get-AzureRmWebAppPublishingProfile -Name $webappname `
-ResourceGroupName $ResourceGroupName `
-OutputFile null)

注意: 大多数使用该 cmdlet 的示例都使用真实文件名,例如test.xml。但是,在这种情况下,null 可能是字符串 null,因此它可能有效。根据 docs.microsoft.com

-outputfile 无论如何都被列为可选