2016-05-23 54 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

感謝您的意見。仍然有問題,但這可能是我未來可以使用的東西!正如我在主線評論中提到的,我想我會找到另一種方法來實現這一點。 –