When I am running the code below I seem to loose the content of my object $VMHostCount every time I exit the function. I want to call the function multiple times and store the results in the $VMHostCount boject before reporting it out at the end of the script. I am pretty new to powershell and could do this type of thing in vbs but I am finding it difficult in powershell. Can anyone help {:o)
Function GetVMHostNums {
Write-Host "I am working - " $Cluster.Name
$OnVM = Get-VM -Location $Cluster | where-object { $_.powerstate -eq
"poweredon"} | measure-object;
$OffVM = Get-VM -Location $Cluster | where-object { $_.powerstate -eq
"poweredoff"} | measure-object;
$OnHost = Get-VMHost -Location $Cluster | where-object { $_.state -eq
"connected"} | measure-object;
$OffHost = Get-VMHost -Location $Cluster | where-object { $_.state -ne
"connected"} | measure-object;
$row = "" | select Cluster, VMsOn, VMsOff, HostsOn, HostsOff, TimeStamp
$row.Cluster = $Cluster.Name
$row.VmsOn = (&{if($OnVM.Count -eq $null){0} else{$OnVM.Count}})
$row.VMsOff = (&{if($OffVM.Count -eq $null){0} else{$OffVM.Count}})
$row.HostsOn = (&{if($OnHost.Count -eq $null){0} else{$OnHost.Count}})
$row.HostsOff = (&{if($OffHost.Count -eq $null){0} else{$OffHost.Count}})
$row.Timestamp = $startDate
Write-Host $row
$VMHostCount += $row;
}
Main
Calling Code ################################################################
Add-PSsnapin VMware.VimAutomation.Core
Initialize-VIToolkitEnvironment.ps1
Connect-VIServer ???????? -User ?????????? -Password ????????
$startDate = (get-date).adddays(-1).tostring("dd/MM/yyyy")
$finishDate = (get-date).adddays(-0).tostring("dd/MM/yyyy")
$VMHostCount = @()
Get-Cluster | %{$Cluster = $_;
Write-Host $Cluster.Name;
GetVMHostNums
}
$VMHostCount | Export-Csv "c:\count.csv" -noTypeInformation
Thanks in advance
S
It has to do with the scope of the variable.
One way of solving this is like the following:
function GetVMHostNums { Write-Host "I am working - " $Cluster.Name $OnVM = Get-VM -Location $Cluster | where-object { $_.powerstate -eq "poweredon"} | measure-object $OffVM = Get-VM -Location $Cluster | where-object { $_.powerstate -eq "poweredoff"} | measure-object $OnHost = Get-VMHost -Location $Cluster | where-object { $_.state -eq "connected"} | measure-object $OffHost = Get-VMHost -Location $Cluster | where-object { $_.state -ne "connected"} | measure-object $row = "" | select Cluster, VMsOn, VMsOff, HostsOn, HostsOff, TimeStamp $row.Cluster = $Cluster.Name $row.VmsOn = (&{if($OnVM.Count -eq $null){0} else{$OnVM.Count}}) $row.VMsOff = (&{if($OffVM.Count -eq $null){0} else{$OffVM.Count}}) $row.HostsOn = (&{if($OnHost.Count -eq $null){0} else{$OnHost.Count}}) $row.HostsOff = (&{if($OffHost.Count -eq $null){0} else{$OffHost.Count}}) $row.Timestamp = $startDate Write-Host $row $row; } ################################################################ Add-PSsnapin VMware.VimAutomation.Core Initialize-VIToolkitEnvironment.ps1 Connect-VIServer ???????? -User ?????????? -Password ???????? $startDate = (get-date).adddays(-1).tostring("dd/MM/yyyy") $finishDate = (get-date).adddays(-0).tostring("dd/MM/yyyy") $VMHostCount = @() Get-Cluster | % {$Cluster = $_; Write-Host $Cluster.Name; $VMHostCount += GetVMHostNums } $VMHostCount | Export-Csv "c:\count.csv" -noTypeInformation
In this case the function returns an object and you add the object to $VMHostCount in the main script part.
Another solution would be to use a "global" variable.
Like this:
function testfunction{ $global:myvar += "abc" } $global:myvar = @() testfunction
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, thats superb, really apreciate the help. tried both ways and they work a treat.
:smileylaugh:
S