VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Error while getting output from invoke-VMScript

Hi,

I am getting the below error while I use the invoke command

Error

Cannot convert value ""CSName","OS_CPU"

,"2"

" to type "System.Int32". Error: "Input string was not in a correct format."

At D:\Processor_Info\Processor_Info_3.0.ps1:58 char:5

+     $report1 += $row | Add-Member -MemberType NoteProperty -Name 'OS_ ...

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

Script :

Get-Folder DEV | get-vm | Where{$_.'PowerState' -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'windows'} | select 'Folder', 'Name',

@{N="IP_Address";E={@($_.guest.IPAddress[0])}},

@{N="OS"; E={@($_.guest.OSFullName)}},

@{N="VM_vCPU"; E={@($_.NumCPU)}},

@{N="VM_Memory(GB)"; E={@($_.MemoryGB)}} | Export-Csv -Path $reportlocation1 -NoTypeInformation -UseCulture

$code = @'

"### Output ###"

"" | Select @{n='OS_CPU';e={get-wmiobject Win32_ComputerSystem NumberOfLogicalProcessors | Select -ExpandProperty NumberOfLogicalProcessors}} | Select CSName,'OS_CPU' | ConvertTo-Csv -UseCulture -NoTypeInformation

"### Output ###"

"" | Select @{n='OS_Memory';e={(get-wmiobject Win32_ComputerSystem | select @{N="TotalPhysicalMemory(GB)";E={($_.TotalPhysicalMemory/1gb).tostring("N0")}}) | Select -ExpandProperty "TotalPhysicalMemory(GB)"}} | Select CSName, 'OS_Memory' | ConvertTo-Csv -UseCulture -NoTypeInformation

'@

$report1 = @()

Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $sInvoke = @{

        VM              = $_.Name

        GuestCredential = $Creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $code

    }

    $result = Invoke-VMScript @sInvoke

    $dummy, $out1, $out2 = $result.ScriptOutput -split '### Output ###'

    $out1 = $out1.TrimStart("`n`r")

    $out2 = $out2.TrimStart("`n`r")

    $report1 += $row | Add-Member -MemberType NoteProperty -Name 'OS_CPU' -Value ([int]$out1) -PassThru | Add-Member -MemberType NoteProperty -Name 'OS_Memory' -Value ([int]$out2) -PassThru

}

$report1 | Export-Csv -Force -Path $reportlocation1 -UseCulture -NoTypeInformation

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I'm not sure why the Select inside the $code.

This one works for me

Get-Folder DEV | get-vm | Where{$_.'PowerState' -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'windows'} | select 'Folder', 'Name',

@{N="IP_Address";E={@($_.guest.IPAddress[0])}},

@{N="OS"; E={@($_.guest.OSFullName)}},

@{N="VM_vCPU"; E={@($_.NumCPU)}},

@{N="VM_Memory(GB)"; E={@($_.MemoryGB)}} | Export-Csv -Path $reportlocation1 -NoTypeInformation -UseCulture

$code = @'

"### Output ###"

(Get-WmiObject Win32_ComputerSystem NumberOfLogicalProcessors).NumberOfLogicalProcessors

"### Output ###"

((Get-WmiObject Win32_ComputerSystem).TotalPhysicalMemory/1GB).ToString("N0")

'@

$report1 = @()

Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $sInvoke = @{

        VM              = $_.Name

        GuestCredential = $Creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $code

    }

    $result = Invoke-VMScript @sInvoke

    $dummy, $out1, $out2 = $result.ScriptOutput -split '### Output ###'

    $out1 = $out1.TrimStart("`n`r")

    $out2 = $out2.TrimStart("`n`r")

    $report1 += $row | Add-Member -MemberType NoteProperty -Name 'OS_CPU' -Value ([int]$out1) -PassThru |

        Add-Member -MemberType NoteProperty -Name 'OS_Memory' -Value ([int]$out2) -PassThru

}

$report1 | Export-Csv -Force -Path $reportlocation1 -UseCulture -NoTypeInformation


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

I'm not sure why the Select inside the $code.

This one works for me

Get-Folder DEV | get-vm | Where{$_.'PowerState' -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'windows'} | select 'Folder', 'Name',

@{N="IP_Address";E={@($_.guest.IPAddress[0])}},

@{N="OS"; E={@($_.guest.OSFullName)}},

@{N="VM_vCPU"; E={@($_.NumCPU)}},

@{N="VM_Memory(GB)"; E={@($_.MemoryGB)}} | Export-Csv -Path $reportlocation1 -NoTypeInformation -UseCulture

$code = @'

"### Output ###"

(Get-WmiObject Win32_ComputerSystem NumberOfLogicalProcessors).NumberOfLogicalProcessors

"### Output ###"

((Get-WmiObject Win32_ComputerSystem).TotalPhysicalMemory/1GB).ToString("N0")

'@

$report1 = @()

Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $sInvoke = @{

        VM              = $_.Name

        GuestCredential = $Creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $code

    }

    $result = Invoke-VMScript @sInvoke

    $dummy, $out1, $out2 = $result.ScriptOutput -split '### Output ###'

    $out1 = $out1.TrimStart("`n`r")

    $out2 = $out2.TrimStart("`n`r")

    $report1 += $row | Add-Member -MemberType NoteProperty -Name 'OS_CPU' -Value ([int]$out1) -PassThru |

        Add-Member -MemberType NoteProperty -Name 'OS_Memory' -Value ([int]$out2) -PassThru

}

$report1 | Export-Csv -Force -Path $reportlocation1 -UseCulture -NoTypeInformation


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much LucD. Smiley Happy

0 Kudos