Azure PowerShell - New-AzWebAppCertificate doesn't return any response - azure

I'm trying to use the New-AzWebAppCertificate cmdlet that creates a new certificate but I want to get the thumbprint after creation.
I have tried the following command that didn't work.
$certificate = New-AzWebAppCertificate -Name $apiName `
-ResourceGroupName $settings.resource `
-WebAppName $apiName `
-HostName $domainName `
-AddBinding -SslState 'SniEnabled' `
Write-Host $certificate.Thumbprint
But $certificate variable is empty.
Also, I've tried adding -OutVariable
New-AzWebAppCertificate -Name $apiName `
-ResourceGroupName $settings.resource `
-WebAppName $apiName `
-HostName $domainName `
-AddBinding -SslState 'SniEnabled' `
-OutVariable out
But still don't work..
If you read the documentation you will find that they are supporting common parameters such as -OutVariable and also the cmdlet produces PSCertificate output. But in fact, none of them are working.

Related

Cannot parse the request - MissingJsonReferenceId : Error while creating NIC using PowerShell Az command

I am using below Az PowerShell command to create the NIC for virtual machine.
# Create a NIC for the web server VM.
$nicVMweb = New-AzNetworkInterface -ResourceGroupName $rgName -Location $location `
-Name $VmFrontendNICCardName -PublicIpAddress $publicipvm1 `
-NetworkSecurityGroup $nsgfe -Subnet $virtualNetwork.Subnets[0]
Cannot parse the request. StatusCode: 400 ReasonPhrase: Bad Request ErrorCode: InvalidRequestFormat ErrorMessage: Cannot parse the request. Additional details: Code: MissingJsonReferenceId Message: Value for
| reference id is missing. Path properties.ipConfigurations[0].properties.subnet. OperationID : 78525e42-a036-460f-10f9-5b993b7ca5e6
Issue Resolved by Below PowerShell
$Subnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $rgName
$IPconfig = New-AzNetworkInterfaceIpConfig -Name $VmFrontendIpConfigName -PrivateIpAddressVersion IPv4 -PrivateIpAddress "10.0.0.10" -SubnetId $Subnet.Subnets[0].Id
$nicVMweb = New-AzNetworkInterface -Name $VmFrontendNICCardName -ResourceGroupName $rgName -Location $location -IpConfiguration $IPconfig
What is the issue in first command?
Could not reproduce your issue, your first command works fine on my side.
$virtualNetwork = Get-AzVirtualNetwork -Name "<vnet-name>" -ResourceGroupName "<group-name>"
$publicipvm1 = Get-AzPublicIpAddress -ResourceGroupName <group-name> -Name joyvm-ip2
$nsgfe = Get-AzNetworkSecurityGroup -Name joyvm-nsg -ResourceGroupName <group-name>
$nicVMweb = New-AzNetworkInterface -ResourceGroupName <group-name> -Location "West US 2" -Name "joyinter" -PublicIpAddress $publicipvm1 -NetworkSecurityGroup $nsgfe -Subnet $virtualNetwork.Subnets[0]
For this issue, it may be related to the version of Az.Network module, I use the 3.3.0 version, try to update it to the latest version:
Update-Module -Name Az.Network

unable to create alert using powershell Add-AzMetricAlertRuleV2

