gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:04:43 27 4
gpt4 key购买 nike

我有一个正在尝试针对 Azure 订阅运行的脚本。该脚本获取已发布的配置文件设置,例如用户名、密码和 FTP 文件到该目录的 url。我直接从 Microsoft 文档中获取了语法,该文档概述了如何执行此操作。请参阅此链接以了解使用空输出文件的 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 文档给出的示例,其中输出文件在我的情况下为空。

最佳答案

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

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

注意:使用该 cmdlet 的大多数示例都使用真实的文件名,例如测试.xml。然而,在本例中 null 可能是字符串 null,因此它可能有效。根据learn.microsoft.com-outputfile 无论如何都被列为可选。

关于powershell - 方法调用失败,因为 [System.String] 不包含名为 'SelectNodes' 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51026354/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com