0

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.

pointysparrow
  • 23
  • 1
  • 3
  • 1
    replace is NOT a valid cmdlet, exe, function, or anything else that would work in that location. [*grin*] – Lee_Dailey Feb 23 '20 at 13:38

3 Answers3

1

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.

Theo
  • 57,719
  • 8
  • 24
  • 41
  • thanks very much for your help, weirdly though now that this is fixed I have a new problem. On my PC it worked so I copied it to my server and ran the same commands, it's prompting me to enter a parameter for Resource Group, and I can't leave it blank. I checked the version of the AzureRM module and it's identical on my PC and the server. Seriously confused now. – pointysparrow Feb 23 '20 at 22:34
  • @pointysparrow The environment can be completely different when on server as opposed to your local machine. Try cmdlet `Get-AzResourceGroup` to get a list of all ResourceGroups under your subscription, Pick the one you need and add that as `-ResourceGroupName` parameter to the `Get-AzureRmVM` in your code. More on [ResourceGroups](https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-powershell) – Theo Feb 24 '20 at 11:14
1

Replace is a windows command to replace files. It's not what you want. https://ss64.com/nt/replace.html

js2010
  • 23,033
  • 6
  • 64
  • 66
0

Replace is not a statement, cmdlet, function, alias or executable program as @Lee_Dailey pointed. There is a replace operator to replace with regex and replace method to do that without Regex. (It is surprising to me that it didn't even show an error). So here is no replace with Regex so I recommend replacing by replace method:

$vms = Get-AzureRmVM -status | select-object name,@{ n='IP Address'; e={"IP removed"}}, @{ n='OsType';
e={$_.StorageProfile.OsDisk.OsType}}, @{n='powerState';e={$_.powerState.ToString().Replace("VM Running", "poweredOn")}}
$vms
Wasif
  • 14,755
  • 3
  • 14
  • 34