2016-05-23 60 views
-2

我发现了很多关于处理Invoke-RestMethod中的正斜杠的信息,但我似乎无法找到任何有关处理反斜杠的信息。我知道他们应该避免,但我有一些限制,在我无法改变信息的地方工作。这里就是我想要做的事:带有反斜杠的Invoke-RestMethod URL

Invoke-RestMethod "http://blah.blah.net/api/Reporting/$ServerName" -Method Get 

它可以在其他情况很好,但这次$ServerName会有像“Server1上\ SQLserv”的值。我尝试过使用$ServerName -replace "\", "\\\"和其他变体,以及用%5C(URL转义字符)代替反斜杠。似乎没有任何工作。任何人遇到(并解决!)这个?

编辑:这是我得到的具体错误。请注意,它做工精细的其他服务器,我查询反对,虽然说该服务已被删除:

Invoke-RestMethod : The resource you are looking for has been removed, had its 
name changed, or is temporarily unavailable. 
At line:1 char:1 
+ Invoke-RestMethod "http://blah.blah.net/api/Report ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException 
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
+1

看起来像一个服务器端的问题给我。此外,'Server1 \ SQLserv'不是真正的服务器名称。这是一个SQL Server实例的名称。你确定你的web应用程序实际上接受这个作为输入吗? –

+0

我同意,这似乎是一个服务器端问题。错,真的,是服务器的名称。是的,网络应用程序应该能够接受这个输入。我越是仔细研究和调查,我认为我应该找到另一种方法来实现这一目标。甚至可能会搞乱数据库条目,以避免必须使用反斜杠。谢谢你的帮助。 –

回答

1

-replace是一个正则表达式替换方法。你可以尝试使用字符串替换方法,它只会替换文字字符。现在

$ServerName.Replace('\','/') 
Invoke-RestMethod "http://blah.blah.net/api/Reporting/$ServerName" -Method Get 

,如果您还需要去掉“SQLserv”字符串,我这样做:

$Server = Split-Path -Path $ServerName -Parent 
Invoke-RestMethod "http://blah.blah.net/api/Reporting/$Server" -Method Get 
+0

伟大的信息,谢谢。我仍然有这个问题,但我想我会尝试一种不同的方法。谢谢您的意见! –

1

由于Ansgar mentioned,这可能是一个服务器错误。不过,我会建议使用EscapeUriString功能来逃避uri

$ServerName = "Server1\SQLserv" 
$myUri = [System.Uri]::EscapeUriString("http://blah.blah.net/api/Reporting/$ServerName") 
Invoke-RestMethod $myUri -Method Get 
+0

感谢您的意见。仍然有问题,但这可能是我未来可以使用的东西!正如我在主线评论中提到的,我想我会找到另一种方法来实现这一点。 –