Вопрос

I am using this form post as a baseline for a PowerCLI script I am trying to write.

Powershell Get-QADUser results to SQL table

My modified scrip looks like this:

Add-PSSnapin VMware.VimAutomation.Core
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100

$db_server = "127.0.0.1\VIM_SQLEXP"
$db = "Billing"
$table = "vmdata"
$username = "import"
$pwd = "myPassWord"

# First, clear existing table
$sql_query_del = "DELETE FROM $table"
Invoke-Sqlcmd -ServerInstance $db_server -Database $db -Username $username -Password $pwd -Query $sql_query_del

# Get vms and resources add to DB
Get-resourcepool -name finance | get-vm | foreach {
    $name = $_.Name
    $PowerState = $_.PowerState
    $NumCPUs = $_.'Num CPUs'
    $MemoryGB = $_.MemoryGB    
    Write-Host " Name : $Name       PowerState : $PowerState       MemoryGB : $MemoryGB       Num CPUs : $NumCPUs"

    $sql_query = "INSERT INTO $table (Name, PowerState, MemoryGB, NumCPUs) VALUES ('$Name', '$PowerState', '$MemoryGB', '$NumCPUs')"
    Invoke-Sqlcmd -ServerInstance $db_server -Database $db -Username $username -Password $pwd -Query $sql_query

}

My problem is the Num CPUs that has a space in it when just executing this command

Get-resourcepool -name finance | get-vm 


Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
GreatPlains01        PoweredOn  4        8.000

what is the appropriate method to reference this to have the result display?

When executing this via power shell I get the following result:

 Name : GreatPlains01       PowerState : PoweredOn       MemoryGB : 8       Num CPUs :

So the variable I am using isn't correct I have tried [] "" and ' ' around Num CPUs with no success.

Это было полезно?

Решение

Although the column header in the output of the Get-VM cmdlet is 'Num CPUs', the property name is NumCPU. So you should use:

$NumCPUs = $_.NumCPU

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top