2016-09-14 59 views
0

我在這裏使用Powershell - 字符串來格式化HTML主體,但獲取以下錯誤:格式化字符串時出錯:索引(從零開始)必須大於或等於零並小於參數列表的大小..用這裏字符串

Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.. 

的代碼如下:

$my_html = @" 
<html> 
<head></head> 
<body> 
<p>You have been invited to the following SharePoint site:</p><a href={0}>{1}</a> 
<p>If you are not sure what your username is, please find it at</p>' + '<a href="https://outside.arup.com/default.aspx">here</a> 
<p>Your username is {3}</p><p>Your password is: {4}</p> 
<p>Please note that for any support queries, please contact your Arup site owner or Global IT (Applications).</p> 
</body> 
</html> 
"@ -f $url, $siteTitle, $username, $password 

所有參數進行初始化,只是標準的字符串。我錯過了什麼?

感謝

+0

其實只是注意到了我的錯誤,參數佔位符應爲​​0,1,2,3! – dotnetdev

+0

如果這只是一個錯字,請提供自我回答或刪除問題:) –

回答

2

它看起來就像你在4個參數傳遞的-f PowerShell中操作的右側。在您的源字符串中,您的佔位符爲{0}{1}{3}{4}。您需要將{3}更改爲{2}並將{4}更改爲{3}

1

可以使用變量直接進入串塊:

 $my_html = @" 
     <html> 
     <head></head> 
     <body> 
     <p>You have been invited to the following SharePoint site:</p><a href=$url>$siteTitle</a> 
     <p>If you are not sure what your username is, please find it at</p>' + '<a href="https://outside.arup.com/default.aspx">here</a> 
     <p>Your username is $username</p><p>Your password is: $password</p> 
     <p>Please note that for any support queries, please contact your Arup site owner or Global IT (Applications).</p> 
     </body> 
     </html> 
"@ 
0

另一種情況可能發生:

$list = New-Object 'System.Collections.Generic.List[string]' 
$list.Add("{0} vs {1}" -f 123,456) # Same error, 456 would be taken as Add's 2nd argument 

$list.Add(("{0} vs {1}" -f 123,456)) # okay 
$list.Add("{0} vs {1}" -f @(123,456)) # okay 
相關問題