Azure Powershell Query to return VM details - powershell

Im trying to write a powershell query to return the following table:
NAME STATUS RESOURCE GROUP VMSIZE SIZEREASON (TAG) LOCATION PUBLIC DNS NAME
I can use Get-AzureRmVM | Select-Object Name, ResourceGroupName, Location but when I try to add on VmSize, the column is there but without values.
If I run just Get-AzureRmVM then I get the VmSize, so I don't understand why I can't select it.
I also need to work out how to add in Status, SIZEREASON (TAG),PUBLIC DNS NAME
Any guidence is most appreciated.
Its also worth noting, that my table also needs to contain both Resource Managed VM's and Classic VM's (So all VM's)
UPDATE
My Powershell query is currently:
Get-AzureRmVm `
| Select-Object ResourceGroupName `
, Name `
, Location `
, #{Name="VmSize"; Expression={$_.ToPSVirtualMachine().HardwareProfile.VmSize}} `
, #{Name="SizeReason(Tag)"; Expression={$_.ToPSVirtualMachine().Tags.Values}} `
, FullyQualifiedDomainName `
| Format-Table
The outstanding problems are:
FullyQualifiedDomainName does not work
I am also missing the status (Eg: Running, Stopped (Deallocated) etc..)

This was trickier than I expected....
Get-AzureRmVm | Select-Object #{Name="VmSize"; Expression={$_.ToPSVirtualMachine().HardwareProfile.VmSize}}

Related

Azure Powershell VM Stopped status in variable

I am trying to write a PowerShell script that will look for shutdown VMs in my Resource Group and deallocate them. The output of the below script does not give me the VM name "clean" when I attempt tp assign the below as a variable. The end result is to execute the Stop-AzureRmVM -ResourceGroupName LAB -Name $VM -force
So for more context, lets say AVGJOE-DC1 is in a stopped state and I run the below line in Azure Powershell it will display
Name
----
AVGJOE-DC1
If I then if I tried to use $VM to call AVGJOE-DC1 in the
Stop-AzureRmVM -ResourceGroupName LAB -Name $VM -force
it fails due to the variable being set to a longer string something like
MicroSoftComputerResource\Resourcegroup[#Name=AVGJOE-DC1].
Hopefully that makes sense.
$VM = Get-AzureRmVM -ResourceGroupName LAB | get-azurermvm -Status | ?{$_.statuses.displaystatus -eq "VM stopped"} | select name
Just like #Theo said in the comment, select name gives you an object with property name. If you want the string value of the name property, you can use Select-Object -ExpandProperty name instead of select name.

SCOM 2012 Windows Service Monitoring Target object

How can I find name of group targeted a windows service monitor which created with template using Powershell?
I mean, I created a monitor in scom using template, I am checking a service. I targeted the monitor to a group. I want to find group name with Powershell.
I wants to get target information on windows service template. How can I get this using Powershell?
To retrieve the target ID for 'All Windows Computers' , execute $targetID = Get-SCOMClassInstance -Class (Get-SCClass | Where-Object DisplayName -Like 'All Windows Computers') | Select ManagementPackClassIds.
Then use the target ID to query ' Get-SCOMMonitor | Select-Object -First 1 | where-object DisplayName -eq 'Queries With High Fetches' | where-object Target -like 'ManagementPackElementUniqueIdentifier=$targetID*'` and see what you get. This is just how you could go about it. Please note that you might have to perform the some string interpolation to get this to work with $targetID.

Where is the definition for object that Get-AzureVm returns?

Where do we find the definitions for the objects returned by Powershell commmands like Get-AzureVm?
For instance if I do a Get-AzureVm I simply get a table with a single row in it with only 3 columns
[ServiceName, Name, Status]
But code I've 'borrowed' off the net can do so much more:
Get-AzureVM | Format-List Name, IpAddress, DNSName, InstanceSize, PowerState
I understand the Powershell commands for Format-List etc but I don't know where the list of available fields like IpAddress, DNSName, InstanceSize, PowerState is defined.
I would have thought the Get-AzureVm MSDN page would tell me but no, nothing.
I also tried the Azure REST MSDN Get information about a virtual machine which listed a bunch of properties coming back from a REST call, most of which are ignored if i append them to the command above.
They are also missing fields like IpAddress so this can't be the definitive/proper list.
So where can I find this info? Seems like such common/basic knowledge that no one has bothered to write it down.
Use the Get-Member cmdlet to retrieve all available properties:
Get-AzureVm | Get-Member -MemberType Property | select Name
Output:
AvailabilitySetName
DeploymentName
DNSName
GuestAgentStatus
HostName
InstanceErrorCode
InstanceFaultDomain
InstanceName
InstanceSize
InstanceStateDetails
InstanceStatus
InstanceUpgradeDomain
IpAddress
Label
Name
NetworkInterfaces
OperationDescription
OperationId
OperationStatus
PowerState
PublicIPAddress
PublicIPDomainNameLabel
PublicIPFqdns
PublicIPName
ResourceExtensionStatusList
ServiceName
Status
VirtualNetworkName
VM
You can also use Format-List to see the properties of your object, try this:
Get-AzureVm | Format-List * -Force

PowerCLI Get-VM filter out blank IP Address

I need to filter out the blanks in the "IP Address" column of the below PowerCLI script but am having a hard time figuring it out.
Here's the core script.
Get-VM | select Name, #{N="IP Address";E={#($_.guest.IPAddress[3])}}, Guest, PowerState
Here's one of the scripts I've tried and failed at.
Get-VM | select Name, #{N="IP Address";E={#($_.guest.IPAddress[3])}}, Guest, PowerState | where-object { #{N="IP Address";E={#($_.guest.IPAddress[3])}} -ne "" }
Any help would be appreciated.
Thank you,
-Rob
You have already made the customer property. You can now use it directly for filtering
Get-VM | select Name, #{N="IP Address";E={#($_.guest.IPAddress[3])}}, Guest, PowerState | where-object{$_."IP Address"}
or
Get-VM | Where-Object{$_.guest.IPAddress[3]} | select Name, #{N="IP Address";E={#($_.guest.IPAddress[3])}}, Guest, PowerState
If the property is null or empty the that will evaluate to false. In your example you were evaluating the creation of hash table. First example checks your newly created IP Address property. Second checks the source data for that property. Either way they should evaluate the same.

How to check if a particular service is running in a remote computer using PowerShell

I have 3 servers, running 3 services:
SERVER-A running serv1.exe service
SERVER-B running serv2.exe service
SERVER-C running serv3.exe service
Using PowerShell how can I query each service to check if they are running and output the information to a text file?
If they all have the same name or display name you can do it in one command. If not you need to run 3 commands.
If all have the same name or display name:
Get-Service -ComputerName server-a, server-b, server-c -Name MyService |
Select Name, MachineName, Status
If they have different names or display names:
I would do this --
#{
'server-a' = 'service-a'
'server-b' = 'service-b'
'server-c' = 'service-c'
}.GetEnumerator() | ForEach-Object {
Get-Service -ComputerName $_.Name -Name $_.Value
} | Select Name, MachineName, Status
To output to a text file use ... | Set-Content ~\Documents\Service_Status.txt where ... is one of the above.
Note - your account will need to have privileges to query the remote machines.
There are several ways to achieve this. I am using a hash of the values since you mentioned that the server to service mapping is always one to one.
$svrHash = #{"SERVER-01"="winmgmt";"SERVER-02"="Wecsvc"}
$svrHash.Keys
| ForEach-Object {Get-Process -ComputerName $_ -Name $svrHash[$_] -Ea SilentlyContinue}
| Select ProcessName
| Out-File C:\Scripts\test.txt
You need to use the service name and not the .exe name.