Как очистить все отключенные хранилища у хостов в VMware ESXi через скрипт

Обновлено 12.01.2015

Ранее я писал Как массово отключить хранилища от хостов VMware ESXI

Собственно сам скрипт внизу

#########################################################################################################
#
#  Скрипт очищает отключенные датасторы у выбранных хостов.
# Переменная $hosts — список хостов
#
#########################################################################################################

# Подключаем оснастку, если ее нет
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
add-pssnapin VMware.*
}
else
{
Write-Host «Snap in already added.»
}

#################################################
# Убираем ругань на самоподписанный сертификат
Set-powercliconfiguration -InvalidCertificateAction Ignore -Confirm:$false | out-null
#################################################

# name vCenter Server
$vCenter = «bs-vc»

# Если не подключены к vCentr, то подключаемся
if ($global:DefaultVIServers.Count -lt 1)
{
Connect-VIServer $vCenter
}

# Список хостов
$hosts=»bs-esx05″

Foreach ($esxhost in $hosts)
{
$esxhostname=$esxhost
$esxhostname
$esxcli=get-esxcli -vmhost $esxhostname
$detachedcounter=0
# Get storage devices that have status of off e.g. Offline/Dead
$detached=$esxcli.storage.core.device.List() | where {$_.Status -eq «off»}
IF ($detached -ne $null)
{
foreach ($device in $detached)
{
$deviceID=($device).Device
Start-sleep -s 5
$IsDetached=$null
#Spisok Offline/Dead Datastores
$IsDetached=$esxcli.storage.core.device.Detached.List($deviceID)
IF ($IsDetached -ne $null)
{
Start-sleep -s 2
Write-Host «Removing Detached LUN $deviceID from $esxhostname»
# Remove Detached device
$esxcli.storage.core.device.Detached.Remove($false,$deviceID)
}
$detachedcounter++
}
# Rescan All HBA’s on server, this is essential as the VI Client misreports the data until this has been done.
Get-VMHostStorage -VMHost $esxhostname -RescanAllHBA
Start-sleep -s 5
}
$esxhostname=$null
}
Write-Host «Itogo » $detachedcounter

Автор - Сёмин Иван

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *