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
}
############
I didn't say 'blank space' but blank line.
Are you sure the last line in the .txt file is not an empty line?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes, i'm pretty much sure that there are no such blank lines exist and looks like this is an issue with $esx.Name and it doesn't inputting any objects due to which script is not going through it and directly executing the statement under else. I have changed $esx.Name to $esx post which script had executed successfully and cleared the older log files under ramdisks tmp & vsantraces.However, there is 1% change in the freespace percentage value in the report when comparing to Ramdisks on the host. Any idea why it' showing such difference? As per below report freespace% value for vSANtraces ramdisk is exactly mating with hosts where as rest of the disk still showing 1% difference.
VMHost | RAMDisk | FreeSpace_Percentage_Pre |
ppinbvsanesx08 | tmp | 61 |
ppinbvsanesx08 | vsantraces | 97 |
ppinbvsanesx07 | tmp | 62 |
ppinbvsanesx07 | vsantraces | 94 |
Those differences might be due to rounding
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD Thanks a lot for the help. I would need your help in adding 2 other parameters to below working script.
1. I would like to add Cluster & VCSA details in the report. but it's not showing details in proper format.
2. Is there a way to pass multiple root passwords to the ESXi hosts in this script if the host authentication fails with primary password then it should check with next password while connecting to ESXi hosts?
########Working_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 'XXXX' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('root', $secPswd)
$ramDisks = 'vsantraces','tmp'
$report = @()
#-PipelineVariable esx
$vmhosts = Get-Content -Path "C:\HostList.txt"
foreach ($esx in $vmhosts){
Get-VMHost $esx |
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $esx -V2
$esxcli.system.visorfs.ramdisk.list.Invoke() |
where{$ramDisks -contains $_.RamdiskName -and $_.Free -lt 10} |
ForEach-Object -Process {
$obj = [ordered]@{
VMHost = $esx
RAMDisk = $_.RamdiskName
FreeSpaceInfo_PreCheck = $_.Free
Cluster = Get-VMHost $esx | select @{N="Cluster Name";E={($_ | Get-Cluster).Name}}
VCSA = Get-VMHost $esx | select @{N='vCenter';E={$_.Uid.Split('@')[1].Split(':')[0]}}
}
Get-VMHostService -VMHost $esx |
where{$_.Key -eq 'TSM-SSH' -and -not $_.Running} |
Start-VMHostService -Confirm:$false | Out-Null
$session = New-SSHSession -ComputerName $esx -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('FreeSpace%_PostCheck',$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 = "XXXXX"
From = "XXXXX"
#Cc =
Subject = "Ramdisks Clean-up report for vSphere Hosts Date: $date "
Body = "Hi Team, The following Ramdisks were uilitzing >90% space and same has been cleaned now..!! Pls refer below report for current free space % details " + $body
BodyAsHtml = $true
#Priority = 'High'
SmtpServer = "XXXX"
}
Send-MailMessage @smail
#######Script End#########
Since this thread seems to become a thread that keeps on asking, I'm afraid I'm not going to be the user that keeps on answering.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD Sorry if i am bothering you on this. These are the 2 final stopes that I wanted to add to the script so that when we have bulk hosts & VCSA it would be easy to track and identify them. Could you help me in completing these 2 pending ones? Once it's done will mark the thread as solution and it will be useful to the community users as well.
Thank you very much for your cooperation and help on this thread.
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
vCenter column is showing empty and while run ([uri]$esx[0].ExtensionData.Client.ServiceUrl).Host manually so it doesn't' return any value.
There was a typo in there.
I corrected the code above
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
@LucD Thank you very much for the help.