swamynaveen
Enthusiast
Enthusiast

PowerCLi script to clear space from Ramdisks root/tmp/var

Jump to solution

Hi Friends,

I am trying to  clear space under Ramdisks root,tmp & var by scheduling a powercli script through windows task scheduler so that it will check the Ramdisk utilization status and then clears the space for more than 2000 ESX hosts to ensure there is no blockers in the host connections/services and below is the script that i am trying to explore and any advise and help would be really appreciated.

 

####################

$vmhosts = get-vmhost 

foreach ($esx in $vmhosts){


$cmd_tmp = @'
rm /tmp/ams-*;
'@

$cmd_var = @'
find /var/log -mtime +1 | grep log.*.[0-9]|xargs rm;
'@

$secPswd = ConvertTo-SecureString 'XXXX' -AsPlainText -Force

$cred = New-Object System.Management.Automation.PSCredential ('root', $secPswd)

Get-VMHostService -VMHost $esx | where{$_.Key -eq 'TSM-SSH'} | Start-VMHostService -Confirm:$false | Out-Null

$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey

Invoke-SSHCommand -SSHSession $session -Command $cmd_tmp | Select -ExpandProperty Output

Invoke-SSHCommand -SSHSession $session -Command $cmd_var | Select -ExpandProperty Output

Remove-SSHSession -SSHSession $session | Out-Null

Get-VMHostService -VMHost $esx | where{$_.Key -eq 'TSM-SSH'} | Stop-VMHostService -Confirm:$false | Out-Null
}

############

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

This includes the vCenter and the cluster.

 

$date = Get-Date -UFormat "%A %m/%d/%Y %R %Z"

$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@

$cmd = @'
find $($_.MountPoint) -mtime +1 | xargs rm;
'@

$secPswd = ConvertTo-SecureString 'xxx' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('root', $secPswd)

$ramDisks = 'root','tmp','vsantraces'
$report = @()

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $esx.Name -V2
    $esxcli.system.visorfs.ramdisk.list.Invoke() | 
    where{$ramDisks -contains $_.RamdiskName -and $_.Free -lt 10} |
    ForEach-Object -Process {
        $obj = [ordered]@{
            vCenter = ([uri]$esx.ExtensionData.Client.ServiceUrl).Host
            Cluster = (Get-Cluster -VMHost $esx).Name
            VMHost = $esx.Name
            RAMDisk = $_.RamdiskName
            FreePre = $_.Free
        }
        Get-VMHostService -VMHost $esx | 
        where{$_.Key -eq 'TSM-SSH' -and -not $_.Running} | 
        Start-VMHostService -Confirm:$false | Out-Null
    
        $session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
        $cmd = $ExecutionContext.InvokeCommand.ExpandString($cmd)
        Invoke-SSHCommand -SSHSession $session -Command $cmd | Select -ExpandProperty Output
        Remove-SSHSession -SSHSession $session | Out-Null

        $newStatus = $esxcli.system.visorfs.ramdisk.list.Invoke() | where{$_.RamdiskName -eq $obj.RAMDisk}
        $obj.Add('FreePost',$newStatus.Free)
        $report += New-Object -TypeName PSObject -Property $obj
    }
    Get-VMHostService -VMHost $esx | 
    where{$_.Key -eq 'TSM-SSH' -and $_.Running} | 
    Stop-VMHostService -Confirm:$false | Out-Null
}

if($report.Count -ne 0){
    $body = $report | ConvertTo-Html -Head $Header | Out-String
}
else{
    $body = '<p>No RAM disks found that meet the criteria</p>'
}

$sMail = @{
    To         = "xxx"
    From       = "xxx"
    #Cc = 
    Subject    = "Ramdisk cleanup report on: $date "
    Body       = $body
    BodyAsHtml = $true
    Priority   = 'High'
    SmtpServer = "xxx"
}
Send-MailMessage 

 

To test with multiple passwords, have a look at Solved: Output for Password Validation - VMware Technology Network VMTN


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

30 Replies
LucD
Leadership
Leadership

Without having checked n detail, that script seems to be doing what you try to achieve.
What is missing or not working?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
swamynaveen
Enthusiast
Enthusiast

