Automation

 View Only
  • 1.  Get-VMResourceConfiguration script output

    Posted Nov 16, 2011 09:20 AM

    Hey there guys im trying to make a csv file to output every vms share and and the number of shares they have. I am a big nub on Powershell i am trying to learn though so i thought to ask you gurus here. Ill put a copy of the script here.

    If i run the command Get-VMResourceConfiguration -VM xxx | Format-List -Property CpuSharesLevel for example i get the right output but when i try the same in my script it does not work.

    $start = Get-Date

    # Make a table with the output i want.
    # Name and then the property i want in the output.
    # Borrowed alot of the code from sandfordit.com from the wiki there thx alot.

    $table = New-Object system.Data.DataTable "Results"

    $col1 = New-Object system.Data.DataColumn Name, ([string])
    $col2 = New-Object system.Data.DataColumn NumCpuShares, ([string])
    $col3 = New-Object system.Data.DataColumn CpuSharesLevel, ([string])

    $table.columns.add($col1)
    $table.columns.add($col2)
    $table.columns.add($col3)

    $duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
    "Lagde tabell etter $duration sekunder"

    # Get me a list of vmms in a folder from the virtual center

    $vms = Get-Folder Boris_test | Get-VM | Sort -property Name

    $duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
    "Fikk antall vmer etter $duration sekunder"

    foreach ($vm in $vms)
    {
            $row = $table.NewRow()
           
            $row.Name = (Get-VM -Name $vm).Name
            $row.NumCpuShares = (Get-VMResourceConfiguration -VM $vm | Format-List -Property NumCpuShares).NumCpuShares
            $row.CpuSharesLevel = (Get-VMResourceConfiguration -VM $vm | Format-List -Property CpuSharesLevel).CpuSharesLevel
           
            $table.Rows.Add($row)
            "La til rad for $vm"
    }

    $duration = (New-TimeSpan $start (Get-Date)).TotalSeconds
    "Tabell fyllt ut etter $duration secs"

    $table | Format-Table
    $table | Export-Csv -path test.csv

    Thx Boris



  • 2.  RE: Get-VMResourceConfiguration script output
    Best Answer

    Posted Nov 16, 2011 01:26 PM

    Hi Boris,

    if you change the two lines:

    $row.NumCpuShares = (Get-VMResourceConfiguration -VM $vm | Format-List -Property NumCpuShares).NumCpuShares
    $row.CpuSharesLevel = (Get-VMResourceConfiguration -VM $vm | Format-List -Property CpuSharesLevel).CpuSharesLevel


    into:

    $VMResourceConfiguration = Get-VMResourceConfiguration -VM $vm

    $row.NumCpuShares = $VMResourceConfiguration.NumCpuShares
    $row.CpuSharesLevel = $VMResourceConfiguration.CpuSharesLevel

    your script will work.

    In the new code I inserted an extra line to call Get-VMResourceConfiguration only once. This will make the script a bit faster.

    Regards, Robert



  • 3.  RE: Get-VMResourceConfiguration script output

    Posted Nov 16, 2011 01:58 PM

    Thank you very much Robert that solved it.

    But i still wonder why it did not work the other way around even if your way works better.

    Easier to add a new variable in that way.

    Im a big beginner at this so but its really fun to work with powershell compared to some other scripting languages i have tried.

    Struggling to get a grasp about all those variables though but i guess thats something you have to learn.

    Best regards Boris



  • 4.  RE: Get-VMResourceConfiguration script output

    Posted Nov 16, 2011 02:27 PM

    The main problem with your two lines of code was that you piped the output of the Get-VMResourceConfiguration cmdlet to a Format-List cmdlet. After you did this the properties of the object changed and you couldn't retrieve the NumCpuShares or the CpuSharesLevel properties anymore.

    The object that is the result of the Get-VMResourceConfiguration cmdlet has the NumCpuShares and the CpuSharesLevel properties. So you could use this output directly as I did in my three lines of code.

    You should use the Format-* cmdlets only if you want to show the output on the screen. That means that they always should be the last cmdlets in the pipeline.

    Message was edited by: RvdNieuwendijk