Compiling DSC Config that contains Get-NetAdapter in Azure Automation - powershell

As seen in the many Azure Quick Start examples, it is common to use Get-NetAdapter to get the Network Interface Name for things like DNS configuration. This is an example:
configuration MyConfig
{
$Interface=Get-NetAdapter|Where Name -Like "Ethernet*"|Select-Object -First 1
$InterfaceAlias=$($Interface.Name)
Node localhost
{
xDnsServerAddress DnsServerAddress
{
Address = $DNSServer
InterfaceAlias = $InterfaceAlias
AddressFamily = 'IPv4'
}
}
}
If the command Get-NetAdapter is in my configuration and the config is compiled by Azure Automation, I get the following error:
Cannot connect to CIM server. The specified service does not exist as an installed service.
Is there a workaround?

The answer is - its not possible. The configs are compiled on the Azure Automation server, not the target node. Even if I were to find a way to get the network adapter name in the config, it would get the name of the adapter on the DSC pull server, not the target node.
The code in the question will work if you use 1 config per node and you are pre-compiling on the target node then uploading it to Azure Automation.

Try this:
xDnsServerAddress DnsServerAddress
{
Address = $DNSServer
InterfaceAlias = (Get-NetAdapter | ? name -Like "Ethernet*" | select -First 1).Name
AddressFamily = 'IPv4'
}

Get-NetAdapter internally is using WMI to get the information which doesn't work in Azure Automation. However you can use get-netipinterface cmdlet to get information about the adapter.

Related

Find Azure ResourceGroup from Azure SQL Server Name

I'm trying to write a powershell script that deploys a BACPAC to an Azure database. I found this example on the Microsoft site
I don't have to have to ask which resource group the SQL Server is in so using Powershell, is it possible to find the resource group a SQL server resides in if you only know the server name?
Try this:
$resourceGroupName = (Get-AzureRmResourceGroup | Get-AzureRmSqlServer | where {$_.ServerName -eq 'Your Server Name Here'}).ResourceGroupName
One quick way of doing this is to use the Find-AzureRmResource cmdlet. You can specify as much information as you'd like (resource group, like name, resource type) to narrow down the search results.
Not really any better or worse than Dean's but using the generic cmdlets (would work for any resourceType):
(Get-AzureRmResource | Where-Object {$_.ResourceType -eq "Microsoft.Sql/servers/databases" -and $_.ResourceName -eq "$svr/$db"}).ResourceGroupName

AzureRm get IP from created virtual machine

I am using azureRm commads in powershell to create virtual machines in azure. And after i create one I would like to get its IP address into variable, but I could not find any azureRm commands to do so. Could anybody help me?
When you are using AzureRM, the IP is no more linked to the "VM" ressource. You need to get the network adapter of your VM and then the IP configured for this on this ressource adapter. Get-AzureRmNetworkInterface
Hope this help.
I'm sure this can be made a bit more compact/efficient, but here is what I'd use in a pinch:
$vmname = "<your-vm-name>"
$ip = (Get-AzureRmNetworkInterface | Where-Object {($_.VirtualMachine.id).Split("/")[-1] -like $vmname}).IpConfigurations.PrivateIpAddress

How do I assign a vNIC to a distributed port group based on its current port group membership?

I have a VM with 3 network adapters:
vNIC1 : OldPortGroup1
vNIC2 : OldPortGroup2
vNIC3 : OldPortGroup3
I need to change the port group of these adapters to NewPortGroup1, NewPortGroup2, NewPortGroup3. I do not know which vNIC is currently assign to what port group but I do know the old port group names. I need to assign the New port group to the vNIC based on its membership to the old port group. The following code does not work:
Get-Datacenter MyDatcenter | Get-VM MyVM | Get-NetworkAdapter | Where-Object { $_.NetworkName -like "OldPortGroup1" } | Set-NetworkAdapter -NetworkName NewPortGroup1
This will return all the vNICs for all virtual machines that are on that network. It does not obey the -VM parameter on Get-VM. As far as I am concerned it is impossible to do what I am thinking. I have tried if statements, switch statements, etc. Any ideas?
Apparently it doesn't work right if you don't actually manually import the Vds plugin. This can be done using the following command:
Get-PSSnapin -Registered -Name VMware.VimAutomation.Vds

