VMware Cloud Community
noto777
Contributor
Contributor
Jump to solution

How to combine these two scripts

I have a script that outputs the hostname, datastore name and NAAid into a csv

$VMhosts = Get-datacenter xxxxxxxx | Get-VMHost

$data =  foreach($vmhost in $VMhosts){

   $vmhost | Get-Datastore |  Select @{N="VMHost";E={$vmhost.Name}},Name,@{N="CanonicalNames";E={[string]::Join(',',($_.ExtensionData.Info.Vmfs.Extent | %{$_.DiskName}))}}

  Get-VMHost $vmhost |Get-ScsiLun -CanonicalName $naa

}

$data | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture

 

Then I have another script that if I put the NAA ids in a txt file it will read them and give me all the multipath settings among other things

$naaids = get-content "c:\ctluns.txt"

foreach ($naa in $naaids) {

Get-Cluster ctclt | Get-VMHost| Get-ScsiLun -CanonicalName $naa | Export-Csv "c:\ctpaths.csv"

}

I would like one script that gives me all four items in one csv file.  Host name, Datastore Name, NAA Id and path.    I'm struggling with getting this to work specfically feeding the NAA Ids from script one into the line in script two that pulls the multipath info.  Can anyone help?

1 Solution

Accepted Solutions
GuilhermeAlves
Enthusiast
Enthusiast
Jump to solution

If you want only the MultipathPolicy, you can remove the rest of atributes.

Note: This script take a large amount of time to run if you have many datastores.

Here it is:


$VMhosts = Get-datacenter xxxxxxxxxx | Get-VMHost

$output=@()

foreach($vmhost in $VMhosts){

  $datastores = $vmhost | Get-Datastore
  $paths =@()

  foreach($dt in $datastores){

  $extentDatastore = $dt.ExtensionData.Info.Vmfs.Extent
  $extentDatastore| %{            
        $path = $vmhost |Get-ScsiLun -CanonicalName $_.DiskName
       
        $ConsoleDeviceName = $path.ConsoleDeviceName
        $LunType = $path.LunType
        $CapacityGB = $path.CapacityGB
        $MultipathPolicy = $path.MultipathPolicy
        $RuntimeName = $path.RuntimeName
        $Model = $path.Model
        $Vendor = $path.Vendor

        $tempOutput = ""| select VMHost,DatastoreName,CanonicalNames,ConsoleDeviceName,LunType,CapacityGB,MultipathPolicy,RuntimeName,Model,Vendor

        $tempOutput.VMHost = $vmhost.Name
        $tempOutput.DatastoreName = $dt.Name
        $tempOutput.CanonicalNames = $_.DiskName
        $tempOutput.ConsoleDeviceName = $ConsoleDeviceName
        $tempOutput.LunType = $LunType
        $tempOutput.CapacityGB = $CapacityGB
        $tempOutput.MultipathPolicy = $MultipathPolicy
        $tempOutput.RuntimeName = $RuntimeName
        $tempOutput.Model = $Model
        $tempOutput.Vendor = $Vendor

        $output+= $tempOutput
       
  }}
}

$output| Export-Csv "c:\ctpaths.csv" -NoTypeInformation -UseCulture

View solution in original post

Reply
0 Kudos
10 Replies
kunaludapi
Expert
Expert
Jump to solution

$VMhosts = Get-datacenter xxxxxxxx | Get-VMHost

$data =  foreach($vmhost in $VMhosts){

   $vmhost | Get-Datastore | Select @{N="VMHost";E={$vmhost.Name}},Name,@{N="CanonicalNames";E={[string]::Join(',',($_.ExtensionData.Info.Vmfs.Extent | %{$_.DiskName}))}}, @{N="multipathingPolicy";E={Get-ScsiLun -CanonicalName $_.ExtensionData.Info.Vmfs.Extent.Diskname | Select-Object -expandProperty MultipathPolicy }}

   ##Get-VMHost $vmhost | Get-ScsiLun -CanonicalName $naa

}

$data | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture

pastedImage_0.png

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
noto777
Contributor
Contributor
Jump to solution

When I run that nothing gets filled in for Multipath policy

Reply
0 Kudos
GuilhermeAlves
Enthusiast
Enthusiast
Jump to solution

If you want only the MultipathPolicy, you can remove the rest of atributes.

