I am trying to use some of the Azure Powershell cmdlets in the AzureRM.ApiManagement module. A lot of the cmdlets require a context parameter, which is PsApiManagementContext object.
Following the guidance I have created a context using the New-AzureRmApiManagementContext cmdlet:
$ApiMgmtContext = New-AzureRmApiManagementContext
-ResourceGroupName "MyResourceGroup"
-ServiceName "MyApimService"
I then pass the context as a parameter to the cmdlet:
Get-AzureRmApiManagementUser -Context $ApiMgmtContext
The problem is that I get an error back as follows:
Get-AzureRmApiManagementUser : FormatException: One more parameters were not formatted correctly
I have used Fiddler to inspect the request that it generates and I can see that the body of the request is empty, so there must be something wrong with the way the context object has been created, even though I have basically copied the example in the microsoft docs. I have logged into my Azure subscription using Login-AzureRmAccount and selected the correct subscription prior to trying to create the context.
What am I doing wrong in the creation of the context object?
Related
I'm having two runbooks of two different Powershell versions (5.1, 7.1). How to call a runbook (7.1) from runbook (5.1)?
Modules used:
AZ-SecurityInsights - 5.1 version (runbook 5.1)
AzSentinel - 7.1 version (runbook 7.1)
Need to pass resourcevariable as an input parameter to second runbook from first runbook.
My code:
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
Start-AzAutomationRunbook -AutomationAccountName $automationAccountName -Name "ExportAlertRule" -ResourceGroupName $automationResourceGroupName
Error:
Object reference not set to an instance of an object.
What can I try next?
If the output stream of Start-AzAutomationAccount contains object, the command does not process the output stream correctly. For the same, you need to implement a polling logic, and use the Get-AzAutomationJobOutput cmdlet to retrieve the output.
For more information, refer below documents.
https://learn.microsoft.com/en-us/azure/automation/troubleshoot/runbooks#resolution-13
https://learn.microsoft.com/en-us/azure/automation/automation-child-runbooks#runbook-types
I am trying to import a Websocket Api through Az scripts.
As described in Microsoft doc here, this api contains a method onHandshake at root level.
$operationDst = New-AzApiManagementOperation
-Context $apimDstContext
-ApiId $fullApiId
-ApiRevision $api.ApiRevision
-Name $operation.Name
-OperationId $operation.OperationId
-Method $operation.Method
-Description $opDescription
-UrlTemplate $urlTemplate
-Request $requestDst
-Responses $responsesDst
-Debug -Verbose -ErrorAction Break;
Problem is when importing this method the -urltemplate param value is "" and I keep receiving this error :
System.Management.Automation.ParameterBindingValidationException: Cannot validate argument on parameter 'UrlTemplate'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
I tried setting it to $null for no better and "/" which then gives me this error :
"Operation entity cannot be defined by user for web socket api type."
Actually the onHandshake method is automatically added when creating a Websocket Api.
So I should just ignore the import of that method
In an attempt to create some reusable powershell code I am trying to get the inline invocation of a child runbook to work. I am relatively new to PowerShell and to Azure Automation.
All the attempts I made so far to make an inline call from a parent workbook to a child runbook fail with the following error:
The term './<name_of_child_runbook>.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Both runbooks are PowerShell (version 7.1). Both runbooks reside under the same Automation account.
For sake of clarity, I tried to bring it down to its simplest form. Here is the content of the child runbook, named rnbk_test_child.
Write-Output "Hello, this is the child runbook."
The parent runbook also has one single line of code and looks like this:
./rnbk_test_child.ps1
I first published the child runbook before creating and testing the parent runbook. I mainly worked from the information in this article.
You can call the child runbook using the Az cmdlet Start-AzAutomationRunbook from the Az.Automation module.
$automationAccountName = '<InsertHere>'
$runbookName = '<InsertHere>'
$automationAccountResourceGroup = '<InsertHere>'
$startAutomationRunBookSplat = #{
AutomationAccountName = $automationAccountName
Name = $runbookName
ResourceGroupName = $automationAccountResourceGroup
Parameters = $ParametersHashtable
}
Start-AzAutomationRunbook #startAutomationRunBookSplat -verbose
Docs on the usage here: https://learn.microsoft.com/en-us/powershell/module/az.automation/start-azautomationrunbook?view=azps-7.4.0
Im not sure if you have already figured this out but as far as I'm aware all you need to do in the parent runbook is call the child runbook as if it was a inline powershell function
e.g
runbktestchild -parameter1 "value"
I dont think it will work if the runbook name has underscores in it :S
I'm trying to invoke a powershell script on to a Virtual Machine and retrieve the output of the script. I'm using the Invoke-AzureRmVMRunCommand cmdlet to invoke the script on the VM as shown below.
$ValidationResult = Invoke-AzureRmVMRunCommand -ResourceGroupName $VM.ResourceGroupName -VMName $VM.Name -CommandId "RunPowerShellScript" -ScriptPath $ValidationScript
When I execute the above cmdlet from a regular powershell terminal, I get the output as expected. However, whenever I'm putting this statement inside an automation runbook, I get null in almost all the fields as shown below
I don't see anything specific to this in documentation as well. Am I doing something wrong here?
Any help would be greatly appreciated! Thank you.
Update:
In the script, I'm logging the output using Write-Output cmdlet.
You need to either add the object name on last line of your script or use the Write-Output command. Otherwise it will not output anything.
The following lines both write an object to the output stream.
Write-Output –InputObject $ValidationResult
$ValidationResult
https://learn.microsoft.com/en-us/azure/automation/automation-runbook-output-and-messages
Hope this helps
This issue is most likely due to a reported bug in the AzureRM modules starting with version 5.7.0 / April 2018. Rolling back to version 5.6.0 / March 2018 is reported to fix it. Issue log: https://github.com/Azure/azure-powershell/issues/5982
My Apologies for the delayed response. I was using this in a runbook of type Powershell Workflow. Many of the PowerShell cmdlets behave differently when executed in workflows.
So in this case, what was happening is the Invoke-AzureRmVMRunCommand was executing correctly but the response was only the TYPE of the response and not the actual response object. Hence I was unable to see any values in the response's properties.
In order to make this work, I had to wrap the cmdlet call within an InlineScript {} block.
$ValidationResult = InlineScript {
$result = Invoke-AzureRmVMRunCommand -ResourceGroupName $USING:VM.ResourceGroupName -VMName $USING:VM.Name -CommandId "RunPowerShellScript" -ScriptPath $USING:ValidationScript
$result.SubStatuses[0].Message
}
The result is returned to $ValidationResult variable.
More detailed post is given here: https://amoghnatu.net/2018/04/15/get-output-of-script-executed-as-part-of-set-azurermvmcustomscriptexecution-cmdlet/
Thanks.
On 4th of August a new version of the Azure powershell module (0.8.7.1) was released. In it is the ability to create credentials which you could then pass to the Add-AzureAccount function. Add-AzureAccount allows you to pull in an account to work with in the current PowerShell session.
$userName = "buildmaster#someaccount.onmicrosoft.com"
$securePassword = ConvertTo-SecureString -String "somepassword." -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($userName, $securePassword)
Add-AzureAccount -Credential $cred
This allowed me to get away from a popup window or messing with a settings file.
It seems to have stopped working! Both Add-AzureAccount (which pops up a window) and the credential based way. They now return an index was out of range issue.
Add-AzureAccount : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
At S:\QA\Azure Scripts\cm-azure-helpers.psm1:1128 char:5
+ Add-AzureAccount
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Add-AzureAccount], ArgumentOutOfRangeException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Profile.AddAzureAccount
Digging deeper with Fiddler shows that the OAuth call to the back end service seems to work. I get a token returned. But then I also get a 302 in the middle of the process stating that the page has moved. I don't know if that redirect was there when it was working previously or not.
Has anyone else experienced the Add-AzureAccount function just stop working like this? And more importantly - have you found a way around it?
Update - More info
I have now tried on several boxes under different azure accounts and seem to have the same results. I get a valid auth token returned with a redirect in the middle (not sure if that is an issue or not) and then get the index was out of range.
I have done this with the following variations:
PS:3 Azure Module: 0.8.6
PS:3 Azure Module: 0.8.7
PS:4 Azure Module: 0.8.6
PS:4 Azure Module: 0.8.7
I know exactly what caused this error for me, and how I worked around it (I thought I was the only one who had seen this :))
What had happened is that I had accidentally added a bogus/empty subscription to my account. And this empty subscription had been set to my "default" subscription.
Run "get-azuresubscription -default" to see what your default subscription is. You can then "remove" any junk subscriptions using "remove-azuresubscription" command.
You can then of course set a new azure subscription for your "default" using PS.
I actually reported this to the Azure PowerShell team now to get a better error message during this scenario.
Hopefully this solved your problem, it's possible other errors manifest the same error message.
If you do a fiddler trace, you should see that right after the login call (where PowerShell passes in your username/password, there should be 1 or multiple calls to GET /subscriptions.
Check the response to see whether there is anything suspicious there. Like, any of them return an empty body, empty array, subscription with id, name, etc..
I had a similar problem recently. The "fix" was for me was to delete the files at C:\Users[username]AppData\Roaming\Windows Azure Powershell (esp. the WindowsAzureProfile.xml file). The next time I ran Add-AzureAccount, the necessary files were created and all was well.
Please use:
Add-AzureRmAccount -SubscriptionId "id";
for login