VMware Cloud Community
VMwareGeek_
Enthusiast
Enthusiast

How to apply a PS-script to a cluster only

Hi,

I have a PS Script that renames all datastores starts "datastore1*" to  " hostname-localds" in vcenter, but even if I add some entries to apply only a cluster, it first shows the host list of cluster I define, but then script applies to all vcenter.

I want to apply to the cluster I defined.

How can I fix this? What am I missing here when I try to update my script?
We need to run the script only for hosts in a cluster for renaming their local datastores from datastore1 to hostname-localds

My Script is as below;

Connect-VIServer vcenter.abc.local

Get-Cluster Clusternamehere | Get-VMHost

$datastoreNames = get-datastore datastore1*
$datastoreNames
foreach ($Datastore in $datastoreNames) {
write-host $Datastore.name "- " -NoNewline
$VMhostFQDN = (get-vmhost -id $(get-datastore $datastore).ExtensionData.host.key).name
$VMhostname = $VMhostFQDN.Split(".")[0]
$datastorenewname = $VMhostname + "-localds"
Get-datastore -name $datastore | Set-datastore -Name $datastorenewname
}

 

0 Kudos
5 Replies
LucD
Leadership
Leadership

On the Get-Datastore cmdlet, you can use the RelatedObject parameter and provide the Cluster object.


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

0 Kudos
VMwareGeek_
Enthusiast
Enthusiast

So, script will be as follow, am I right?

 

Connect-VIServer vcenter.abc.local

Get-Cluster Clusternamehere | Get-VMHost

$datastoreNames = get-datastore datastore1*
$datastoreNames
foreach ($Datastore in $datastoreNames) {
write-host $Datastore.name "- " -NoNewline
$VMhostFQDN = (get-vmhost -id $(get-datastore -cluster Clusternamehere $datastore).ExtensionData.host.key).name
$VMhostname = $VMhostFQDN.Split(".")[0]
$datastorenewname = $VMhostname + "-localds"
Get-datastore -name $datastore | Set-datastore -Name $datastorenewname
}

0 Kudos
LucD
Leadership
Leadership

No, not like that.

Are these datastores local to each ESXi node in the cluster?


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

0 Kudos
VMwareGeek_
Enthusiast
Enthusiast

Yes, these are the local datastores for each host located on same esx cluster at vcenter.

0 Kudos
LucD
Leadership
Leadership

Then I would do something like this

Connect-VIServer vcenter.abc.local

Get-Cluster -Name <Clusternamehere> |
Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
  Get-Datastore -VMHost $esx -Name datastore1* | 
  Set-Datastore -Name "$($esx.Name.Split('.')[0])-localds" -Confirm:$false
}


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

0 Kudos