VMware Cloud Community
PSScripter
Enthusiast
Enthusiast
Jump to solution

Trouble adding a string to my array

Hello Folks,

I have this script that mostly does what I need, Im only trying to add the cluster name as the 1st column in the output. I am not doing to correctly as I do not see the cluster name listed in output.

$virtualcenter = 'a', 'b', 'c', 'd'
$clustername = 'e', 'f', 'g', 'h'
$outputFile = "C:\temp\output.csv"

foreach ($vc in $virtualcenter) {

    Connect-VIServer -Server $vc
    foreach ($clus in $clustername) {
        $VMsAdv = get-cluster $clus -ErrorAction SilentlyContinue | Get-VM | Sort-Object Name | ForEach-Object { Get-View $_.ID } 
        $myCol = @() 
        ForEach ($VMAdv in $VMsAdv) { 
            ForEach ($Disk in $VMAdv.Layout.Disk) { 
                $myobj = $clus
                $myObj = "" | Select-Object Name, Disk 
                $myObj.Name = $VMAdv.Name 
                $myObj.Disk = $Disk.DiskFile[0] 
                $myCol += $myObj 
            } 
        } 
        $myCol | Export-Csv -Append $outputFile -NoTypeInformation
    } Disconnect-VIServer -Server $vc -Confirm:$false
}

Thanks much for taking a look

Reply
0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I think the script can be made a bit simpler, and still get all the properties you want (and some more).
You can remove the properties you don't want.

Try like this

 

$virtualcenter = 'a', 'b', 'c', 'd'
$clustername = 'e', 'f', 'g', 'h'
$outputFile = "C:\temp\output.csv"

Connect-VIServer -Server $vc

Get-Cluster -Name $clustername -PipelineVariable cluster |
Get-VM -PipelineVariable vm |
Get-HardDisk -PipelineVariable hd |
ForEach-Object -Process {
  $dsName = $hd.FileName.Split(']')[0].TrimStart('[')
  New-Object -TypeName PSObject -Property ([ordered]@{
    vCenter = ([uri]$vm.ExtensionData.Client.ServiceUrl)
    Cluster = $cluster.Name
    VMHost = $vm.VMHost.Name
    VM = $vm.Name
    Harddisk = $hd.Name
    DiskFile = $hd.FileName
    Datastore = $dsName
    DatastoreCluster = (Get-DatastoreCluster -Datastore $dsName).Name
  })
} | Export-Csv -Append $outputFile -NoTypeInformation

Disconnect-VIServer -Server * -Confirm:$false

 

 


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

View solution in original post

PSScripter
Enthusiast
Enthusiast
Jump to solution

Thats great LucD, here is my final version. Thanks alot for your help!

 

 

 

 

 

$vcs = (Get-Content C:\temp\ITScript\vSphere\vcs.txt)
$clusters = (Get-Content C:\temp\ITScript\vSphere\scmclusters.txt)

foreach ($vc in $vcs) {
    Connect-VIServer $vc
    Get-Cluster -Name $clusters -ErrorAction SilentlyContinue -PipelineVariable  cluster |
    Get-VMHost -PipelineVariable esx |
    Get-VMHostNetworkAdapter -VMKernel -PipelineVariable vmk |
    ForEach-Object -Process {
        $esxcli = Get-EsxCli -VMHost $esx -V2
        $if = $esxcli.network.ip.interface.ipv4.get.Invoke(@{interfacename = $vmk.Name })
        $stack = $vmk.ExtensionData.Spec.NetStackInstanceKey
        $dns = Get-VMHostNetworkStack -VMHost $esx -Name $stack
        $ip6enabled = Get-VMHostNetwork -VMHost $esx | Select-Object IPv6Enabled
        New-Object -TypeName PSObject -Property ([ordered]@{
                vCenter         = ([uri]$esx.ExtensionData.Client.ServiceUrl).Host
                Cluster         = $cluster.Name
                Hostname        = $esx.Name
                Device          = $vmk.Name
                DeviceIP        = $if.IPv4Address
                DeviceMask      = $if.IPv4Netmask
                Gateway         = $if.Gateway
                DnsAddress      = $dns.DnsAddress -join ','
                NTP             = (Get-VMHostNtpServer -VMHost $esx) -join ', '
                PortGroup       = $vmk.PortGroupName
                DNSSearchDomain = $dns.DnsSearchDomain -join '.'
                IP6enabled      = $ip6enabled.IPv6Enabled -join '.'
                })
    } | Export-Csv -Append C:\temp\VMK1GW-Output.csv
Disconnect-VIServer -Server * -Confirm:$false
}

 

 

 

