VMware Cloud Community
Gr33nEye
Enthusiast
Enthusiast

Invoke Script Output Issues

Hi All,

I'm hoping someone can help, I am trying to get outputs from a Invoke-VMScript to be used as inputs further on. Whilst the script works and gives the outputs I'm having issues in formating them to be used as inputs.

The script is just a basic get-disk but i would like to be able to pipe the Number and OperationalStatus into a bigger Invoke-VMScript.

Any help would be appreciated.

$disks =  Invoke-VMScript -VM BNESTGFS001 -ScriptText 'get-disk' -ScriptType PowerShell

($disks).scriptoutput

Number F Serial Number                    HealthStatus         OperationalStatu

       r                                                       s

       i

       e

       n

       d

       l

       y

       N

       a

       m

       e

------ - -------------                    ------------         ----------------

0      V                                  Healthy              Online

1      V                                  Healthy              Offline

2      V                                  Healthy              Offline

3      V                                  Healthy              Offline

4      V                                  Healthy              Offline

0 Kudos
14 Replies
LucD
Leadership
Leadership

Try something like this

$vmName = 'MyVM'

$pattern = '(?m)^(?<devnumber>(?:[0-9]+)+)[\w\s]+?(?<status>(?:\w+)+)\s+\d+[\w\s]+$'


$result = Invoke-VMScript -VM $vmName -ScriptText 'Get-Disk' -ScriptType Powershell

$result.ScriptOutput.Split("`r") | Select-String -Pattern $pattern |

Select @{N='Number';E={$_.Matches[0].Groups['devnumber'].Value}},

  @{N='Status';E={$_.Matches[0].Groups['status'].Value}}


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

0 Kudos
Gr33nEye
Enthusiast
Enthusiast

Thnx, not sure how to read the output.

PS C:\WINDOWS\system32> $disks =  Invoke-VMScript -VM "VM" -ScriptText 'get-disk' -ScriptType PowerShell

PS C:\WINDOWS\system32> $pattern = '(?m)^(?<devnumber>(?:[0-9]+)+)[\w\s]+?(?<status>(?:\w+)+)\s+\d+[\w\s]+$'

PS C:\WINDOWS\system32> $pattern

(?m)^(?<devnumber>(?:[0-9]+)+)[\w\s]+?(?<status>(?:\w+)+)\s+\d+[\w\s]+$

PS C:\WINDOWS\system32> $disks.ScriptOutput.Split("`r") | Select-String -Pattern $pattern | Select @{N='Number';E={$_.Matches[0].Groups['devnumber'].Value}}, @{N='Status';E={$_.Matches[0].Groups['status'].Value}}

PS C:\WINDOWS\system32> $stuff = $disks.ScriptOutput.Split("`r") | Select-String -Pattern $pattern | Select @{N='Number';E={$_.Matches[0].Groups['devnumber'].Value}}, @{N='Status';E={$_.Matches[0].Groups['status'].Value}}

PS C:\WINDOWS\system32> $stuff

PS C:\WINDOWS\system32>

0 Kudos
LucD
Leadership
Leadership

What is the exact output of the Invoke-VMScript cmdlet (the ScriptOutput part).
Is that what you showed in the beginning of this thread?

For me it seems to work

get-disk.jpg


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

0 Kudos
Gr33nEye
Enthusiast
Enthusiast

yep, that's all i get.

Pic

failing1.png

0 Kudos
LucD
Leadership
Leadership

Can you eventually attach the ScriptOutput content in a .txt file?


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

0 Kudos
Gr33nEye
Enthusiast
Enthusiast

Tried to change the status to OperationalStatus

failing2.png

0 Kudos
Gr33nEye
Enthusiast
Enthusiast

sure,

you mean this

*EDIT*, that was meant to be a txt file. Why did it zip it? I've also attached the actual script

0 Kudos
LucD
Leadership
Leadership

Thanks, attachments are always zipped.

Wondering why you get the output in this compressed format.

That is also the reason why my RegEx doesn't work for you.

In my testing the output appears as

Number Friendly Name                            Operationa Total Size Partition

                                                lStatus                Style  

------ -------------                            ---------- ---------- ---------

1      VMware Virtual disk SCSI Disk Device     Online           2 GB MBR     

0      VMware Virtual disk SCSI Disk Device     Online          60 GB MBR     

10     VMware Virtual disk SCSI Disk Device     Online          40 GB MBR     

7      VMware Virtual disk SCSI Disk Device     Offline         30 GB MBR


Is the line-width on that target station set to a specific value?
What is the content of $Host.UI.RawUI?

You can eventually try to change it with

$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 25)


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

0 Kudos
Gr33nEye
Enthusiast
Enthusiast

Hmm, something seems wrong. Will investigate.

PS C:\WINDOWS\system32> $Host.UI.RawUI

ForegroundColor       : DarkYellow

BackgroundColor       : DarkMagenta

CursorPosition        : 0,61

WindowPosition        : 0,13

CursorSize            : 25

BufferSize            : 120,3000

WindowSize            : 120,50

MaxWindowSize         : 120,73

MaxPhysicalWindowSize : 274,73

KeyAvailable          : False

WindowTitle           : Administrator: Windows PowerShell

PS C:\WINDOWS\system32> $Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 25)

Exception setting "BufferSize": "Cannot set the buffer size because the size specified is too large or too small.

Parameter name: value

Actual value was 500,25."

At line:1 char:1

+ $Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Siz ...

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

    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException

    + FullyQualifiedErrorId : ExceptionWhenSetting

0 Kudos
LucD
Leadership
Leadership

Is that from the station where you run the Get-Disk, or from the station from where you run the Invoke-VMScript?


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

0 Kudos
Gr33nEye
Enthusiast
Enthusiast

Station,You mean like laptop? yes,

That was just form the Powershell window. Here is the ISE version

PS C:\WINDOWS\system32>  $Host.UI.RawUI

ForegroundColor       : -1

BackgroundColor       : -1

CursorPosition        : 0,0

WindowPosition        :

CursorSize            :

BufferSize            : 181,0

WindowSize            :

MaxWindowSize         :

MaxPhysicalWindowSize :

KeyAvailable          :

WindowTitle           : Administrator: Windows PowerShell ISE

PS C:\WINDOWS\system32>  $Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 25)

PS C:\WINDOWS\system32> $result.ScriptOutput

Number F Serial Number                    HealthStatus         OperationalStatu

       r                                                       s              

       i                                                                      

       e                                                                      

       n                                                                      

       d                                                                      

       l                                                                      

       y                                                                      

                                                                              

       N                                                                      

       a                                                                      

       m                                                                      

       e                                                                      

------ - -------------                    ------------         ----------------

0      V                                  Healthy              Online         

1      V                                  Healthy              Online         

2      V                                  Healthy              Online         

3      V                                  Healthy              Online         

4      V                                  Healthy              Online         

PS C:\WINDOWS\system32>

0 Kudos
LucD
Leadership
Leadership

No, I mean on the VM, the one where you run the Get-Disk.
Just display the content of $Host.UI.RawUI with Invoke-VMScript.
That's most probably where the settings make the output such a compressed format.


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

0 Kudos
Gr33nEye
Enthusiast
Enthusiast

Ok, So when i run the 'Get-Disk' localy on the VM it outputs the correctly. However when I do it via an Invoke it outputs the messed up version.

The top output is from the VM's console. The bottom is from my PC.

failing3.png

0 Kudos
LucD
Leadership
Leadership

Can you try sending the following instead?

Get-Disk | Out-String -Width 132


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

0 Kudos