Automation

 View Only
  • 1.  How to apply a PS-script to a cluster only

    Posted May 24, 2023 02:12 PM

    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
    }

     



  • 2.  RE: How to apply a PS-script to a cluster only

    Posted May 24, 2023 02:36 PM

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



  • 3.  RE: How to apply a PS-script to a cluster only

    Posted May 24, 2023 02:45 PM

    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
    }



  • 4.  RE: How to apply a PS-script to a cluster only

    Posted May 24, 2023 03:03 PM

    No, not like that.

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



  • 5.  RE: How to apply a PS-script to a cluster only

    Posted May 24, 2023 03:07 PM

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



  • 6.  RE: How to apply a PS-script to a cluster only

    Posted May 24, 2023 03:18 PM

    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
    }