Azure Powershell: Get public virtual IP of service

Is it possible to get the public virtual IP (VIP) of an azure service using powershell?
One approach would be to use the Get-AzureEndpoint command
Get-AzureVM -Name "thevmname" -ServiceName "theservicename" | Get-AzureEndpoint | select { $_.Vip }
I'm not sure, but I doubt there is an easy way, because it might change (although it rarely does).
Windows Azure provides a friendly DNS name like “blogsmarx.cloudapp.net” or “botomatic.cloudapp.net.” There’s a reason for providing these (other than simply being prettier than an IP address). These are a necessary abstraction layer that lets the Virtual IP addresses (VIPs) underneath change without disrupting your service. It’s rare for the VIP of an application to change, but particularly thinking ahead to geo-location scenarios, it’s important that Windows Azure reserves the right to change the VIP. The friendly DNS entries provide a consistent interface for users to get to your application.
Source: http://blog.smarx.com/posts/custom-domain-names-in-windows-azure
However, if you get the dns name you could do a dns lookup.
To obtain the Virtual IP of an Azure CloudService deployment via powershell, you can use the Get-AzureService cmdlet combined with the Get-AzureDeployment cmdlet like this:
(Get-AzureService -ServiceName "myCloudService" `
| Get-AzureDeployment -Slot Production).VirtualIPs[0].Address
(Just assign the previous command to, e.g., $CloudServiceIp to plug the IP into subsequent commands.)
You can also get a list of all cloud services and virtual IPs for your subscription by running the following:
Get-AzureService | Select-Object -Property ServiceName, `
#{Name='ProdIP';Expression={(Get-AzureDeployment -Slot Production `
-ServiceName $_.ServiceName).VirtualIPs[0].Address}} | Format-Table -AutoSize

Find SCVMM Management Server within a VM Host/VM

Is it possible to locate a SCVMM Management server from within a Hyper-v host? Is this possible via powershell? I am trying to find which machine manages one of my Hyper-v hosts. I've had no luck searching through registry/wmi. Any ideas?
Thanks in advance!
Not sure about powershell, but I just had to do this today.
1) looked up the port config for scvmm. So far, 5985 looks like the one: Link
2) On your host, run netstat -ano |find "5985"
3) That should return a list scvmm management servers connected.
As far as I know, no. What you could do is to query all of your SCVMM servers and see which happens to know the guest.
Load up the VMM module and connect to your VMM.
# VM name, might or might not be hostname
$guestToLook = "myLostVM"
# A list of all of your VMM servers
$VMMServers = #("vmmsrv01", "vmmsrv02", "vmmsrv03")
$VMMServers | % {
# Connect to VMM server
Get-VMMServer $_
# Get a VM from the VMM host by guest name
$vm = Get-VM -name $myLostVM
# If VM object is returned, you got the VM from current VMM host.
if($vm.Name -eq $myLostVM) { "{0} is managed by VMM host: {1}" -f $vm.Name, $_}
}
Here's a solution via PowerShell.
First we need the registry path on the host for SCVMM config values.
$scvmmAgentKeys = 'HKLM:\SOFTWARE\Microsoft\Microsoft System Center Virtual Machine Manager Agent\Setup'
Now we grab the SCVMM agent port from the registry path.
$wsManTcpPort = (Get-ItemProperty -Path $scvmmAgentKeys -Name WSManTcpPort).WSManTcpPort
Finally, collect the addresses which are listening on that port.
$scvmmAddress = (Get-NetTCPConnection -LocalPort $wsManTcpPort).LocalAddress | Get-Unique | where { $_ -ne "::" }
Note that if anything else has a connection established on the same port (default port at time of writing is 5985) then $scvmmAddress will be an array including the addresses of those other established connections, which are not necessarily SCVMMs.
Fantastic . The only correction shall be
3. $scvmmAddress = (Get-NetTCPConnection -LocalPort $wsManTcpPort).**Remoteaddress** | Get-Unique | where { $_ -ne "::" }