PowerShell to get Azure API report? - azure

Using the PowerShell below, How can I create the report in a .CSV format for all APIs under my Azure Tenant ?
I tried using the below, as a Global Admin account, but it still failed.
Get-AzApiManagement
$Context = Get-AzContext
Get-AzApiManagementBackend -Context $Context
Get-AzApiManagementProduct -Context $Context
Get-AzApiManagementPolicy -Context $Context
Error:
Get-AzApiManagementBackend : Cannot bind parameter 'Context'. Cannot convert the "Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext" value of type
"Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext" to type "Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext".
At line:3 char:37
+ Get-AzApiManagementBackend -Context $Context
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzApiManagementBackend], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.GetAzureApiManagementBackend
Get-AzApiManagementProduct : Cannot bind parameter 'Context'. Cannot convert the "Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext" value of type
"Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext" to type "Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext".
At line:4 char:37
+ Get-AzApiManagementProduct -Context $Context
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzApiManagementProduct], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.GetAzureApiManagementProduct
Get-AzApiManagementPolicy : Cannot bind parameter 'Context'. Cannot convert the "Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext" value of type
"Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext" to type "Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Models.PsApiManagementContext".
At line:5 char:36
+ Get-AzApiManagementPolicy -Context $Context
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzApiManagementPolicy], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Azure.Commands.ApiManagement.ServiceManagement.Commands.GetAzureApiManagementPolicy

-context parameter accepts Instance of PsApiManagementContext.
You need to create the ApiManagementcontext using the New-AzApiManagementContext cmdlet and then pass the Context to the -context parameter to Get-AzApiManagementBackend cmdlet as shown below.
You can refer to this documentation for more information about these powershell cmdlet for APIM

Related

Failing to upload file to Azure Fileshare

I am trying to upload a csv file to azure file share. Below is the cmdlets I am using:
$folderPath="/"
$ctx=(Get-AzStorageAccount -ResourceGroupName 'rg-aexch' -Name 'storaexch').Context
$fileShare = Get-AZStorageShare -Context $ctx -Name "fs-aexch"
Set-AzStorageFileContent -Share $fileShare -Source "E:\ Exc\dash\batch1.csv" -Path $folderPath -Force
But I am getting the below error:
Set-AzStorageFileContent : Cannot bind parameter 'Share'. Cannot convert the "Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare" value of type
"Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare" to type "Microsoft.Azure.Storage.File.CloudFileShare".
At line:13 char:37
Set-AzStorageFileContent –Share $s –Source $localFile
~~
CategoryInfo : InvalidArgument: (:) [Set-AzStorageFileContent], ParameterBindingException
FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet.SetAzureStorageFileContent

Convert System.Object[]' to the type 'System.String

I am trying to create a script that shows the IP set in the firewall of a Azure SQL server, for that I am using the following code:
$sqlservers = Get-AzSqlServer
foreach ($server in $sqlservers)
{
$sqlserver = $sqlservers.ServerName
$sqlRg = $sqlservers.ResourceGroupName
$firewallIP = Get-AzSqlServerFirewallRule -ServerName $sqlserver -ResourceGroupName $sqlRg
}
But I am getting this error
Get-AzSqlServerFirewallRule : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ServerName'. Specified method is not supported.
At line:11 char:56
+ ... rewallIP = Get-AzSqlServerFirewallRule -ServerName $sqlserver -Resour ...
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzSqlServerFirewallRule], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Azure.Commands.Sql.FirewallRule.Cmdlet.GetAzureSqlServerFirewallRule
Get-AzSqlServerFirewallRule : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ServerName'. Specified method is not supported.
At line:11 char:56
+ ... rewallIP = Get-AzSqlServerFirewallRule -ServerName $sqlserver -Resour ...
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-AzSqlServerFirewallRule], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Azure.Commands.Sql.FirewallRule.Cmdlet.GetAzureSqlServerFirewallRule
Home somebody can help me
thanks in advance
You used the array ($sqlservers) of servers to assign $sqlserver and $sqlRg you need to change it to the $server
$sqlservers = Get-AzSqlServer
foreach ($server in $sqlservers)
{
$sqlserver = $server.ServerName
$sqlRg = $server.ResourceGroupName
$firewallIP = Get-AzSqlServerFirewallRule -ServerName $sqlserver -ResourceGroupName $sqlRg
}

azure cloud powershell runbook upload in storage account

I am uploading the result of some computation to an azure storage account.
I use these statements:
$context = New-AzureStorageContext -StorageAccountName "csb5387a20f4e8cx435ax9b8" -StorageAccountKey "xxxx "
Set-AzureStorageBlobContent -Container "azurescan" -File $FileName -context $context
It has worked and this statments still work on the Cloud Shell.
But in the runbook they get the following error-messages, which are not very helpful.
Can anybody help me?
Set-AzureStorageBlobContent : Method not found:
'System.Net.HttpWebRequest
Microsoft.WindowsAzure.Storage.RequestEventArgs.get_Request()'. At
line:329 char:5
+ Set-AzureStorageBlobContent -Container "azurescan" -File $FileNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Set-AzureStorageBlobContent], StorageException
+ FullyQualifiedErrorId : StorageException,Microsoft.WindowsAzure.Commands.Storage.Blob.SetAzureBlobContentCommand

Enable/Disable availability tests in Azure on Schedule

