如何通过powershell发送通知邮件
How to send notification mail via powershell
我想在 vm 部署后发送通知邮件,如下所示。
我的问题是:
1 - 如果 DOMAIN 字段 'Y' 则为 TRUE,否则将为 WORKGROUP
2- 如果备份 IP 字段 'N' 那么它将 "not defined"
3- 最后,我有 $diskSizes 变量。 VM 有一个或多个磁盘。 $diskSizes[0 -> Hard disk 2] , $diskSizes[1 -> Hard disk 3] 等等。
我的脚本:
$VCServer = Read-Host "Enter the vCenter server name"
Import-Csv -Path C:\temp\vmdeploy.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
New-Vm .... blah blah
$diskSizes = @()
do {
$diskSize = Read-Host -Prompt "Additional disk (size in GB or 'no' to stop)"
if($diskSize -ne 'no'){
$diskSizes += $diskSize
}
}
until($diskSize -eq 'no')
if($diskSizes.Count -gt 0){
$diskSizes | %{
New-HardDisk -VM $row.ServerName -CapacityGB $_ | Out-Null
}
}
....
...
$Report = [PSCustomObject]@{
'VMName' = $row.ServerName
'OS' = $row.ServerName
'DOMAIN' = $row.DOMAIN
'LAN IP' = $row.LANIP
'BACKUP IP' = $row.BACKUPIP
'VMState' = (Get-VM -Name $row.ServerName).summary.runtime.powerState
'TotalCPU' = $row.NumCPU
'Totalmemory' = $row.MemoryGB
'vCenter' = $VCServer
'VMHost' = $row.ESXHOST
}
Send-MailMessage .....
}
我的 CSV 文件:
ServerName ESXHOST Datastore OSCapacityGB NumCPU MemoryGB NetworkName Second Network Adapter LANIP LANGW BACKUPIP DOMAIN
TestVM01,192.168.30.10,LUNPRDVM01,50,4,16,PG_VLAN_250,Y,10.100.10.12,255.255.255.0,192.168.172.12,Y
TestVM02,192.168.30.11,LUNPRDVM02,60,6,24,PG_VLAN_250,N,10.100.10.13,255.255.255.0,N,N
我想要的通知邮件:
VMName OS DOMAIN LAN IP BACKUP IP VMState TotalCPU Totalmemory vCenter VMHost Hard disk 2 Hard disk 3
TestVM01 TestVM01 TRUE 10.100.10.12 192.168.172.12 PoweredON 4 16 192.168.100.10 192.168.30.10 50GB not defined
TestVM02 TestVM02 WORKGROUP 10.100.10.13 not defined PoweredON 6 24 192.168.100.10 192.168.30.11 60GB 500GB
我想你是在问如何将 $Report
作为 -body
参数的字符串输出。
您可以这样做:
$body = ($Report | Out-String)
棘手的部分是从 Read-Host 获取以 GB 为单位的磁盘大小。
我可能会最大化可以添加的磁盘数量,并确保用户输入的给定值实际上是一个整数。
像这样:
# set a maximum number of disks to add in this demo no more than 10
$maxDisks = 10
$VCServer = Read-Host "Enter the vCenter server name"
$report = Import-Csv -Path C:\temp\vmdeploy.csv -UseCulture -PipelineVariable row | ForEach-Object {
New-Vm .... blah blah
$newVM = [PSCustomObject]@{
'VMName' = $row.ServerName
'OS' = $row.ServerName
'DOMAIN' = if ($row.DOMAIN -eq 'Y') { 'TRUE' } else { 'WORKGROUP' }
'LAN IP' = $row.LANIP
'BACKUP IP' = if ($row.BACKUPIP -eq 'N') { 'not defined' } else { $row.BACKUPIP }
'VMState' = (Get-VM -Name $row.ServerName).summary.runtime.powerState
'TotalCPU' = $row.NumCPU
'Totalmemory' = $row.MemoryGB
'vCenter' = $VCServer
'VMHost' = $row.ESXHOST
}
# add the harddisk properties to the object, initialize to 'not defined'
for ($i = 1; $i -le $maxDisks; $i++) {
$newVM | Add-Member -MemberType NoteProperty -Name "Hard disk $i" -Value 'not defined'
}
# now ask for the disk sizes
$diskNumber = 1
$intSize = 0 # a variable to use in TryParse()
while ($diskNumber -le $maxDisks) {
$diskSize = Read-Host -Prompt "Additional disk (size in GB or 'no' to stop)"
if ($diskSize -eq 'no') { break } # exit the loop
if ([int]::TryParse($diskSize, [ref]$intSize)) {
New-HardDisk -VM $row.ServerName -CapacityGB $intSize -Confirm:$false | Out-Null
# update the value in the $newVM object
$diskItem = "Hard disk $diskNumber" # the property name in the object
$newVM.$diskItem = '{0}GB' -f $intSize
$diskNumber++ # increment the disk number
}
}
# output the completed object to be collected in the $report variable
$newVM
}
更新
根据您的评论,您不想最大化预先添加的磁盘数量,您可以更改代码以简单地跟踪添加的磁盘以及之后添加的磁盘 'fill in the blanks',这样您最终会得到具有以下内容的对象相同数量的属性。
$VCServer = Read-Host "Enter the vCenter server name"
# counter to keep track of the maximum number of disks added
$maxDisks = 0
$report = Import-Csv -Path C:\temp\vmdeploy.csv -UseCulture | ForEach-Object {
Write-Host "Creating new VM: $($_.ServerName)_" -ForegroundColor Yellow
New-Vm .... blah blah
$newVM = [PSCustomObject]@{
'VMName' = $_.ServerName
'OS' = $_.ServerName
'DOMAIN' = if ($_.DOMAIN -eq 'Y') { 'TRUE' } else { 'WORKGROUP' }
'LAN IP' = $_.LANIP
'BACKUP IP' = if ($_.BACKUPIP -eq 'N') { 'not defined' } else { $_.BACKUPIP }
'VMState' = (Get-VM -Name $_.ServerName).summary.runtime.powerState
'TotalCPU' = $_.NumCPU
'Totalmemory' = $_.MemoryGB
'vCenter' = $VCServer
'VMHost' = $_.ESXHOST
}
# ask for the disk sizes and create new disks
$diskNumber = 0
$intSize = 0 # a variable to use in TryParse()
while ($true) {
$diskSize = Read-Host -Prompt "Additional disk (size in GB or 'no' to stop)"
if ($diskSize -eq 'no') { break } # exit the loop
if ([int]::TryParse($diskSize, [ref]$intSize)) {
New-HardDisk -VM $_.ServerName -CapacityGB $intSize -Confirm:$false | Out-Null
# update the value in the $newVM object
$diskNumber++ # increment the disk number
$diskItem = "Hard disk $diskNumber" # the property name in the object
$newVM | Add-Member -MemberType NoteProperty -Name $diskItem -Value ('{0}GB' -f $intSize)
}
}
# update the $maxDisks variable
$maxDisks = [Math]::Max($diskNumber, $maxDisks)
# output the completed object to be collected in the $report variable
$newVM
}
# Your $report variable now holds a collection of VMs with a variable number of properties.
# To be able to create a decent table out of this, you need to add hardisk properties with value 'not defined' where needed.
foreach ($vm in $report) {
$diskCount = ($vm | Select-Object -Property 'Hard disk*').Count
for ($i = $diskCount; $i -lt $maxDisks; $i++) {
$vm | Add-Member -MemberType NoteProperty -Name "Hard disk $($i + 1)" -Value 'not defined'
}
}
# output on screen
$report
之后您可以通过电子邮件发送报告,可以是纯文本或 HTML table,与您的
完全相同
我想在 vm 部署后发送通知邮件,如下所示。
我的问题是:
1 - 如果 DOMAIN 字段 'Y' 则为 TRUE,否则将为 WORKGROUP
2- 如果备份 IP 字段 'N' 那么它将 "not defined"
3- 最后,我有 $diskSizes 变量。 VM 有一个或多个磁盘。 $diskSizes[0 -> Hard disk 2] , $diskSizes[1 -> Hard disk 3] 等等。
我的脚本:
$VCServer = Read-Host "Enter the vCenter server name"
Import-Csv -Path C:\temp\vmdeploy.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
New-Vm .... blah blah
$diskSizes = @()
do {
$diskSize = Read-Host -Prompt "Additional disk (size in GB or 'no' to stop)"
if($diskSize -ne 'no'){
$diskSizes += $diskSize
}
}
until($diskSize -eq 'no')
if($diskSizes.Count -gt 0){
$diskSizes | %{
New-HardDisk -VM $row.ServerName -CapacityGB $_ | Out-Null
}
}
....
...
$Report = [PSCustomObject]@{
'VMName' = $row.ServerName
'OS' = $row.ServerName
'DOMAIN' = $row.DOMAIN
'LAN IP' = $row.LANIP
'BACKUP IP' = $row.BACKUPIP
'VMState' = (Get-VM -Name $row.ServerName).summary.runtime.powerState
'TotalCPU' = $row.NumCPU
'Totalmemory' = $row.MemoryGB
'vCenter' = $VCServer
'VMHost' = $row.ESXHOST
}
Send-MailMessage .....
}
我的 CSV 文件:
ServerName ESXHOST Datastore OSCapacityGB NumCPU MemoryGB NetworkName Second Network Adapter LANIP LANGW BACKUPIP DOMAIN
TestVM01,192.168.30.10,LUNPRDVM01,50,4,16,PG_VLAN_250,Y,10.100.10.12,255.255.255.0,192.168.172.12,Y
TestVM02,192.168.30.11,LUNPRDVM02,60,6,24,PG_VLAN_250,N,10.100.10.13,255.255.255.0,N,N
我想要的通知邮件:
VMName OS DOMAIN LAN IP BACKUP IP VMState TotalCPU Totalmemory vCenter VMHost Hard disk 2 Hard disk 3
TestVM01 TestVM01 TRUE 10.100.10.12 192.168.172.12 PoweredON 4 16 192.168.100.10 192.168.30.10 50GB not defined
TestVM02 TestVM02 WORKGROUP 10.100.10.13 not defined PoweredON 6 24 192.168.100.10 192.168.30.11 60GB 500GB
我想你是在问如何将 $Report
作为 -body
参数的字符串输出。
您可以这样做:
$body = ($Report | Out-String)
棘手的部分是从 Read-Host 获取以 GB 为单位的磁盘大小。
我可能会最大化可以添加的磁盘数量,并确保用户输入的给定值实际上是一个整数。
像这样:
# set a maximum number of disks to add in this demo no more than 10
$maxDisks = 10
$VCServer = Read-Host "Enter the vCenter server name"
$report = Import-Csv -Path C:\temp\vmdeploy.csv -UseCulture -PipelineVariable row | ForEach-Object {
New-Vm .... blah blah
$newVM = [PSCustomObject]@{
'VMName' = $row.ServerName
'OS' = $row.ServerName
'DOMAIN' = if ($row.DOMAIN -eq 'Y') { 'TRUE' } else { 'WORKGROUP' }
'LAN IP' = $row.LANIP
'BACKUP IP' = if ($row.BACKUPIP -eq 'N') { 'not defined' } else { $row.BACKUPIP }
'VMState' = (Get-VM -Name $row.ServerName).summary.runtime.powerState
'TotalCPU' = $row.NumCPU
'Totalmemory' = $row.MemoryGB
'vCenter' = $VCServer
'VMHost' = $row.ESXHOST
}
# add the harddisk properties to the object, initialize to 'not defined'
for ($i = 1; $i -le $maxDisks; $i++) {
$newVM | Add-Member -MemberType NoteProperty -Name "Hard disk $i" -Value 'not defined'
}
# now ask for the disk sizes
$diskNumber = 1
$intSize = 0 # a variable to use in TryParse()
while ($diskNumber -le $maxDisks) {
$diskSize = Read-Host -Prompt "Additional disk (size in GB or 'no' to stop)"
if ($diskSize -eq 'no') { break } # exit the loop
if ([int]::TryParse($diskSize, [ref]$intSize)) {
New-HardDisk -VM $row.ServerName -CapacityGB $intSize -Confirm:$false | Out-Null
# update the value in the $newVM object
$diskItem = "Hard disk $diskNumber" # the property name in the object
$newVM.$diskItem = '{0}GB' -f $intSize
$diskNumber++ # increment the disk number
}
}
# output the completed object to be collected in the $report variable
$newVM
}
更新
根据您的评论,您不想最大化预先添加的磁盘数量,您可以更改代码以简单地跟踪添加的磁盘以及之后添加的磁盘 'fill in the blanks',这样您最终会得到具有以下内容的对象相同数量的属性。
$VCServer = Read-Host "Enter the vCenter server name"
# counter to keep track of the maximum number of disks added
$maxDisks = 0
$report = Import-Csv -Path C:\temp\vmdeploy.csv -UseCulture | ForEach-Object {
Write-Host "Creating new VM: $($_.ServerName)_" -ForegroundColor Yellow
New-Vm .... blah blah
$newVM = [PSCustomObject]@{
'VMName' = $_.ServerName
'OS' = $_.ServerName
'DOMAIN' = if ($_.DOMAIN -eq 'Y') { 'TRUE' } else { 'WORKGROUP' }
'LAN IP' = $_.LANIP
'BACKUP IP' = if ($_.BACKUPIP -eq 'N') { 'not defined' } else { $_.BACKUPIP }
'VMState' = (Get-VM -Name $_.ServerName).summary.runtime.powerState
'TotalCPU' = $_.NumCPU
'Totalmemory' = $_.MemoryGB
'vCenter' = $VCServer
'VMHost' = $_.ESXHOST
}
# ask for the disk sizes and create new disks
$diskNumber = 0
$intSize = 0 # a variable to use in TryParse()
while ($true) {
$diskSize = Read-Host -Prompt "Additional disk (size in GB or 'no' to stop)"
if ($diskSize -eq 'no') { break } # exit the loop
if ([int]::TryParse($diskSize, [ref]$intSize)) {
New-HardDisk -VM $_.ServerName -CapacityGB $intSize -Confirm:$false | Out-Null
# update the value in the $newVM object
$diskNumber++ # increment the disk number
$diskItem = "Hard disk $diskNumber" # the property name in the object
$newVM | Add-Member -MemberType NoteProperty -Name $diskItem -Value ('{0}GB' -f $intSize)
}
}
# update the $maxDisks variable
$maxDisks = [Math]::Max($diskNumber, $maxDisks)
# output the completed object to be collected in the $report variable
$newVM
}
# Your $report variable now holds a collection of VMs with a variable number of properties.
# To be able to create a decent table out of this, you need to add hardisk properties with value 'not defined' where needed.
foreach ($vm in $report) {
$diskCount = ($vm | Select-Object -Property 'Hard disk*').Count
for ($i = $diskCount; $i -lt $maxDisks; $i++) {
$vm | Add-Member -MemberType NoteProperty -Name "Hard disk $($i + 1)" -Value 'not defined'
}
}
# output on screen
$report
之后您可以通过电子邮件发送报告,可以是纯文本或 HTML table,与您的