VMWare PowerCLI Get DiskUsage of powered off vm's - powershell

I'm creating a script that gets all vm's and shows the DiskSpace. THe Problem is, that if a vm is powered off, it won't show the uesed Space of a disk.
Here are two examples: First one with an VM that is powered on:
PowerCLI C:\> Get-VM sluwv0039
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
sluwv0039 PoweredOn 2 4.000
PowerCLI C:\> $VM = Get-VM sluwv0039
PowerCLI C:\> $VM.guest.disks
CapacityGB FreeSpaceGB Path
---------- ----------- ----
49.997 5.417 C:\
Example two where the VM is powered off:
PowerCLI C:\> Get-VM sluwv0012
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
sluwv0012 PoweredOff 4 8.000
PowerCLI C:\> $VM = Get-VM sluwv0012
PowerCLI C:\> $VM.guest.disks
PowerCLI C:\>
Note: The Last line is the output. There is no "CapacityGB" etc.

Correct, that property is reading from the guest file system to see how much space is left on the partition. In your case, the C:\ drive. If the VM is off, there's no way for PowerCLI to find that property.
Alternatively, you could look at the $vm.ExtensionData.Summary.Storage properties and do some rough conversions. Note: the output of those are in byte, so you'll want to convert them to GB. Example: $tempVM.ExtensionData.Summary.Storage.Committed / 1GB
It won't be exact, but it will be better than no output at all.

here is example of script to show vm specification:
Get-Vm | Select-Object Name,PowerState,VMHost,NumCPU,MemoryGB,ProvisionedSpaceGB,#{N="HostName";E={#($.guest.HostName)}},#{N="Gateway";E={#($.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress[0])}},#{N="DNS";E={$.ExtensionData.Guest.IpStack.DnsConfig.IpAddress}},#{N="IPAddress";E={#($.guest.IPAddress -like "192.168.*")}},#{N="Nics";E={#($.guest.Nics)}},#{N="Datastore";E={#($ | Get-DataStore)}},#{N="Disks";E={#($.guest.Disks)}},Version,#{N="State";E={#($.guest.State)}},#{N="OS";E={#($_.guest.OSFullName)}}
the sample output is like this:
Name State VMHost NumCpu MemoryGB PowerState ProvisionedSpaceGB Version IPAddress HostName OS Nics Disks VMwareTools Gateway DNS
test Running 192.168.32.100 2 1 PoweredOn 43.1085147 v8 192.168.122.1 Elenoon Ubuntu Linux (64-bit) Network adapter 1:VM Network Network adapter 2:local : : Capacity:17167286272, FreeSpace:14212493312, Path:/ Capacity:15188623360, FreeSpace:15154872320, Path:/media/files Capacity:10724835328, FreeSpace:10672824320, Path:/var/log Capacity:973770752, FreeSpace:690139136, Path:/boot guestToolsRunning 127.0.0.1
hope to be useful ;)

Related

Why the select -first 1 doesn't work in this PowerShell command?

I'm testing in a Windows Failover cluster environment. Below is my code.
PS C:\Users\administrator.DEV> Get-ClusterResource *disk*
Name State OwnerGroup ResourceType
---- ----- ---------- ------------
Cluster Disk 1 Online Available Storage Physical Disk
Cluster Disk 2 Online Cluster Group Physical Disk
Cluster Disk 3 Online Available Storage Physical Disk
PS C:\Users\administrator.DEV> (Get-ClusterResource *disk*).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
PS C:\Users\administrator.DEV> (Get-ClusterResource *disk*)[0].GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False ClusterResource Microsoft.FailoverClusters.PowerShell.ClusterObject
PS C:\Users\administrator.DEV>
As you can see, I have three disk resource. But when I want to get the first one with the select cmdlet, I got empty output.
PS C:\Users\administrator.DEV> Get-ClusterResource *disk* | select -First 1
PS C:\Users\administrator.DEV>
Why this behavior? How can I get the first disk resource in this case?
Apparently, Get-ClusterResource exhibits nonstandard behavior by emitting an array of results as a whole (as a single object) rather than enumerating it, i.e. emitting its elements one by one (the latter is what cmdlets are generally expected to do).
Therefore, either use (Get-ClusterResource *disk*) | select -First 1 (note the (...) to force enumeration of the array), or - as you're already showing - simply index directly into the array: (Get-ClusterResource *disk*)[0]

List all services in PowerShell

There are some services in Windows (such as http and USBStor) which are not listed when you view Services, or when running the Get-Service cmdlet. What is the simplest way to list all services, even the hidden or unlisted ones?
For example, the http and USBStor services are not enumerated when listing services, but they can be accessed directly by name:
PS C:\Windows\System32> Get-Service | Where-Object {"http","usbstor","spooler" -contains $_.Name}
Status Name DisplayName
------ ---- -----------
Running Spooler Print Spooler
PS C:\Windows\System32> Get-Service "http","usbstor","spooler"
Status Name DisplayName
------ ---- -----------
Running http HTTP Service
Running spooler Print Spooler
Stopped usbstor USB Mass Storage Driver
This might not be the most elegant way of getting all the services (hidden per say), but this will give you all the services along with ones these are dependent on.
Get-Service -RequiredServices | select -Unique DisplayName | ? {$_.DisplayName -like "Http*" }
Try 'Get-CimInstance'.
Such functions (Get-Service) delivered by Microsoft rely on and use CIM/Win32 classes.
(Get-Service only shows Windows services. 'HTTP' is a system driver.)
Get-CimInstance 'CIM_Service'