Actually, I would like to enable mail alert post completion of space clean up of RAM Disks for specific name only which are highlighted below. Do we have an where cause to fetch the Ramdisk utilization report for root,tmp & vsantraces? Pls advise.

 

swamynaveen_0-1607621905794.png

 

0 Kudos
LucD
Leadership
Leadership

To recap, you want to check a specific set of the RAM disks?
If they don't have enough free space, the script shall clean them up.
And you want to receive a single email with all the RAM disks that have been cleared?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
swamynaveen
Enthusiast
Enthusiast

@LucD Yep. That's the exact thing that i am looking for and also the final report should have all RAM disks which are utilization >90% and post clean up disk utilization % report as well so that we can have a windows task scheduler in place everyday post business hours.

 

Regards,

Naveen

0 Kudos
LucD
Leadership
Leadership

Try something like this

$date = Get-Date -UFormat "%A %m/%d/%Y %R %Z"

$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@

$cmd = @'
find $($_.MountPoint) -mtime +1 | xargs rm;
'@

$secPswd = ConvertTo-SecureString 'xxx' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('root', $secPswd)

$ramDisks = 'root','tmp','vsantraces'
$report = @()

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $esx.Name -V2
    $esxcli.system.visorfs.ramdisk.list.Invoke() | 
    where{$ramDisks -contains $_.RamdiskName -and $_.Free -lt 10} |
    ForEach-Object -Process {
        $obj = [ordered]@{
            VMHost = $esx.Name
            RAMDisk = $_.RamdiskName
            FreePre = $_.Free
        }
        Get-VMHostService -VMHost $esx | 
        where{$_.Key -eq 'TSM-SSH' -and -not $_.Running} | 
        Start-VMHostService -Confirm:$false | Out-Null
    
        $session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
        $cmd = $ExecutionContext.InvokeCommand.ExpandString($cmd)
        Invoke-SSHCommand -SSHSession $session -Command $cmd | Select -ExpandProperty Output
        Remove-SSHSession -SSHSession $session | Out-Null

        $newStatus = $esxcli.system.visorfs.ramdisk.list.Invoke() | where{$_.RamdiskName -eq $obj.RAMDisk}
        $obj.Add('FreePost',$newStatus.Free)
        $report += New-Object -TypeName PSObject -Property $obj
    }
    Get-VMHostService -VMHost $esx | 
    where{$_.Key -eq 'TSM-SSH' -and $_.Running} | 
    Stop-VMHostService -Confirm:$false | Out-Null
}

if($report.Count -ne 0){
    $body = $report | ConvertTo-Html -Head $Header | Out-String
}
else{
    $body = '<p>No RAM disks found that meet the criteria</p>'
}

$sMail = @{
    To         = "xxx"
    From       = "xxx"
    #Cc = 
    Subject    = "Ramdisk cleanup report on: $date "
    Body       = $body
    BodyAsHtml = $true
    Priority   = 'High'
    SmtpServer = "xxx"
}
Send-MailMessage @smail


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

swamynaveen
Enthusiast
Enthusiast

@LucD Thank you. I will run the script against couple hosts and update the status. 

0 Kudos
swamynaveen
Enthusiast
Enthusiast

@LucD  As the current script  deletes all the files which are older than 2day under each ramdisk whose current storage is >90%. As there are lot of other files on each ramdisk which i don't want to clear as we may run into any issues if we delete all other files. hence, Can we use if condition to run the clean up for each ramdisk with speific SSH commands link below? Kindly suggest.

 

$cmd_tmp = @'
find /tmp/ -name "ams-*" -mtime +1 | xargs rm;
'@

$cmd_vsantraces = @'
find /vsantraces/ -name "*.gz" -mtime +1 | xargs rm;
'@

$cmd_var = @'
find /var/log/ -name "*.gz" -mtime +1 | xargs rm;
'@

