所以,这是我的问题...我有以下PowerShell脚本,我想用它来执行以下操作:
ForEach
循环,以获取所有启用了Hyper-V复制的VM。以下是代码:
$Cluster = Get-ClusterNode -Cluster hv-cluster01.contoso.com
$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{column-width: 200px;border-width: 1px;padding: 8px;border-style: solid;border-color: black;background-color:thistle}"
$a = $a + "TD{column-width: 200px;border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:PaleGoldenrod;text-align: center}"
$a = $a + "</style>"
ForEach ($Node in $Cluster)
{
Get-VMReplication -ComputerName $Node | Select-Object VMName, State, Health, PrimaryServer | ConvertTo-HTML -head $a -body "<H2>DR Replication Status</H2>" | Out-File C:\Scripts\Test.htm
}
我的最终目标是将其置于计划任务中,每天运行2-3次,并使用Send-MailMessage
将html内容发送到我自己和团队中的其他人身上。 。
我感谢任何帮助。
答案 0 :(得分:0)
在您的代码中,您为$ Cluster中的每个$ Node转换并输出对象。相反,您首先必须填充包含所有信息的最终对象,然后转换并输出这些信息。
您可以通过将转换和输出部分移动到循环外部来实现。例如:
$Cluster = Get-ClusterNode -Cluster hv-cluster01.contoso.com
$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{column-width: 200px;border-width: 1px;padding: 8px;border-style: solid;border-color: black;background-color:thistle}"
$a = $a + "TD{column-width: 200px;border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:PaleGoldenrod;text-align: center}"
$a = $a + "</style>"
$Report = ForEach ($Node in $Cluster) {
Get-VMReplication -ComputerName $Node | Select-Object VMName, State, Health, PrimaryServer
}
$Report | ConvertTo-HTML -head $a -body "<H2>DR Replication Status</H2>" | Out-File C:\Scripts\Test.htm
答案 1 :(得分:0)
我已经修改了相当多的脚本,以适应我想要做的其余事情......
我设法让脚本提取HTML文件的内容并将其放入电子邮件中。我的问题是,一些表格样式信息没有转移到电子邮件。具体来说,column-width
部分。因此,所有表条目都被挤压在一起。见下文:
Remove-Item C:\Scripts\ReplicationHealth.htm
$Cluster = Get-ClusterNode -Cluster hv-cluster01.contoso.com
$a = "<style>"
$a = $a + "BODY{background-color:LightGrey;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{column-width: 200px;border-width: 1px;padding: 8px;border-style: solid;border-color: black;background-color:DodgerBlue}"
$a = $a + "TD{column-width: 200px;border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:PapayaWhip;text-align: center}"
$a = $a + "</style>"
$Report = ForEach ($Node in $Cluster) {
Get-VMReplication -ComputerName $Node | Select-Object VMName, State, Health, PrimaryServer
}
$time = Get-Date -Format G
$Report | ConvertTo-HTML -head $a -body "<H2>Hyper-V Replication Status</H2>", $time | Out-File C:\Scripts\ReplicationHealth.htm
$MailBody = Get-Content C:\Scripts\ReplicationHealth.htm -Raw
Send-MailMessage -SmtpServer "SERVERIP" -From "HyperV-Replication@contoso.com" -To "User@contoso.com" -Subject "Hyper-V Replication Status" -Body $MailBody -BodyAsHtml
更多的帮助会很棒! 提前谢谢!