View solution in original post

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$virtualcenter = 'a', 'b', 'c', 'd'
$clustername = 'e', 'f', 'g', 'h'
$outputFile = "C:\temp\output.csv"

$myCol = @()

foreach ($vc in $virtualcenter) {
  Connect-VIServer -Server $vc
  foreach ($clus in $clustername) {
    $VMsAdv = Get-Cluster $clus -ErrorAction SilentlyContinue | Get-VM | Sort-Object Name | ForEach-Object { Get-View $_.ID }
    ForEach ($VMAdv in $VMsAdv) {
      ForEach ($Disk in $VMAdv.Layout.Disk) {
        $myObj = "" | Select-Object Cluster, Name, Disk
        $myobj.Cluster = $clus
        $myObj.Name = $VMAdv.Name
        $myObj.Disk = $Disk.DiskFile[0]
        $myCol += $myObj
      }
    }
  }
  Disconnect-VIServer -Server $vc -Confirm:$false
}
$myCol | Export-Csv -Append $outputFile -NoTypeInformation


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

Reply
0 Kudos
PSScripter
Enthusiast
Enthusiast
Jump to solution

LucD,

Nice, that does add the cluster name, thanks!

I think my looping is wrong as I am getting 5 instances of the output. I think I may need to move the Disconnect-VIServer up a line or 2, Ill play with that.

I have trouble wrapping my head around the logic though, and I also wanted to add Datastore Cluster Name. Ill try and add that, but suggestions would be welcome.

 

Thanks!

Reply
0 Kudos
PSScripter
Enthusiast
Enthusiast
Jump to solution

Yea Ive had one of those days. That's a script I put together from other places, not your script. I knew it was easy to add the cluster name, thanks now I know to just reference the property of the object.

Any suggestions about adding Datastore Cluster name to the output would be fantastic!

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

A DatastoreCluster is defined on the Datacenter level.
How would you add that your report that is on the cluster level


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

Reply
0 Kudos
PSScripter
Enthusiast
Enthusiast
Jump to solution

This seems to work, I need to go through the data though a bit closer. but I still get like multiple instances of the output, my loop is not right.

 

 

$virtualcenter = 'a', 'b', 'c', 'd'
$clustername = 'e', 'f', 'g', 'h'
$dsclustername = 'i', 'j', 'k', 'l','m','n','o','p'
$outputFile = "C:\temp\output.csv"

$myCol = @()

foreach ($vc in $virtualcenter) {
    Connect-VIServer -Server $vc
    foreach ($dsclu in $dsclustername) {
        foreach ($clus in $clustername) {
            $VMsAdv = Get-Cluster $clus -ErrorAction SilentlyContinue | Get-VM | Sort-Object Name | ForEach-Object { Get-View $_.ID }
            ForEach ($VMAdv in $VMsAdv) {
                ForEach ($Disk in $VMAdv.Layout.Disk) {
                    $myObj = "" | Select-Object Cluster, Name, Disk, DSName
                    $myobj.Cluster = $clus
                    $myObj.Name = $VMAdv.Name
                    $myObj.Disk = $Disk.DiskFile[0]
                    $myObj.Name = $dsclu
                    $myCol += $myObj
                }
            }
        }  
    } Disconnect-VIServer -Server $vc -Confirm:$false
} 
$myCol | Export-Csv -Append $outputFile -NoTypeInformation
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Leave out the Datastorecluster loop,
There is no link between a Datastorecluster and a Cluster.
You are running for each cluster inside the loop for each DatastoreCluster.
That makes no sense


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

