0
我試圖將字符串成員添加到一個名爲$htmlbodytest
的數組中,然後我可以在腳本的末尾發送電子郵件(在所有循環關閉後)。Powershell:爲什麼某個字符串數組被格式化爲「System.Int32」?
爲什麼會收到以下錯誤消息時,我嘗試填充用繩子成員(也是唯一的字符串成員)?:數組
Cannot convert value "test server" to type "System.Int32". Error: "Input string was not in a correct format."
- 我在劇本其他幾個交錯數組(內循環)存儲字符串沒有問題。
- 我還沒有宣佈任何關於數據類型的任何地方。
- 如果我在電子郵件部分的循環後面添加一個測試數組成員,只是「測試」它仍然失敗。
- 如果我把它複製到它自己的腳本中,我可以使用字符串填充數組。
更多細節:
# before loops begin, array is declared as:
$htmlbodytest = @()
[...]
foreach ($server_name in $server_names) {
Start-Job -Scriptblock {
#If server fails a ping test, skip it and add to array to email
if(!(Test-Connection -ComputerName $using:server_name -Quiet)) {
$htmlbodytest += "$using:server_name"
continue
} else {
[...]
} # after work of loop is finished, send email
$smtpServer = "10.10.10.10"
$smtpFrom = "[email protected]"
$smtpTo = "[email protected]"
$messageSubject = "Servers that didn't record uptime this month due to no ping"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = $htmlbodytest | ForEach-Object {Add-Member -InputObject $_ -Type NoteProperty -Name Servers -Value $_; $_} | ConvertTo-Html -Head $a -Property Server
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
我很難找到一個問題,而不是那些對我說的很多。如果你改變了聲明,它會改變什麼嗎? '[string []] $ htmlbodytest = @()'。我覺得答案可能在缺少的代碼中。有沒有其他地方可以嘗試填充未顯示的$ htmlbodytest? – Matt 2014-10-16 16:40:18
我關閉了ISE並重新打開並更改了變量名,現在它正在工作。我在一個單獨的標籤中聲明瞭$ htmlbodytest,這是我沒有意識到的。對不起浪費你的時間,但感謝你的迴應。 – kiwisan 2014-10-16 17:09:57