$ramDisks = 'root','tmp','vsantraces'
$report = @()

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx.Name -V2
$esxcli.system.visorfs.ramdisk.list.Invoke() |
where{$ramDisks -contains $_.RamdiskName -and $_.Free -lt 10} |
ForEach-Object -Process {
$obj = [ordered]@{
VMHost = $esx.Name
RAMDisk = $_.RamdiskName
FreePre = $_.Free
}

if($ramDisks.name -eq "tmp") {
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and -not $_.Running} |
Start-VMHostService -Confirm:$false | Out-Null

$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
$cmd_tmp = $ExecutionContext.InvokeCommand.ExpandString($cmd_tmp)
Invoke-SSHCommand -SSHSession $session -Command $cmd_tmp | Select -ExpandProperty Output
Remove-SSHSession -SSHSession $session | Out-Null

$newStatus = $esxcli.system.visorfs.ramdisk.list.Invoke() | where{$_.RamdiskName -eq $obj.RAMDisk}
$obj.Add('FreePost',$newStatus.Free)
$report += New-Object -TypeName PSObject -Property $obj
}

elseif($ramDisks.name -eq "vsantraces") {
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and -not $_.Running} |
Start-VMHostService -Confirm:$false | Out-Null

$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
$cmd_vsantraces = $ExecutionContext.InvokeCommand.ExpandString($cmd_vsantraces)
Invoke-SSHCommand -SSHSession $session -Command $cmd_vsantraces | Select -ExpandProperty Output
Remove-SSHSession -SSHSession $session | Out-Null

$newStatus = $esxcli.system.visorfs.ramdisk.list.Invoke() | where{$_.RamdiskName -eq $obj.RAMDisk}
$obj.Add('FreePost',$newStatus.Free)
$report += New-Object -TypeName PSObject -Property $obj
}

else($ramDisks.name -eq "var") {
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and -not $_.Running} |
Start-VMHostService -Confirm:$false | Out-Null

$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
$cmd_var = $ExecutionContext.InvokeCommand.ExpandString($cmd_var)
Invoke-SSHCommand -SSHSession $session -Command $cmd_var | Select -ExpandProperty Output
Remove-SSHSession -SSHSession $session | Out-Null

$newStatus = $esxcli.system.visorfs.ramdisk.list.Invoke() | where{$_.RamdiskName -eq $obj.RAMDisk}
$obj.Add('FreePost',$newStatus.Free)
$report += New-Object -TypeName PSObject -Property $obj
}
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and $_.Running} |
Stop-VMHostService -Confirm:$false | Out-Null
}

0 Kudos
LucD
Leadership
Leadership

Sure, you can do that.

But this thread, again, seems to be going on forever without ever getting the "Solved" indication.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

swamynaveen
Enthusiast
Enthusiast

@LucD  Actually, the script is not working as per requirement. Hence, I have not marked this thread as solved for now. I've tested it for one ESX host and getting the following errors. Could you pls suggest if i am missing any thing here?

 

 

#####################

Import-Module Posh-SSH
$date = Get-Date -UFormat "%A %m/%d/%Y %R %Z"

$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@

$cmd_vsantraces = @'
find /vsantraces/ -name "*.gz" -mtime +1 | xargs rm;
'@
$cmd_tmp = @'
find /tmp/ -name "ams-*" -mtime +1 | xargs rm;
'@

$secPswd = ConvertTo-SecureString 'XXXX' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('root', $secPswd)
Connect-VIServer -Server "vcsa1"

$ramDisks = 'vsantraces','tmp'
$report = @()

$esx = "Hostname1"
Get-VMHost $esx |
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx.Name -V2
$esxcli.system.visorfs.ramdisk.list.Invoke() |
where{$ramDisks -contains $_.RamdiskName -and $_.Free -lt 10} |
ForEach-Object -Process {
$obj = [ordered]@{
VMHost = $esx.Name
RAMDisk = $_.RamdiskName
FreePre = $_.Free
}
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and -not $_.Running} |
Start-VMHostService -Confirm:$false | Out-Null

$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
$cmd_vsantraces = $ExecutionContext.InvokeCommand.ExpandString($cmd_vsantraces)
Invoke-SSHCommand -SSHSession $session -Command $cmd_vsantraces | Select -ExpandProperty Output
$cmd_tmp = $ExecutionContext.InvokeCommand.ExpandString($cmd_tmp)
Invoke-SSHCommand -SSHSession $session -Command $cmd_tmp | Select -ExpandProperty Output
Remove-SSHSession -SSHSession $session | Out-Null

$newStatus = $esxcli.system.visorfs.ramdisk.list.Invoke() | where{$_.RamdiskName -eq $obj.RAMDisk}
$obj.Add('FreePost',$newStatus.Free)
$report += New-Object -TypeName PSObject -Property $obj
}
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and $_.Running} |
Stop-VMHostService -Confirm:$false | Out-Null
}

