How to send notification mail via powershell

百般思念 提交于 2020-06-17 09:44:39

问题


I want to send notification mail after vm deploy like below.

My question is :

1 - If DOMAIN field 'Y' then TRUE else will WORKGROUP

2- IF BACKUP IP field 'N' then it will "not defined"

3- Lastly , I have $diskSizes variable. A VM have one or more disks. $diskSizes[0 -> Hard disk 2] , $diskSizes[1 -> Hard disk 3] and so on.

My script:

$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 .....

}

My CSV File:

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

My desired notification mail :

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

回答1:


The tricky part is where you get the disk sizes in GB from Read-Host.
I would probably maximize the number of disks that can be added and ensure the given value the user typed in is in fact an int.

Something like this:

# 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
}


UPDATE

As per your comment you do not want to maximize the number of disks added beforehand, you can change the code to simply keep track of the disks added and afterwards 'fill in the blanks' so you will end up with objects having the same number of properties.

$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

After this you can send the report via email, either as plain text or HTML table, exactly the same as in your previous question




回答2:


I think you're asking how to output $Report as a string for the -body parameter.

You can do so like this: $body = ($Report | Out-String)



来源:https://stackoverflow.com/questions/61857643/how-to-send-notification-mail-via-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!