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.
Related
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
Have a local basic Powershell form for searching and creating VMware virtual machines.
Using new powershell powerCLI module, as described in link
Let's take Get-VM for example:
LOGIC: Type a certain string in TextBox > click search > prints VM's status/parameters in the form
The problem is, I can't execute Get-VM straight away from the script, but first have to connect using Connect-VIServer command and only than Get-VM will work
Is there any way to do it from the script? Something similar to -m flag of commands plink or putty.
Like: Connect-VIServer -server testvc -flagForExample "commands_list.txt"
Yes, you can. Before providing an immediate answer I'd like to explain what is actually happening.
When you call Connect-VIServer the command sets the value of the variable $DefaultVIServer behind the scenes, which is later used by other cmdlets (such as Get-VM).
However, the Get-VM documentation states that there is a Server parameter available. Which means that you can store your server connection in a variable and then pass it to the Get-VM cmdlet.
Here's a pseudo-code example:
$server = Connect-VIServer -server testvc
Get-VM -Server $server
Furthermore, the Get-VM supports an array of servers, so theoretically you can run the cmdlet on multiple servers at once. For example:
$server1 = Connect-VIServer -server testvc
$server2 = Connect-VIServer -server testvc2
Get-VM -Server #($server1, $server2)
is there any way to get all VM's expanded properties and not just the ones Get-azurermvm brings, using powershell?
I was trying to use Select but I don't know where to get all the names. The ones on the Portal's Column won't work (bring back empty fields)
and if I use get-member, they don't bring anything because I think they are nested inside...
As I mentioned in my comment, this is not trivial because an Azure VM is not a self contained thing like a Hyper-V VM. There is no single source of truth because it is made of many components, and you would need to collect the information from those components individually. For example: Getting all of a VM's IP addresses.
To start getting IPs for a machine you get the VM info:
$VM = Get-AzureRmVm -ResourceGroup $RG -Name 'MyUberVM'
Now you can look at the network profile for the VM, which will list the Network Interface objects that are associated with VM, but those have all of 2 properties, Primary and Id. The Primary property is just what it sounds like, it specifies the primary network interface if you have more than one. The Id property will have the full ResourceId for the Network Interface object, something like:
/subscriptions/12345abc-0000-1111-2222-ssl430asd432/resourceGroups/MyVMRG/providers/Microsoft.Network/networkInterfaces/myubervm01715
Now we can use that to get the actual Network Interface object a couple ways, but the simplest is to just run Get-AzureRmResource against it, and pipe that to Get-AzureRmNetworkInterface.
$NIs = $VM.NetworkProfile.NetworkInterfaces.Id|%{Get-AzureRmResource -ResourceId $_}|Get-AzureRmNetworkInterface
That will get you a collection of Network Interface objects. Each of these will have several properties, but the one we care about is the IpConfigurations property. Each IpConfig of each Network Interface will have a PrivateIpAddress property, and a PublicIp property. The PrivateIp property is just a string, so we can use that, but the PublicIp is an object, so we will need to refer to it's IpAddress property. Now, this is going to potentially return more than one of each of those depending on how many network interfaces you have associated with a given VM, so I'll join them with ', ' just to be safe. To break that down, we can do:
$PrivateIps = $NIs.IpConfigurations.PrivateIpAddress -join ', '
$PublicIps = $NIs.IpConfigurations.PublicIp.IpAddress -join ', '
Thus ends the walk-through of getting the IPs for an Azure VM. That takes care of 2 of your suggested columns. Admittedly, two of the harder ones, but still just 2 of them none the less. Once you go through and get all of the data you need for each individual VM I'd make a custom object for it, and output those custom objects to your CSV file.
If my understanding is right, get-azurermvm could get all vms in your subscription, but you could not get detailed properties. You could use Get-AzurermVM get-azurermvm -ResourceGroupName $rg -Name $vm.Name to get detailed properties. You could try to use the following scripts.
##resource group name
$rg="<>"
$vms=get-azurermvm -ResourceGroupName $rg
ForEach ($vm in $vms) {
get-azurermvm -ResourceGroupName $rg -Name $vm.Name
}
Note: You could use get-azurermvm -ResourceGroupName $rg -Status to get VM's status information.
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
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: