![](/img/trans.png)
[英]Insert random username to SharePoint 2013 column from array PowerShell script
[英]PowerShell Insert into array
我們正在創建一個大型腳本,需要生成一些HTML代碼。 這一切都很好,我已經在數組$HTMLcode
收集了我需要的所有信息。
我在數組/字符串操作方面還是有點菜鳥,所以我想知道是否有可能在以文本ERROR開頭的數組HTMLcode
中的每一行上方插入一行文本?
$HTMLcode=@()
$HTMLcode += "ERROR: Problem 1<br>"
$HTMLcode += "You did this wrong.. <br>"
$HTMLcode += "ERROR Problem 2<br>"
$HTMLcode += "Something happened here.. <br>"
$HTMLcode += "ERROR Porblem 3<br>"
$HTMLcode += "Did you try.. <br>"
期望的結果將是:
$HTMLcode=@()
$HTMLcode += "--------------------------<br>"
$HTMLcode += "ERROR: Problem 1<br>"
$HTMLcode += "You did this wrong.. <br>"
$HTMLcode += "--------------------------<br>"
$HTMLcode += "ERROR Problem 2<br>"
$HTMLcode += "Something happened here.. <br>"
$HTMLcode += "--------------------------<br>"
$HTMLcode += "ERROR Porblem 3<br>"
$HTMLcode += "Did you try.. <br>"
謝謝您的幫助。
除非您小心,否則如果管道中的操作(cmdlet)返回兩個對象,則它們都將作為單獨的對象放置在管道中。
例如。
2,4,6 | % { %_/2; $_ }
退貨
1 2 2 4 3 6
因此,您可以執行以下操作:
$updatedHtmlCode = $HTMLCode |
Select-Object { if ($_ -clike 'ERROR*') { '--------------------------<br>' } $_ }
終於找到了這個答案:
$Array = @"
computer1
computer2
computer2
"@ -split "`n" | % { $_.trim() }
訣竅是拆分和修剪。 這使其在管道中工作。 資料來源: http : //pleasework.robbievance.net/howto-easily-convert-block-of-text-into-an-array-in-powershell/
另一種可能性:
$htmlcode = @()
$HTMLcode += "ERROR: Problem 1<br>"
$HTMLcode += "You did this wrong.. <br>"
$HTMLcode += "ERROR Problem 2<br>"
$HTMLcode += "Something happened here.. <br>"
$HTMLcode += "ERROR Porblem 3<br>"
$HTMLcode += "Did you try.. <br>"
$htmlcode -replace '^(?=ERROR)',"--------------------------<br>`n"
--------------------------<br>
ERROR: Problem 1<br>
You did this wrong.. <br>
--------------------------<br>
ERROR Problem 2<br>
Something happened here.. <br>
--------------------------<br>
ERROR Porblem 3<br>
Did you try.. <br>
這不會在數組中添加任何其他元素,它只是將Error行替換為一個新字符串,該字符串包含在原始文本之前帶有嵌入式換行符的其他文本。
對於html,完成后可能會通過Out-String發送,並且最終結果應該與將新行插入數組一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.