if($report.Count -ne 0){
$body = $report | ConvertTo-Html -Head $Header | Out-String
}
else{
$body = '<p>No RAM disks found that meet the criteria</p>'
}

$sMail = @{
To = "XXXX"
From = "XXXX"
#Cc =
Subject = "Ramdisk cleanup report on: $date "
Body = $body
BodyAsHtml = $true
Priority = 'High'
SmtpServer = "XXXX"
}
Send-MailMessage @smail

#############################

 

Output & Error: I'm getting below report which is completely invalid.

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

VMHost

RAMDisk

FreePre

FreePost

 

tmp

0

0

 

vsantraces

0

0

 

tmp

0

0

 

vsantraces

0

0

 

 

Error:

=====

New-SSHSession : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the
command again.
At line:42 char:49
+ $session = New-SSHSession -ComputerName $esx.Name -Credential ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-SSHSession], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,SSH.NewSshSession

Invoke-SSHCommand : Cannot bind argument to parameter 'SSHSession' because it is null.
At line:44 char:39
+ Invoke-SSHCommand -SSHSession $session -Command $cmd_vsantrac ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-SSHCommand], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Invoke-SSHCommand

Invoke-SSHCommand : Cannot bind argument to parameter 'SSHSession' because it is null.
At line:46 char:39
+ Invoke-SSHCommand -SSHSession $session -Command $cmd_tmp | Se ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-SSHCommand], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Invoke-SSHCommand

 
 

 

 

0 Kudos
LucD
Leadership
Leadership

It looks as if the names with which the ESXi nodes are added to the VCSA do not correspond with the FQDN of the ESXi node.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
swamynaveen
Enthusiast
Enthusiast

Yes. that's right ESXi name on VCSA and FQDN is not matching. How can we i run the script in such cases?

 

swamynaveen_1-1607950027100.png

 

0 Kudos
LucD
Leadership
Leadership

You will have to get to the FQDN.
Is it only the domain that is missing?
Is it the same domain for ESXi nodes?
In that case you can just append the domainname


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
swamynaveen
Enthusiast
Enthusiast

Yep. It's same domain name for  all ESX nodes

0 Kudos
LucD
Leadership
Leadership

Then can't you just add that to the ESXi nodename?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
swamynaveen
Enthusiast
Enthusiast

@LucD  I could see that $esx.Name doesn't showing any value due to which script is throwing below errors. Could you pls let me know how can i run the script for specific number of hosts? pls review below script once.

$vmhosts = gc "C:\HostList.txt"

foreach ($esx in $vmhosts){
Get-VMHost $esx |
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx.Name -V2
$esxcli.system.visorfs.ramdisk.list.Invoke() |
where{$ramDisks -contains $_.RamdiskName -and $_.Free -lt 10} |
ForEach-Object -Process {
$obj = [ordered]@{
VMHost = $esx.Name
RAMDisk = $_.RamdiskName
FreePre = $_.Free
}
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and -not $_.Running} |
Start-VMHostService -Confirm:$false | Out-Null

$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
$cmd_vsantraces = $ExecutionContext.InvokeCommand.ExpandString($cmd_vsantraces)
Invoke-SSHCommand -SSHSession $session -Command $cmd_vsantraces | Select -ExpandProperty Output
$cmd_tmp = $ExecutionContext.InvokeCommand.ExpandString($cmd_tmp)
Invoke-SSHCommand -SSHSession $session -Command $cmd_tmp | Select -ExpandProperty Output
Remove-SSHSession -SSHSession $session | Out-Null

$newStatus = $esxcli.system.visorfs.ramdisk.list.Invoke() | where{$_.RamdiskName -eq $obj.RAMDisk}
$obj.Add('FreePost',$newStatus.Free)
$report += New-Object -TypeName PSObject -Property $obj
}
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and $_.Running} |
Stop-VMHostService -Confirm:$false | Out-Null
}
}

if($report.Count -ne 0){
$body = $report | ConvertTo-Html -Head $Header | Out-String
}
else{
$body = '<p>No RAM disks found that meet the criteria</p>'
}

 