I'm running into a weird issue here.
I'm trying to use powershell to create individual availbility alerts for all my storageaccounts.
My code is this
$storageaccounts= get-azstorageaccount |get-azresource
$criteria = New-AzMetricAlertRuleV2Criteria -MetricName "Availability" `
-TimeAggregation average `
-Operator lessthan `
-Threshold 100
foreach ($storageaccount in $storageaccounts){
Add-AzMetricAlertRuleV2 -Name "$storageaccount.Name availbility" `
-ResourceGroupName $RG.ResourceGroupName `
-WindowSize 00:05:00 `
-Frequency 00:01:00 `
-Description "Catching storageaccount availbility" `
-condition $criteria `
-ActionGroup $action `
-Severity 3 `
-TargetResourceId "$storageaccount.resourceid"
}
however I keep getting this error
Add-AzMetricAlertRuleV2 : Exception type: ErrorResponseException,
Message: Null/Empty, Code: Null, Status code:BadRequest, Reason phrase: Bad Request
I think the problem is due to
Add-AzMetricAlertRuleV2 -Name "$storageaccount.Name availbility"
if i put storageaccount.name into double quotation, i'm getting
Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.PSResource.Name
I also tried this
foreach ($storageaccount in $storageaccounts){
$sa=[string]$storageaccount.Name
Add-AzMetricAlertRuleV2 -Name "$sa availbility"
but still gives me the same error
How can i fix this?
There are two problems in the command that you are using:
Name: You can pass the name as: "$($storageaccount.StorageAccountName) availbility"
Resource Id: Resource Id which you want to pass is stored in the property "id" of storage account not the "resourceId".
Updated code:
Add-AzMetricAlertRuleV2 -Name "$($storageaccount.StorageAccountName) availbility" `
-ResourceGroupName $RG.ResourceGroupName `
-WindowSize 00:05:00 `
-Frequency 00:01:00 `
-Description "Catching storageaccount availbility" `
-condition $criteria `
-ActionGroup $action `
-Severity 3 `
-TargetResourceId $storageaccount.id

Change password of Azure VM using PowerShell

I have tried this approach to change a password of an Azure VM:
$resgroup = "rsource1"
$vmName = "virtualmachine1"
$VM = Get-AzVM -ResourceGroupName $resgroup -Name $vmName
$Credential = Get-Credential
$VM | Set-AzureVMAccessExtension –UserName $Credential.UserName `
–Password $Credential.GetNetworkCredential().Password
$VM | Update-AzVM
But I keep getting this error:
Object reference not set to an instance of an object.
When I console.log the values of $Credential.UserName and $Credential.GetNetworkCredential().Password I got the values of username and password that I have inputted.
What am I missing here?
I've never used Set-AzureVMAccessExtension, but I've used the Az PowerShell equivalant Set-AzVMAccessExtension. It needs you to pass -Credential $Credential instead of -UserName and -Password.
You can try this script I made a while ago to to reset passwords for Azure VMs:
# Replace these values with your own
$resourceGroupName = "Servers-RG"
$vmName = "server1"
# Get the VM into an object
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName
# Store credentials you want to change
$credential = Get-Credential -Message "Enter your username and password for $vmName"
# Store parameters in a hashtable for splatting
# Have a look at https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7
$extensionParams = #{
'VMName' = $vmName
'Credential' = $credential
'ResourceGroupName' = $resourceGroupName
'Name' = 'AdminPasswordReset'
'Location' = $vm.Location
}
# Pass splatted parameters and update password
Set-AzVMAccessExtension #extensionParams
# Restart VM
# Don't need to pass any switches since they are inferred ByPropertyName
# Have a look at https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-7
$vm | Restart-AzVM
I found that the password update doesn't happen until you restart the VM, so Restart-VM is required.
If anyone interested in the Linux (KISS) version (no VM restart needed):
$settings = '{}'
$protectedSettings = '{
"username": "<yourusername, prefer using Credentials object>",
"password": "<yourpassword, prefer using Credentials object>"
}'
Set-AzVMExtension `
-VMName $vmName `
-ResourceGroupName $rgName `
-Location $location `
-Name "VMAccessForLinux" `
-Publisher "Microsoft.OSTCExtensions" `
-ExtensionType "VMAccessForLinux" `
-TypeHandlerVersion "1.4" `
-Settingstring $settings `
-ProtectedSettingString $protectedSettings

Create VM in Azure with powershell with no public IP

I'm creating VM on Azure from an Image using powershell.
This is the script I'm using .
$UserName = "username"
$Password = ConvertTo-SecureString "password#123" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
New-AzureRmVm `
-ResourceGroupName "RSG" `
-Name "VMName" `
-ImageName "ImageName" `
-Location "West US" `
-VirtualNetworkName "VNName" `
-SubnetName "default" `
-Credential $psCred
-PublicIpAddressName "None" `
-OpenPorts 3389
But, when I got into the Azure portal and see, some Public Ip is getting assigned by default. I have also tried without giving PublicIpAddressName property assuming , it wont assign any IP, but still it is assigning.
I want the Public IP to be none.Can anyone help me achieve this.Thanks!
Currently this an issue which is still in Open state on official azure-powershell github. You can refer it here . Incase if you still want to bypass this you can try using New-AzureReservedIP or after the deployment command try to remove the public ip by yourself Remove-AzureRmPublicIpAddress.
Note : I have'nt tested it yet. Just an idea.
Refer : Docs
To set no public ip address you have can just define it as "" , in powershell you will need to quote that again so it will be """" .
If you are using PowerShell, then you will need to escape all empty parameters by changing "" to '""' to properly pass an empty string into the command. Without this, PowerShell will not pass the empty string, and you will get an error from the command indicating it's missing a parameter.
$winVmCred = Get-Credential `
-Message "Enter username and password for the Windows management virtual machine."
# Create a NIC for the VM.
$winVmNic = New-AzNetworkInterface -Name "winVMNIC01" `
-ResourceGroupName $resourceGroup.ResourceGroupName `
-Location $location `
-SubnetId $targetVMSubnet.Id `
-PrivateIpAddress "10.10.12.10"
# Configure the Windows management VM.
$winVmConfig = New-AzVMConfig -VMName $winVmName -VMSize $winVmSize | `
Set-AzVMOperatingSystem -Windows -ComputerName $winVmName -Credential $winVmCred | `
Set-AzVMSourceImage -PublisherName $winVmPublisher `
-Offer $winVmOffer `
-Skus $winVmSku `
-Version $winVmVersion | `
Add-AzVMNetworkInterface -Id $winVmNic.Id
# Create the VM.
$winVM = New-AzVM -ResourceGroupName $resourceGroup.ResourceGroupName `
-Location $location `
-VM $winVmConfig `
-ErrorAction Stop

Azure Automation moving blobs hashtable error

I am trying to create a script to move blobs from one container to another after they are processed by another automation process. The code I am using is below.
workflow Move-AttendeeFiles
{
$connectionName = Get-AutomationConnection -Name 'AzureConnection'
$storageAccountName = Get-AutomationVariable -Name 'StorageAccountName'
$storageContainerName = Get-AutomationVariable -Name 'toprocessContainer'
$destContainerName = Get-AutomationVariable -Name 'processedContainer'
Connect-Azure `
-AzureConnectionName $connectionName
inlineScript{
Select-AzureSubscription `
-SubscriptionName $Using:connectionName
Set-AzureSubscription `
-SubscriptionName $Using:connectionName `
-CurrentStorageAccount $Using:storageAccountName
Get-AzureStorageBlob `
-Container $Using:storageContainerName | Start-AzureStorageBlobCopy `
-DestContainer $Using:destContainerName
}
}
It is throwing the below error
Could not retrieve 'System.Collections.Hashtable' connection asset.
Check that you created this first in the Automation service.
I can't seem to get it running and must be staring at it to long. Any help would be much appreciated.
The Connect-Azure runbook takes as a string the connection asset name. You are passing it the connection, itself. Pass the connection name instead.
Joe's answer got my past the Hashtable error but I had some other errors in my code. I wanted to post the working code for everyone.
workflow Move-AttendeeFiles
{
$connectionName = Get-AutomationVariable -Name 'azureConnectionName'
$subId = Get-AutomationVariable -Name 'azureSubscriptionId'
$storageAccountName = Get-AutomationVariable -Name 'StorageAccountName'
$storageContainerName = Get-AutomationVariable -Name 'toprocessContainer'
$destContainerName = Get-AutomationVariable -Name 'processedContainer'
Connect-Azure `
-AzureConnectionName $connectionName
inlineScript{
Select-AzureSubscription `
-SubscriptionName $Using:connectionName
Set-AzureSubscription `
-CurrentStorageAccountName $Using:storageAccountName `
-SubscriptionId $Using:subId
}
}
Note the difference in the Set-AzureSubscription code to use the -SubscriptionId instead of -SubscriptionName as that is not the proper variable.

Resources