Reply
0 Kudos
PSScripter
Enthusiast
Enthusiast
Jump to solution

Yea your right. Is there any way to just tack on what DatastoreCluster each vmdk is in?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I think the script can be made a bit simpler, and still get all the properties you want (and some more).
You can remove the properties you don't want.

Try like this

 

$virtualcenter = 'a', 'b', 'c', 'd'
$clustername = 'e', 'f', 'g', 'h'
$outputFile = "C:\temp\output.csv"

Connect-VIServer -Server $vc

Get-Cluster -Name $clustername -PipelineVariable cluster |
Get-VM -PipelineVariable vm |
Get-HardDisk -PipelineVariable hd |
ForEach-Object -Process {
  $dsName = $hd.FileName.Split(']')[0].TrimStart('[')
  New-Object -TypeName PSObject -Property ([ordered]@{
    vCenter = ([uri]$vm.ExtensionData.Client.ServiceUrl)
    Cluster = $cluster.Name
    VMHost = $vm.VMHost.Name
    VM = $vm.Name
    Harddisk = $hd.Name
    DiskFile = $hd.FileName
    Datastore = $dsName
    DatastoreCluster = (Get-DatastoreCluster -Datastore $dsName).Name
  })
} | Export-Csv -Append $outputFile -NoTypeInformation

Disconnect-VIServer -Server * -Confirm:$false

 

 


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

PSScripter
Enthusiast
Enthusiast
Jump to solution

Thats great LucD, here is my final version. Thanks alot for your help!

 

 

 

 

 

$vcs = (Get-Content C:\temp\ITScript\vSphere\vcs.txt)
$clusters = (Get-Content C:\temp\ITScript\vSphere\scmclusters.txt)

foreach ($vc in $vcs) {
    Connect-VIServer $vc
    Get-Cluster -Name $clusters -ErrorAction SilentlyContinue -PipelineVariable  cluster |
    Get-VMHost -PipelineVariable esx |
    Get-VMHostNetworkAdapter -VMKernel -PipelineVariable vmk |
    ForEach-Object -Process {
        $esxcli = Get-EsxCli -VMHost $esx -V2
        $if = $esxcli.network.ip.interface.ipv4.get.Invoke(@{interfacename = $vmk.Name })
        $stack = $vmk.ExtensionData.Spec.NetStackInstanceKey
        $dns = Get-VMHostNetworkStack -VMHost $esx -Name $stack
        $ip6enabled = Get-VMHostNetwork -VMHost $esx | Select-Object IPv6Enabled
        New-Object -TypeName PSObject -Property ([ordered]@{
                vCenter         = ([uri]$esx.ExtensionData.Client.ServiceUrl).Host
                Cluster         = $cluster.Name
                Hostname        = $esx.Name
                Device          = $vmk.Name
                DeviceIP        = $if.IPv4Address
                DeviceMask      = $if.IPv4Netmask
                Gateway         = $if.Gateway
                DnsAddress      = $dns.DnsAddress -join ','
                NTP             = (Get-VMHostNtpServer -VMHost $esx) -join ', '
                PortGroup       = $vmk.PortGroupName
                DNSSearchDomain = $dns.DnsSearchDomain -join '.'
                IP6enabled      = $ip6enabled.IPv6Enabled -join '.'
                })
    } | Export-Csv -Append C:\temp\VMK1GW-Output.csv
Disconnect-VIServer -Server * -Confirm:$false
}

 

 

 

Reply
0 Kudos