Note: This script take a large amount of time to run if you have many datastores.

Here it is:


$VMhosts = Get-datacenter xxxxxxxxxx | Get-VMHost

$output=@()

foreach($vmhost in $VMhosts){

  $datastores = $vmhost | Get-Datastore
  $paths =@()

  foreach($dt in $datastores){

  $extentDatastore = $dt.ExtensionData.Info.Vmfs.Extent
  $extentDatastore| %{            
        $path = $vmhost |Get-ScsiLun -CanonicalName $_.DiskName
       
        $ConsoleDeviceName = $path.ConsoleDeviceName
        $LunType = $path.LunType
        $CapacityGB = $path.CapacityGB
        $MultipathPolicy = $path.MultipathPolicy
        $RuntimeName = $path.RuntimeName
        $Model = $path.Model
        $Vendor = $path.Vendor

        $tempOutput = ""| select VMHost,DatastoreName,CanonicalNames,ConsoleDeviceName,LunType,CapacityGB,MultipathPolicy,RuntimeName,Model,Vendor

        $tempOutput.VMHost = $vmhost.Name
        $tempOutput.DatastoreName = $dt.Name
        $tempOutput.CanonicalNames = $_.DiskName
        $tempOutput.ConsoleDeviceName = $ConsoleDeviceName
        $tempOutput.LunType = $LunType
        $tempOutput.CapacityGB = $CapacityGB
        $tempOutput.MultipathPolicy = $MultipathPolicy
        $tempOutput.RuntimeName = $RuntimeName
        $tempOutput.Model = $Model
        $tempOutput.Vendor = $Vendor

        $output+= $tempOutput
       
  }}
}

$output| Export-Csv "c:\ctpaths.csv" -NoTypeInformation -UseCulture

Reply
0 Kudos
noto777
Contributor
Contributor
Jump to solution

I do have a lot of datastores.  I also get this error when I run the second script

Unable to index into an object of type VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl.

At C:\scripts\VMware\one more time.ps1:15 char:29

+ foreach($vmhost in $VMhosts[ <<<< 0]){

    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException

    + FullyQualifiedErrorId : CannotIndex

Reply
0 Kudos
GuilhermeAlves
Enthusiast
Enthusiast
Jump to solution

I've changed the VMhosts[0] to Vmhosts. Try to run again.

Reply
0 Kudos
noto777
Contributor
Contributor
Jump to solution

OK now I get

Get-ScsiLun : Cannot validate argument on parameter 'CanonicalName'. The argument is null or empty. Supply an argument that is not null or empty and

he command again.

At C:\scripts\VMware\one more time.ps1:21 char:52

+         $path = $vmhost |Get-ScsiLun -CanonicalName <<<<  $_.DiskName

    + CategoryInfo          : InvalidData: (:) [Get-ScsiLun], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetScsiLun

Reply
0 Kudos
GuilhermeAlves
Enthusiast
Enthusiast
Jump to solution

I get the same error when the Vcenter server is not connected.

Try to connect before or change the first line:

$VMhosts = Get-datacenter -server (connect-viserver "yourserver") | Get-VMHost

Reply
0 Kudos
noto777
Contributor
Contributor
Jump to solution

I'm actually connecting to vcenter earlier in the script. 

Reply
0 Kudos
GuilhermeAlves
Enthusiast
Enthusiast
Jump to solution

So try to use $defaultviservers at the start to check if it's really connected. If isn't, paste here all the code you are running pls.

--- Mensagem Original ---

De: "noto777" <communities-emailer@vmware.com>

Enviado: 21 de março de 2014 21:15

Para: "GuilhermeAlves" <guilhermestela@hotmail.com>

Assunto: New message: "How to combine these two scripts"

VMware Communities<https://communities.vmware.com/index.jspa>

How to combine these two scripts

created by noto777<https://communities.vmware.com/people/noto777> in VMware vSphere™ PowerCLI - View the full discussion<https://communities.vmware.com/message/2360088#2360088>

Reply
0 Kudos
noto777
Contributor
Contributor
Jump to solution

OK its working now.  I changed


$extentDatastore = $datastores.ExtensionData.Info.Vmfs.Extent 


to



$extentDatastore = $dt.ExtensionData.Info.Vmfs.Extent 



Thanks!