Search code examples
powershellreplaceoutputnulazure-rm

Piping into replace discards all my data?


I'm running the following command that returns a long list of virtual machines:

$vms = Get-AzureRmVM -status | select name,@{ n='IP Address'; e={"IP removed"}}, @{ n='OsType';
e={$_.StorageProfile.OsDisk.OsType}}, powerState
$vms

When I pipe into replace, there is literally no data left..

$vms = Get-AzureRmVM -status | select name,@{ n='IP Address'; e={"IP removed"}}, @{ n='OsType';
e={$_.StorageProfile.OsDisk.OsType}}, powerState | replace "VM Running", "poweredOn"
$vms

I get nothing back.


Solution

  • You need to change the last part of your Select-Object line from

    powerState | replace "VM Running", "poweredOn"
    

    into:

    @{Name = 'powerState'; Expression = { $_.powerState -replace "VM Running", "poweredOn"}}
    

    To create another calculated property.