ServiceName And Name Differences? - azure

So I was lingering around my Azure account, and found some way to set static IP.
Commands to set static IP is:
Get-AzureVM -ServiceName <service-name> -Name <name> | Set-AzureStaticVNetIP -IPAddress <ip-address> | Update-AzureVM
When I do:
Get-AzureVM -ServiceName <service-name> | Set-AzureStaticVNetIP -IPAddress <ip-address> | Update-AzureVM
; or:
Get-AzureVM -Name <name> | Set-AzureStaticVNetIP -IPAddress <ip-address> | Update-AzureVM
it still succeeded.
What's the differences between ServiceName and Name?
And, related question. How do I list VMs that have IP address set to static?

According to manual:
-Service-name return information about all VM running in cloud service, -Name return info about exactly one VM.

Related

'Unauthorized' error creating app service plan

Don't know where exactly it is failing. I'm using VS Code to create the following script and running from there only:
$ResourceGroupName="powershell-grp"
$Location="North Europe"
$AppServicePlanName="PowershellAppService1975"
$WebAppName="PowershellWebApp1975"
Connect-AzAccount
Get-AzSubscription -SubscriptionName "Visual Studio Enterprise" | Select-AzSubscription
New-AzResourceGroup -Name $ResourceGroupName -Location $Location
New-AzAppServicePlan -Name $AppServicePlanName -Location $Location -Tier "B1" -NumberofWorkers 1 -ResourceGroupName $ResourceGroupName
New-AzWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName -Location $Location -AppServicePlan $AppServicePlanName
And in the powershell console, i get this error:
New-AzAppServicePlan: C:\Temp\AzureCmds\Azure Powershell-WebApp\Script1.ps1:9:1
Line |
9 | New-AzAppServicePlan -Name $AppServicePlanName -Location $Location -T …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Operation returned an invalid status code 'Unauthorized'
New-AzWebApp: C:\Temp\AzureCmds\Azure Powershell-WebApp\Script1.ps1:10:1
Line |
10 | New-AzWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Operation returned an invalid status code 'Unauthorized'
Used -debug cmdlet option that gave the exact info on the issue.
The resource group location supported 0 instances for the chosen app service plan. Therefore, deployed app service plan and the web app to an altogether different location

Azure Invoke-AzVMRunCommand -

I am wondering if there is way to use the Invoke-AzVMRunCommand to run a single command, rather than a powershell ps1 file?
As an example, I want to execute a single command... "C:\app\app.exe -c exit". Without the need to push a powershell commandlet to the system.
I am able to do this via the Azure Portal "RunPowerShellScript" and it works but would like to do it to multiple systems via the command line via Invoke-AzVMRunCommand. These systems do not share a command account that can be used.
According to Microsoft, here is the syntax...
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter #{param1 = "var1"; param2 = "var2"}
I don't want to run a script, I merely want to be able to execute a command on the system. Is this possible?
There is no direct way of doing it. But, you can write a script block and generate a file from it and then run Invoke-AzVMRunCommand using that file and later on delete that file if required.
$Server = "server01"
[System.String]$ScriptBlock = {Get-Process}
$FileName = "RunScript.ps1"
Out-File -FilePath $FileName -InputObject $ScriptBlock -NoNewline
$vm = Get-AzVM -Name $Server
Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -Name $Server -CommandId 'RunPowerShellScript' -ScriptPath $FileName
Remove-Item -Path $FileName -Force -ErrorAction SilentlyContinue
It's now possible to use the:
-ScriptString
... option, however you need to ensure that the Az version will support it.
Azure Pipelines as of 2022-07-21 don't support it:
"A parameter cannot be found that matches parameter name 'ScriptString'."
See the documentation:
https://learn.microsoft.com/en-us/powershell/module/az.compute/invoke-azvmruncommand?view=azps-8.1.0

How do I find the network interface ID associated with an Azure VM

