Getting Azure VM OS name using PowerShell - powershell

I have been trying to get the VM OS name from Microsoft Azure using PowerShell.
I think I am very close to the solution but I don't know where I'm going wrong.
This is the command that I am using to get the VM details:
Get-AzureRmVM -ResourceGroupName TEST -Name VF-Test1 | Select OsType
The answer I get is just blank.
When running the following command:
Get-AzureRmVM -ResourceGroupName TEST -Name VF-Test1
I get all the details that belong to that VM.

The osType property lives inside $_.StorageProfile.osDisk
Get-AzureRmVM -ResourceGroupName TEST -Name VMNAME |
Format-Table Name, #{l='osType';e={$_.StorageProfile.osDisk.osType}}
Name osType
------ ------
VMNAME Windows
Use https://resources.azure.com to explore the object representation when in doubt, or pipe to Show-Object, like i did below.

You can get resource groups' VMs by Get-AzureRmVM and classic VMs by Get-AzureVM. Both of the returning values of the two cmdlets contain OS type properties but in different paths.
For the cmdlet Get-AzureRmVM, the OS type property path is $vm.StorageProfile.OsDisk.OsType
For the cmdlet Get-AzureVM, the OS type property path is $vm.VM.OSVirtualHardDisk.OS
There exists a sample code about fetching Azure VM OS Type here: https://gallery.technet.microsoft.com/How-to-retrieve-Azure-5a3d3751

Get-AzVM -name SERVERNAME | select name, #{n="OS";E={$_.StorageProfile.OsDisk.OsType}}, #{n="Offer";E={$_.StorageProfile.ImageReference.offer}} , #{n="SKU";E={$_.StorageProfile.ImageReference.sku}}, #{n="Publisher";E={$_.StorageProfile.ImageReference.Publisher}}
RESULT:

Related

PowerCLI - Get VM Disk Partition Type

I'm looking to conduct an audit on our virtual environment to get the disk partition types (MBR, GPT) of our VMs. I haven't found any documentation in PowerCLI to get the partition type. Any ideas how I can go about this? Thanks!
That sort of information is normally not known at the VM object level and instead known at the Guest-OS level. If the VMs you're working with have VMware Tools (or Open VM Tools), you can still use PowerCLI to run scripts against them to pull that information with Invoke-VMScript (docs), but you'll still need to write your own code to pass to the guest OS to pull partition type.
If they're windows systems, you may be able to do something as simple as:
Invoke-VMScript -ScriptText {Get-Partition | select DriveLetter, Type} -VM VMName -GuestCredential $guestCredential
Thanks #Kyle Ruddy!
This was what I did:
$vmName = "VM NAME"
$output = Invoke-VMScript -ScriptText {Get-Disk | select Number, #{name='Size (GB)';expr={[int]($_.Size/1GB)}}, PartitionStyle} -VM $vmName -GuestUser $Username -GuestPassword $Password
$output.ScriptOutput | FT -AutoSize

Enable-AzureWebsiteApplicationDiagnostic: specify storage account name?

How to enable diagnostics for a slot to Azure Table Storage?
I try:
> Enable-AzureWebsiteApplicationDiagnostic -Name mysite `
-LogLevel Verbose -Slot myslot -TableStorage `
-StorageAccountName 'mystorageaccountname'
fails:
ResourceNotFound: The storage account 'mystorageaccountname' was not found.
But it exists.
And when I do:
> Set-AzureRmCurrentStorageAccount –ResourceGroupName mygroup `
–StorageAccountName mystorageaccountname
> Enable-AzureWebsiteApplicationDiagnostic -Name mysite `
-LogLevel Verbose -Slot myslot -TableStorage
it fails:
Value cannot be null. Parameter name: accountName
whereas the doc says:
-StorageAccountName
Specifies the name of a storage account in which to store logs. The default value is the current storage account.
> gcm Enable-AzureWebsiteApplicationDiagnostic
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Enable-AzureWebsiteApplicationDiagnostic 5.3.0 azure
> get-module azure*
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 5.3.0 azure {Add-AzureAccount, Add-AzureApplicationGatewaySslCertif...
Script 4.3.2 Azure.Storage {Disable-AzureStorageDeleteRetentionPolicy, Enable-Azur...
Script 5.3.3 AzureRM.Profile {Add-AzureRmEnvironment, Clear-AzureRmContext, Clear-Az...
Script 5.0.0 AzureRM.Storage
Update. The same issue for the site.
> Enable-AzureWebsiteApplicationDiagnostic -Name mysite `
-LogLevel Verbose -BlobStorage -StorageBlobContainerName appslogs `
-StorageAccountName 'mystorageaccountname'
I'm not sure the cause of this error. But I've found the similar thread in MSDN which you are experiencing now.
From that David Ebbo mentioned like
The old Enable-AzureWebsiteApplicationDiagnostic CmdLet may have
issues. Please try something like this instead. Please look at what
the config/log API returns in https://resources.azure.com/ to see
everything that you can pass in there.
I didn't find how to make Enable-AzureWebsiteApplicationDiagnostic work. And it seems that this Cmdlet can be removed. But Set-AzureRmResource was used to set the diagnostic for the site using Azure template. Please see full script at https://stackoverflow.com/a/51617949/511144

How to Get MAC Address of VMs with Azure PowerShell

Does anyone know how to get Mac address of vms in Azure through Azure PowerShell?
i know i can get it with WMI or something else inside the VM, but i don't know how can i do that without logging on the VM.
Use the Get-AzureRmNetworkInterface command and the MacAddress property the resulting object has:
(Get-AzureRmNetworkInterface -ResourceGroupName %rgName%).MacAddress
this will list all the macs of the network interfaces in a resource group, to be more specific you could add the -Name parameter.
(Get-AzureRmNetworkInterface -ResourceGroupName %rgName% -Name %nicName%).MacAddress

Connects a virtual network adapter to a virtual switch

How can connect a virtual switch to a virtual machine from powershell by Ids?
I tried:
# get virtual machine object
$vm = get-vm -Id '...id...'
# get virtual switch object
$vs = get-switch -Id '...id...'
# connect both
connect-vmnetworkadapter -vm $vm -vmswitch $vs
I want to connect VM to VS by Ids because the name is not unique.
I get the error:
Connect-VMNetworkAdapter : Parameter cannot be processed because the parameter name 'vm'
is ambiguous. Possible match include: -VMName -VMNetworkAdapter -VMSwitch -Name.
The error message is actually rather self-explanatory. The parameter -VM is ambiguous, i.e. the Connect-VMNetworkAdapter cmdlet has more than one parameter beginning with VM. Use the parameter -VMName with the name of the VM:
Connect-VMNetworkAdapter -VMName $vm.Name -VMSwitch $vs
If you can't use the name you must read the adapter from a pipeline, since AFAICS Connect-VMNetworkAdapter doesn't accept VM objects as input.
$vm.NetworkAdapters | Connect-VMNetworkAdapter -VMSwitch $vs
You may need to select the right adapter, if the VM has more than one, though.

How to get an instance of Msvm_VirtualSystemGlobalSettingData class for particular VM in Powershell

So, I've been trying my hand at powershell automation of Hyper-V VM administration, and I can't seem to find a reliable way to find the VM Data Root for a given VM. I understand that when programmatically building one, I set it via the Msvm_VirtualSystemGlobalSettingData WMI class. How do I access this class post VM creation?
Thanks!
The GetRelated() method is the key:
PS C:\> $vm = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter 'ElementName="SomeVM"'
PS C:\> $vm.GetRelated("Msvm_VirtualSystemGlobalSettingData")