static async Task GetVMInfo() { var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION", EnvironmentVariableTarget.User)); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); IVirtualMachines _client = azure.VirtualMachines; var list = await _client.ListAsync(); foreach (var instance in list) { var name = instance.Name; var ip = instance.GetPrimaryPublicIPAddress().IPAddress; Console.WriteLine("name: " + name + ", ip: " + ip); } }
// Helper method to pre-create infrastructure to test Network Watcher ICreatedResources <IVirtualMachine> EnsureNetwork(INetworkManager networkManager, IComputeManager computeManager, String groupName) { IVirtualMachines vms = computeManager.VirtualMachines; // Create an NSG INetworkSecurityGroup nsg = networkManager.NetworkSecurityGroups.Define(SdkContext.RandomResourceName("nsg", 8)) .WithRegion(REGION) .WithNewResourceGroup(groupName) .Create(); // Create a network for the VMs INetwork network = networkManager.Networks.Define(SdkContext.RandomResourceName("net", 8)) .WithRegion(REGION) .WithExistingResourceGroup(groupName) .WithAddressSpace("10.0.0.0/28") .DefineSubnet("subnet1") .WithAddressPrefix("10.0.0.0/29") .WithExistingNetworkSecurityGroup(nsg) .Attach() .WithSubnet("subnet2", "10.0.0.8/29") .Create(); INetworkInterface nic = networkManager.NetworkInterfaces.Define(SdkContext.RandomResourceName("ni", 8)) .WithRegion(REGION) .WithExistingResourceGroup(groupName) .WithNewPrimaryNetwork("10.0.0.0/28") .WithPrimaryPrivateIPAddressDynamic() .WithNewPrimaryPublicIPAddress(SdkContext.RandomResourceName("pip", 8)) .WithIPForwarding() .WithExistingNetworkSecurityGroup(nsg) .Create(); // Create the requested number of VM definitions String userName = "******"; var vmDefinitions = new List <ICreatable <IVirtualMachine> >(); var vm1 = vms.Define(SdkContext.RandomResourceName("vm", 15)) .WithRegion(REGION) .WithExistingResourceGroup(groupName) .WithExistingPrimaryNetworkInterface(nic) .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer14_04_Lts) .WithRootUsername(userName) .WithRootPassword("Abcdef.123456") .WithSize(VirtualMachineSizeTypes.StandardA1) .DefineNewExtension("packetCapture") .WithPublisher("Microsoft.Azure.NetworkWatcher") .WithType("NetworkWatcherAgentLinux") .WithVersion("1.4") .WithMinorVersionAutoUpgrade() .Attach(); String vmName = SdkContext.RandomResourceName("vm", 15); ICreatable <IVirtualMachine> vm2 = vms.Define(vmName) .WithRegion(REGION) .WithExistingResourceGroup(groupName) .WithExistingPrimaryNetwork(network) .WithSubnet(network.Subnets.Values.First().Name) .WithPrimaryPrivateIPAddressDynamic() .WithoutPrimaryPublicIPAddress() .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer14_04_Lts) .WithRootUsername(userName) .WithRootPassword("Abcdef.123456") .WithSize(VirtualMachineSizeTypes.StandardA1); vmDefinitions.Add(vm1); vmDefinitions.Add(vm2); vms.Create(vmDefinitions); var createdVMs2 = vms.Create(vmDefinitions); return(createdVMs2); }