VMware Cloud Community
virtualdive
VMware Employee
VMware Employee
Jump to solution

PowerCLI output coming two times

Hello,

I am using the script below to generate the datastore report. This script was provided by the person called 'vlife' (thanks to him).

The problem I am facing is, the html output generated by this script is having two duplicate rows for same thing. Not sure where is it msising. See below, would appreciate if someone can help to edit this script.

DatastoreCapacityGBUsedGBFreeGBPercFree
Std_App187818628.35152.42
Std_App187818628.35152.42
Wyn_Standard_120001765.08234.6712
Wyn_Standard_120001765.08234.6712

======================================================================

$fileloc = 'c:\errors.htm'
$body = "Datastore Info“
$mailfrom = "xxx"
$to = "xxx"
$subject = "This message is sent by PowerCLI script"
$smtpserver = 'xxx'
Add-PSsnapin VMware.VimAutomation.Core
Connect-VIServer -Server 'vcenter' -User 'xxx' -Password 'xxx'

function UsedSpace
{
param($ds)
[math]::Round(($ds.CapacityMB - $ds.FreeSpaceMB)/1024,2)
}

function FreeSpace
{
param($ds)
[math]::Round($ds.FreeSpaceMB/1024,2)
}

function PercFree
{
param($ds)
[math]::Round((100 * $ds.FreeSpaceMB / $ds.CapacityMB),0)
}

$Datastores = Get-datastore -name "Datastore1", "Datastore2"
$myCol = @()
ForEach ($Datastore in $Datastores)
{
$myObj = "" | Select-Object Datastore, UsedGB, FreeGB, PercFree
$myObj.Datastore = $Datastore.Name
$myObj.UsedGB = UsedSpace $Datastore
$myObj.FreeGB = FreeSpace $Datastore
$myObj.PercFree = PercFree $Datastore
$myCol += $myObj
}

$a = "<style>"
$a = $a + "BODY{background-color:#CCFFFF;}"
$a = $a + "TABLE{border-width: 20px;border-style: ridge;border-bottom-color:#3399FF;border-right-color:#3399FF;border-top-color:#336633;border-left-color:#336633;border-collapse: collapse;}"
$a = $a + "TH{border-width: 3px;padding: 5px;border-style: ridge;border-color: black;background-color:#99FFFF}"
$a = $a + "TD{border-width: 3px;padding: 3px;border-style: ridge;border-color: black;background-color:#CCFFFF;text-align:center;}"
$a = $a + "</style>"

$myCol | Sort-Object PercFree |
ConvertTo-HTML -head $a -body "<H2>Datastore Usage</H2>" |
Out-File $fileloc

$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($fileloc)
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$msg.From = $mailfrom
foreach($mailTo in $to){
$msg.To.Add($mailto)
}
$msg.Subject = $subject
$msg.Body = $body
$msg.Attachments.Add($att)
$smtp.send($msg)
$att.Dispose()
$msg.Dispose()
Start-Sleep -s 5
if (Test-Path $fileloc){Remove-Item -Path $fileloc -confirm:$false}

Regards,

'V'
thevshish.blogspot.in
vExpert-2014-2021
Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, virtualdive-

I wonder -- do you happen to have an existing connection to the given vCenter (or given VMHosts) before you run the script?  As you know, that script you are using makes a connection to vCenter on its own, in addition to any existing connections in your PowerCLI session.  That would return more than one object for each datastore, as the Get-Datastore call would do that against all connected VI servers (vCenters, VMHosts).  You can check by checking the value of the auto-populated variable $global:DefaultVIServers.  Just type that variable name at the PowerShell prompt to see its current value.

Is it already populated with connection info (are you arleady connected to a VI server)?

View solution in original post

Reply
0 Kudos
2 Replies
mattboren
Expert
Expert
Jump to solution

Hello, virtualdive-

I wonder -- do you happen to have an existing connection to the given vCenter (or given VMHosts) before you run the script?  As you know, that script you are using makes a connection to vCenter on its own, in addition to any existing connections in your PowerCLI session.  That would return more than one object for each datastore, as the Get-Datastore call would do that against all connected VI servers (vCenters, VMHosts).  You can check by checking the value of the auto-populated variable $global:DefaultVIServers.  Just type that variable name at the PowerShell prompt to see its current value.

Is it already populated with connection info (are you arleady connected to a VI server)?

Reply
0 Kudos
virtualdive
VMware Employee
VMware Employee
Jump to solution

thanks Matt,

full marks! that was it... cheers!

Regards,

'V'
thevshish.blogspot.in
vExpert-2014-2021
Reply
0 Kudos