Hyper-V changing Static MAC address on one of the adapters

I have an Ubuntu 18.06 VM with 2 network adapters connected, one internal and one external. However, I would like set the adapters which is using the internal switch "NATSwitch" to have a static MAC address while the other can have a dynamic one.
PS C:\> Get-VMNetworkAdapter -VMName "Docker_1806"
Name IsManagementOs VMName SwitchName MacAddress Status IPAddresses
---- -------------- ------ ---------- ---------- ------ -----------
Network Adapter False Docker_1806 Robot Switch 001122334455 {Ok} {169.254.74.100, fe80::215:5dff:feb2:1188}
Network Adapter False Docker_1806 NATSwitch 554433221100 {Ok} {192.168.137.100, fe80::a00:3cff:fea3:4209}
I have tried using this command:
PS C:\> Set-VMNetworkAdapter -VMName "Docker_1806" -StaticMacAddress "01-02-03-04-05"
But that seemed to have changed both of my adapter's MAC address to static.
I've tried:
PS C:\> $vmadapter = Get-VMNetworkAdapter -VMName "Docker_1806"
PS C:\> $vmadapter[1]
Name IsManagementOs VMName SwitchName MacAddress Status IPAddresses
---- -------------- ------ ---------- ---------- ------ -----------
Network Adapter False Docker_1806 NATSwitch 554433221100 {Ok} {192.168.137.100, fe80::a00:3cff:fea3:4209}
PS C:\> Set-VMNetworkAdapter -VMName "Docker_1806" -VMNetworkAdapter $vmadapter[1] -StaticMacAddress "01-02-03-04-05"
Set-VMNetworkAdapter : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ set-vmnetworkadapter -vmname "Docker_V2_Dev" -VMNetworkAdapter $vmada ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-VMNetworkAdapter], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.HyperV.PowerShell.Commands.SetVMNetworkAdapter
Is there a way to change only one of them through Powershell on the host? I can't seem to find a way to set static MAC for a specific adapter for that VM.
On another note, the network configurations for this VM is set using netplan configurations inside the VM so that it can have static IP addresses.
Thanks in advance!
You should choose which of parameters sets you would like to use - searchinf by VMName or VMNetworkAdapter object.
More information about different parameter sets you could find in the doc:
https://learn.microsoft.com/en-us/powershell/module/hyper-v/set-vmnetworkadapter?view=win10-ps
$vmadapter = Get-VMNetworkAdapter -VMName "Docker_1806"
Set-VMNetworkAdapter -VMNetworkAdapter $vmadapter[1] -StaticMacAddress "01-02-03-04-05"
This one worked for me.
Change MAC address (Properties->Configure...->Network Address) in Default switch properties. In my case i see incorrectly set MAC address with dashes (maybe set while default Hyper-V installation).

Why is Get-DnsClientServerAddress | select AddressFamily output not IPv4 and IPv6

When I type the cmdlet Get-DnsClientServerAddress I get all the interfaces my PC has like for example
InterfaceAlias Interface Address ServerAddresses
Index Family
-------------- --------- ------- ---------------
Ethernet 7 IPv4 {10.10.15.40, 10.10.25.44}
So when I type in Get-DnsClientServerAddress | where AddressFamily -Like "4" I would expect to see the Ethernet Adapter.
But for any reason it didn't show up. So I typed Get-DnsClientServerAddress | select AddressFamily and what I got was
AddressFamily
-------------
2
23
2
23
Can anyone explain this to me ?
As you found, the AddressFamily is categorised internally using a (not obvious) numbering scheme, where IPv4 addresses are type '2'. This comes from the underlying WMI type (MSFT_DNSClientServerAddress) and is not an issue with PowerShell.
The default display helps you out by translating this to IPv4, etc, but you can't filter on that as it's for display only. You can, however, still filter if you use the correct value:
Get-DnsClientServerAddress | Where-Object AddressFamily -Like 2
This formatting of data for display purposes happens all the time in PowerShell and is acheived through Format.ps1xml files. For example, compare the output of the Working Set values from Get-Process in table and list format:
PS C:\> Get-Process powershell
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
662 31 97928 110256 1.27 11452 2 powershell
PS C:\> Get-Process powershell | Format-List *
Handles : 705
VM : 2204040044544
WS : 113082368
PM : 100356096
NPM : 31512
The property (itself added by PowerShell for convenience) is called WS, but is shown as WS(K) in the table and the actual value is stored in bytes, but is displayed in KB, so some manipulation is going on for the default output.
Following from my comment, I would use Get-NetIPAddress instead.
Get-NetIPAddress -InterfaceAlias "Ethernet" | Select-Object FamilyAddress

How to get all the network names of a VM using powershell

Am new to powershell.I have to get the names of all network a VM can connect to.So, please can anyone help me out.
If your using Hyper-V you can try to install PowerShell Management Library for Hyper-V
http://pshyperv.codeplex.com/
Or you have to find a WMI you can use
Get-WmiObject win32_NetworkAdapter
In PowerCLI, you can use the Get-VirtualPortGroup command:
Get-VirtualPortGroup ESXi.FQDN.local
Name Key VirtualSwitch VLanId
---- --- ------------- ------
VM Network key-vim.host.PortGroup-VM N... vSwitch0 0
Management Network key-vim.host.PortGroup-Mana... vSwitch0 0