1

I have a code:

$x = Invoke-AzVMRunCommand -ResourceGroupName 'testrg1' -Name 'sqlvm1'-CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\joe\OneDrive - Microsoft\Documents\joe\Miscellaneous\run_sqlserver_script.ps1' -Parameter @{"VmName" = 'sqlvm1'}

Output of $x: enter image description here

Type of X: enter image description here

I want the stuff in the Message to be in an array so that I can access each value. But right now $x.Value[0].Message.gettype() is a String. When I run $x.Value[0].Message.ComputerName nothing shows up. How do I convert the stuff in the Message from String to Array?

enter image description here enter image description here enter image description here

angel101
  • 45
  • 4
  • you will have to post the contents of the script `run_sqlserver_script.ps1`...its probably either converting or assigning string values to the message property. – Kiran Reddy Jul 30 '21 at 06:06
  • Or [ConvertTo-Json](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertto-json) or [ConvertTo-Expression](https://www.powershellgallery.com/packages/ConvertTo-Expression) `$x` and add the results (as text) to the question. – iRon Jul 30 '21 at 06:28
  • @Kiran This is the content ```#Calling Named Parameters param([string]$VmName) #Invoke SQL Script on the VMs Invoke-Sqlcmd -ServerInstance $VmName -query "SELECT SERVERPROPERTY('MachineName') AS 'VirtualMachineName', SERVERPROPERTY('ServerName') AS 'ServerName', SERVERPROPERTY('Edition') AS 'Edition', SERVERPROPERTY('EngineEdition') AS 'EngineEdition', SERVERPROPERTY('ProductVersion') AS 'ProductVersion', SERVERPROPERTY('ProductLevel') AS 'ProductLevel'" ``` – angel101 Jul 30 '21 at 22:43

1 Answers1

1

If as you show $x.Value[0].Message.gettype() is a string looking like the output of Format-List

ComputerName     : sqlvm1
InstanceName     : sqlvm1
Edition          : Developer Edition (64-bit)
EngineEdition    : 3
ProductVersion   : 15.0.4138.2
ProductLevel     : RTM

you can parse that out like:

($x.Value[0].Message -replace '(?<!:.*):', '=' | ConvertFrom-StringData).ComputerName

Regex details on the -replace to replace only the first occurrence of the colon:

(?<!        Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind)
   :        Match the character “:” literally
   .        Match any single character
      *     Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)          
:           Match the character “:” literally
Theo
  • 57,719
  • 8
  • 24
  • 41