Get-EsxCli : 14-12-2020 22:00:10 Get-EsxCli Value cannot be found for the mandatory parameter VMHost
At line:30 char:15
+ $esxcli = Get-EsxCli -VMHost $esx.Name -V2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-EsxCli], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.EsxCli.GetEsxCli

Response status code does not indicate success: 500 (Internal Server Error).
At line:31 char:5
+ $esxcli.system.visorfs.ramdisk.list.Invoke() |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ViError
+ FullyQualifiedErrorId : VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViError

New-SSHSession : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the

0 Kudos
LucD
Leadership
Leadership

That is not the code I posted earlier.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
swamynaveen
Enthusiast
Enthusiast

@LucD  I  just added foreach loop to your main script in order to input some hosts. please find below the complete script.

 

Import-Module Posh-SSH
$date = Get-Date -UFormat "%A %m/%d/%Y %R %Z"

$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@

$cmd_vsantraces = @'
find /vsantraces/ -name "*.gz" -mtime +1 | xargs rm;
'@
$cmd_tmp = @'
find /tmp/ -name "ams-*" -mtime +1 | xargs rm;
'@

$secPswd = ConvertTo-SecureString 'XXXXX' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('root', $secPswd)

$ramDisks = 'vsantraces','tmp'
$report = @()
#-PipelineVariable esx
$vmhosts = gc "C:\HostList.txt"
foreach ($esx in $vmhosts){
Get-VMHost $esx |
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx.Name -V2
$esxcli.system.visorfs.ramdisk.list.Invoke() |
where{$ramDisks -contains $_.RamdiskName -and $_.Free -lt 10} |
ForEach-Object -Process {
$obj = [ordered]@{
VMHost = $esx.Name
RAMDisk = $_.RamdiskName
FreePre = $_.Free
}
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and -not $_.Running} |
Start-VMHostService -Confirm:$false | Out-Null

$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
$cmd_vsantraces = $ExecutionContext.InvokeCommand.ExpandString($cmd_vsantraces)
Invoke-SSHCommand -SSHSession $session -Command $cmd_vsantraces | Select -ExpandProperty Output
$cmd_tmp = $ExecutionContext.InvokeCommand.ExpandString($cmd_tmp)
Invoke-SSHCommand -SSHSession $session -Command $cmd_tmp | Select -ExpandProperty Output
Remove-SSHSession -SSHSession $session | Out-Null

$newStatus = $esxcli.system.visorfs.ramdisk.list.Invoke() | where{$_.RamdiskName -eq $obj.RAMDisk}
$obj.Add('FreePost',$newStatus.Free)
$report += New-Object -TypeName PSObject -Property $obj
}
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and $_.Running} |
Stop-VMHostService -Confirm:$false | Out-Null
}
}

if($report.Count -ne 0){
$body = $report | ConvertTo-Html -Head $Header | Out-String
}
else{
$body = '<p>No RAM disks found that meet the criteria</p>'
}

$sMail = @{
To = "XXXXXX"
From = "XXXXXX"
#Cc =
Subject = "Ramdisk cleanup report on: $date "
Body = $body
BodyAsHtml = $true
Priority = 'High'
SmtpServer = "XXXXX"
}
Send-MailMessage @smail

0 Kudos
LucD
Leadership
Leadership

It looks as if your .txt file might contain a blank line.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
swamynaveen
Enthusiast
Enthusiast

@LucD I've reviewed hostlist in text file and there are no such blank spaces. I"m still getting below error message. Could you suggest if there any changes required in the script to be able to run it for few hosts?

 

Get-EsxCli : 15-12-2020 14:21:37 Get-EsxCli Value cannot be found for the mandatory parameter VMHost
At line:30 char:15
+ $esxcli = Get-EsxCli -VMHost $esx.Name -V2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-EsxCli], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.EsxCli.GetEsxCli

You cannot call a method on a null-valued expression.
At line:31 char:5
+ $esxcli.system.visorfs.ramdisk.list.Invoke() |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Get-EsxCli : 15-12-2020 14:21:38 Get-EsxCli Value cannot be found for the mandatory parameter VMHost
At line:30 char:15
+ $esxcli = Get-EsxCli -VMHost $esx.Name -V2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-EsxCli], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.EsxCli.GetEsxCli

0 Kudos