Automation

 View Only
  • 1.  Error while getting output from invoke-VMScript

    Posted Jun 22, 2020 05:34 PM

    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



  • 2.  RE: Error while getting output from invoke-VMScript
    Best Answer

    Posted Jun 22, 2020 06:04 PM

    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



  • 3.  RE: Error while getting output from invoke-VMScript

    Posted Jun 23, 2020 06:03 AM

    Thank you very much LucD. :smileyhappy: