【问题标题】:Method invocation failed because [System.String] does not contain a method named 'SelectNodes'方法调用失败,因为 [System.String] 不包含名为“SelectNodes”的方法
【发布时间】:2018-06-25 14:52:44
【问题描述】:

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

很遗憾,我收到一条错误消息:

方法调用失败,因为 [System.String] 不包含名为“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 文档给出的示例,其中输出文件在我的情况下为空。

【问题讨论】:

标签: powershell azure azure-powershell


【解决方案1】:

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

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

注意: 大多数使用该 cmdlet 的示例都使用真实文件名,例如测试.xml。但是 null 在这种情况下可能是字符串 null 所以它可能有效。根据docs.microsoft.com-outputfile 无论如何都被列为可选

【讨论】:

  • 感谢您的回答。您可以查看与此确切脚本相关的 Microsoft 文档;请注意,在他们的示例中,他们没有将 xml 显式转换为字符串,他们也将输出文件设置为 null。 Msft.Doc - 我对这个脚本如何工作的理解是输出文件为空,然后在脚本的提取部分中使用。
  • @DangeRuss 我会在您的问题中添加这一点,因为您从官方示例中提取了这个,这是一个重要的细节。可能意味着文档是错误的。 $xml 在您的情况下显然是一个字符串,并且大概文档中的字符串也是如此。
  • 谢谢!我将其添加到问题中。我会尝试添加一个输出文件,看看它是否能解决问题。
  • 我在最后一段中的意思是你也应该可以省略它。
  • 是的,我明白了,但我也会尝试明确说明 xml 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
  • 2013-07-03
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多