I'm wondering if there is an easy way to run scheduled automation commands in Azure.
I managed to write Enable/Disable command for availability tests both in
Azure CLI:
az resource update --set properties.enabled=true --name 'someName' --resource-type 'Microsoft.Insights/webtests' --resource-group 'soemResourceGroup'
and
Powershell:
#Get All webTests
$resourceGroupnames = "someGroupName1", "someGroupName2";
$enableTests = "True";
ForEach ($resourceGroupname in $resourceGroupnames) {
$resourceGroupname
$allAvailabilityTestsIds = Get-AzureRmResource -ResourceGroupName $resourceGroupname `
| Where-Object -Property ResourceType -EQ "microsoft.insights/webtests" `
| Select-Object -ExpandProperty ResourceId;
ForEach ($availabilityTestId in $allAvailabilityTestsIds) {
$availabilityTest = Get-AzureRmResource -ResourceId $availabilityTestId;
$availabilityTest.Properties.Enabled = $enableTests;
$availabilityTest | Set-AzureRmResource -Force;
}
}
problem is that I'm not sure to run them outside of Comamnd line and on schedule. I've read that I could use Automation account to use powershell scripts but that seems a nightmare since I got tons of issues with authentication (not sure why).
Is that an only way ?
EDIT:
I post the errror I was/am getting below.
Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:37 char:29
+ $availabilityTest | Set-AzureRmResource -Force;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource],
ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet
Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:37 char:29
+ $availabilityTest | Set-AzureRmResource -Force;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource],
ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet
Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:37 char:29
+ $availabilityTest | Set-AzureRmResource -Force;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource],
ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet
Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:37 char:29
+ $availabilityTest | Set-AzureRmResource -Force;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource],
ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet
Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:37 char:29
+ $availabilityTest | Set-AzureRmResource -Force;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource],
ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet
Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:37 char:29
+ $availabilityTest | Set-AzureRmResource -Force;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource],
ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet
Set-AzureRmResource : Cannot validate argument on parameter 'Sku'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:37 char:29
+ $availabilityTest | Set-AzureRmResource -Force;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Azure...dels.PSResource:PSObject) [Set-AzureRmResource],
ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationError,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetAzureResourceCmdlet
Regards.
You could follow the steps as below to use the azure runbook in automation to do that.
1.Navigate to your automation account -> Runbooks -> Create a runbook -> create a Powershell runbook.
2.In the runbook, add the script to login, your complete script should be like below. (Before running the runbook, make sure you have imported the AzureRM.Resources and AzureRM.Profile powershell module in your automation account -> Modules, if not, in the Modules -> Browse Gallery, search for the modules and import them.)
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
#Get All webTests
$resourceGroupnames = "someGroupName1", "someGroupName2";
$enableTests = "True";
ForEach ($resourceGroupname in $resourceGroupnames) {
$resourceGroupname
$allAvailabilityTestsIds = Get-AzureRmResource -ResourceGroupName $resourceGroupname `
| Where-Object -Property ResourceType -EQ "microsoft.insights/webtests" `
| Select-Object -ExpandProperty ResourceId;
ForEach ($availabilityTestId in $allAvailabilityTestsIds) {
$availabilityTest = Get-AzureRmResource -ResourceId $availabilityTestId;
$availabilityTest.Properties.Enabled = $enableTests;
$availabilityTest | Set-AzureRmResource -Force;
}
}
3.After running the script successfully, follow this link Scheduling a runbook in Azure Automation to add a schedule to your runbook.

Set-AzureRmResourceGroup : 'resourceGroupName' does not match expected pattern '^[-\w\._\(\)]+$'

Running this below and can't seem to get the collection correct to pass into the ForEach loop, even though it returns the names I want.
$RGInfo returns as expected. But when I pass it into the loop to Set-AzureRmResourceGroup, it errors as below
PS H:\> $rginfo
ResourceGroupName
-----------------
rg-crp-d365-bp-n
rg-crp-d365-dev1-n
rg-crp-d365-dev2-n
rg-crp-d365-upgrad-n
$RGInfo = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "RG-CRP-D365*" } | Select-Object ResourceGroupName
ForEach ($RGName in $RGInfo) {
If ($RGName.Tags -eq $null) {
Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag #{BUSINESS_UNIT="CRP"; COST_CENTER="6435" }
}
}
Know why I keep getting the below? There are four RG's so the ForEach loop is functional.
Set-AzureRmResourceGroup : 'resourceGroupName' does not match expected
pattern '^[-\w._()]+$'. At line:7 char:2
+ Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag #{BUSINESS_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Set-AzureRmResourceGroup], ValidationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetA
zureResourceGroupCmdlet Set-AzureRmResourceGroup :
'resourceGroupName' does not match expected pattern '^[-\w._()]+$'.
At line:7 char:2
+ Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag #{BUSINESS_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Set-AzureRmResourceGroup], ValidationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetA
zureResourceGroupCmdlet Set-AzureRmResourceGroup :
'resourceGroupName' does not match expected pattern '^[-\w._()]+$'.
At line:7 char:2
+ Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag #{BUSINESS_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Set-AzureRmResourceGroup], ValidationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetA
zureResourceGroupCmdlet Set-AzureRmResourceGroup :
'resourceGroupName' does not match expected pattern '^[-\w._()]+$'.
At line:7 char:2
+ Set-AzureRmResourceGroup -ResourceGroupName $RGName -Tag #{BUSINESS_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Set-AzureRmResourceGroup], ValidationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.SetA
zureResourceGroupCmdlet
First you filtered the content of $RGInfo to only the Resource Group name, you cannot add tags to just the name
$RGInfo = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "RG-CRP-D365*" } | Select-Object ResourceGroupName
Below syntax should work.
$RGInfo = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "RG-CRP-D365*" }
ForEach ($RGName in $RGInfo)
{
If ($RGName.Tags -eq $null)
{
Set-AzureRmResourceGroup -ResourceGroupName $RGName.ResourceGroupName -Tag #{BUSINESS_UNIT="CRP"; COST_CENTER="6435" }
}
}
Hope this helps.
I had a similar issue when importing a list from a CSV file. Turns out the RG names had spaces on it 🤦‍♂️.

Resources