I can find the VM by using
$vm = Get-AzureRmVM -ResourceGroupName "MyResource" -Name "MyVM"
But how can I find the network interface associated with this VM?
Once you have your VM in a variable $vm (as shown below)
$vm = Get-AzureRmVM -ResourceGroupName "MyResource" -Name "MyVM"
You can get the NIC by finding the interface that matches the ID from your VM
$nic = Get-AzureRmNetworkInterface
| Where {$_.Id -eq $vm.NetworkProfile.NetworkInterfaces[0].Id}

Setting a static IP address while using New-AzureQuickVM

I'm currently writing a powershell script that provisions a virtual machine, more testing at the moment. The code at present
New-AzureQuickVM -ImageName $VMImage.ImageName -Windows -Name $VMName -ServiceName $VMName -AdminUsername $adminLogin `
-Password $adminPasswd -AffinityGroup $affinityGrp -InstanceSize $instanceSize -VNetName $virtualNetwork -SubnetNames $virtualSubnet -WaitForBoot
I can't see a parameter on MSDN to set the IP address of the VM. I know you can do it like this:
New-AzureVMConfig -Name $vmname -ImageName $img –InstanceSize Small | Set-AzureSubnet –SubnetNames $sub | Set-AzureStaticVNetIP -IPAddress 192.168.4.7 | New-AzureVM –ServiceName $vmsvc1 –AffinityGroup "NorthEuropeAG";
But it seems neater to use the New-AzureQuickVM. Am I able to just pipe New-AzureQuickVM to Set-AzureStaticVNetIP similar to how New-AzureVMConfig works or is there a better way to do it?
The purpose of the New-AzureQuickVM is to create the VM with absolute minimum number of required fields. Like the Quick Create.
On the Other hand the New-AzureVMConfig give you all options that are necessary to have the StaticIP , as you have mentioned or something like the following.
New-AzureVMConfig -Name "testvm123" -InstanceSize "Small" -ImageName $ImageName |
Add-AzureProvisioningConfig -Windows -AdminUsername $username-Password $password |
Set-AzureSubnet -SubnetNames "Subnetname" |
Set-AzureStaticVNetIP -IPAddress "10.0.0.22" |
New-AzureVM -ServiceName "somevmservicename"
Only way to do it is by setting the IP after VM creation:
Get-AzureVM -ServiceName $VMName -Name $VMName | Set-AzureStaticVNetIP -IPAddress 192.168.4.7 | Update-AzureVM

Edit an existing IIS IP Restriction with Powershell

I am writing a Powershell script to add/remove/edit IP restrictions for websites using Powershell. So far I am able to add restrictions, however wondering the best way to edit an existing ip restriction.
Add
Add-WebConfiguration -Filter /system.webserver/security/ipsecurity -Value #{ipAddress=$ipAddress;subnetMask="255.255.255.255";allowed=$allowed} -Location $websiteName -PSPath "IIS:\"
Edit
I have tried various combinations of:
Set-WebConfiguration -Filter system.webServer/security/ipSecurity/add[#ipAddress='192.123.123.123'] -Name "ipAddress" -Value $ipAddress -Location $websiteName -PSPath "IIS:\"
Set-WebConfigurationProperty -Filter system.webServer/security/ipSecurity/add[#ipAddress='192.123.123.123'] -Value = #{ipAddress=$ipAddress;subnetMask="255.255.255.255";allowed=$allowed} -Location $websiteName -PSPath "IIS:\"
Is the best way essentially to clear all, and recreate each time?
I had the same problem and fixed it like this -
# Compose new entry
$value = #{allowed="true";ipAddress="192.168.0.1"}
# Add new entry to restrictions
Add-WebConfigurationProperty -Filter 'system.webServer/security/ipSecurity' -PSPath "IIS:\Sites\MySite\" -Location "MyService" -Name "." -Value $value -ErrorAction Stop

Resources