I wanted to stop web jobs with a certain name that are currently running on all apps in an app plan in Azure. I prefer PowerShell, but Azure PS module is lacking compared to az cli, so I ended up with the following, which feels really clunky:
Get-AzWebApp
|
where ServerFarmId -EQ '$appPlanId'
|
select Name, ResourceGroup
|
% {az webapp webjob continuous list --name $_.Name --resource-group $_.ResourceGroup --query '[].{Id:id}' | ConvertFrom-Json}
|
% {az webapp webjob continuous stop --webjob-name $webjobName --ids "$($_.Id)"}
I'd appreciate advice on better approaches.
As I known, you can use two APIs below of WebJobs API for Continuous Jobs to realize your needs in PowerShell.
List all continuous jobs
Stop a continuous job
For how to call these APIs above, you can refer to the Sample of using REST API with PowerShell.
Meanwhile, please see the document Deployment credentials to know how to use the credentials in the REST calling. And you can refer to my answer for a similar SO thread Unable to access admin URL of Azure Functions to know how to get the credential parameters.
Related
I want to get app insights of all the subscriptions available in the portal but whenever I run the script
$resources = az monitor app-insights component show | ConvertFrom-Json
I get app insights only for the same subscription every time , even during the time when I change the subscription through the script
Set-AzContext -SubscriptionName "some-name"
the whole script goes like this
Set-AzContext -SubscriptionName "some-name"
$resources = az monitor app-insights component show | ConvertFrom-Json
So even if I change the subscription name to something else suppose "some-name1"
still I am getting the app-insights for subscription "some-name"
This is by design.
While you could switch the context in a script, searches across multiple subscriptions are easier and much, much faster using the Resource Graph.
PowerShell Query:
Search-AzGraph -Query "resources | where type =~ 'Microsoft.Insights/components'"
Azure CLI Query:
az graph query -q "resources | where type =~ 'Microsoft.Insights/components'"
Both options should get you all Application Insights resources across your tenant.
For more details, please see the Starter Resource Graph query samples.
On a side note I would also recommend to stick to either Azure CLI or Az PowerShell. While the choice of language is personal preference, sticking to one of the two decreases the dependencies. If you stick to Azure CLI, the only prerequisite is having the Azure CLI binaries installed. If you stick to Az Modules in PowerShell, you don't need Azure CLI but only the Az Modules. Mixing both makes the code more difficult to port to other machines.
So, if using the Az Modules was preferred, instead of...
$resources = az monitor app-insights component show | ConvertFrom-Json
I would recommend:
$resources = Search-AzGraph -Query "resources | where type =~ 'Microsoft.Insights/components'"
The issue you're experiencing with the Set-AzContext command is that it only sets the subscription context for the current PowerShell session.
The az monitor app-insights component show command is running in a separate process or thread, so it is not able to see the updated subscription context set by the Set-AzContext command.
To work around this, you can pass the -Subscription parameter to the az monitor app-insights component show command, like so:
$resources = az monitor app-insights component show --subscription "some-name1" | ConvertFrom-Json
This will ensure that the az command is running with the correct subscription context, and you will get the app insights for the correct subscription.
After running a DevOps pipeline that created several resources, only two (a new resource group and a Functions app) show up in Azure CLI using az ... list --subscription ....
Other resources, like Static Web Apps and a SQL Server don't show up in that list or when running az staticwebapp list --subscription ... or az sql server list --subscription ... with the subscription argument specified. I have confirmed that these resources DO show up in the portal, and my account is listed as an owner (inherited from subscription) for them in the portal.
I have deployed Azure Static WebApp using Azure Devops.
Followed this MSDoc to publish the static web app.
By using,
az staticwebapp list --resource-group YourRGName
and
az staticwebapp list --subscription YourSubscriptionID
,Iam able to get the StaticWeb App list
For anyone else running into this:
I have not found a solution to getting the az staticwebapp list command to work for my use case, as the results that show up immediately after creating a resource are just inconsistent. However, as a workaround that should satisfy most use cases, using this API (using az rest command) to find all resources associated with the group created by the pipeline, then filtering those down to find the Static Web App I want, seems to work.
Then, when using other APIs that reference that app before it shows up in the normal list, fully specifying subscription and resource group alongside the app's name seems to make things more reliable too.
As far as I can tell, this is probably just a limitation with the inconsistency of how long information about newly deployed resources takes to propagate that shows up when trying to find a resource immediately after creating it.
Good morning,
I've been trying to get information about all the resources in a given Azure subscription. Is there a way to see all the resources hosted in a subscription?
Have you tried running Az-GetResource via PowerShell
First Install Az Modules
Then would run
Connect-AzAccount
Then after the Pop Up asking to login
Get-AzResource | ft
I can get we apps using the Az cmdlets by using az webapp list.
I have logic apps , I tried using az logicapp list
but its not recognised in Powershell.
Is there a manual listing the cmdlets to list logic apps ,sqlservers and sqldatabases ?
I wanted to provide an update to #4c74356b41's answer since it has been almost 3 years. For the Azure CLI there are now (as of at least June 2022) commands for managing Logic Apps, both of the "standard" and "consumption" varieties.
For "standard" Logic Apps you can manage them with the az logicapp command. E.g.:
az logicapp list --resource-group MyResourceGroup
For all the operations, run az logicapp --help.
For "consumption" Logic Apps, there is a (currently in preview) command of az logic workflow that requires a logic extension to be installed for the Azure CLI. E.g.:
az logic workflow list --resource-group MyResourceGroup
For all the operations, run az logic workflow --help.
#4c74356b41's answer is definitely still valid, but better support for managing Logic Apps is now a part of the Azure CLI (which is also not directly tied to PowerShell as indicated by the OP's question...the Azure CLI does not have to be run in PowerShell).
See Microsoft's official docs for these Azure CLI commands (and others) at:
Standard Logic Apps
Consumption Logic Apps
the AZ cli equivalent of Get-AzLogicApp is the following:
az resource list --resource-type 'Microsoft.Logic/workflows'
there are no dedicated cmdlets for managing logic apps in Az cli yet.
You could use below method to get logic app list.
Get-AzResource -ResourceType "Microsoft.Logic/workflows" -ResourceGroupName resourcegroupname
As for the sqlservers and sqldatabase, you could just use the method provided.
Get-AzSqlDatabase and Get-AzSqlServer support to list the servers and the databases.
I am looking solution to find out Stopped | Deallocated resources Orphan Resources in Azure. I grab the VM data. But if someone spins the VM and VM showing running, How to check owner not used that VM since 30 Days.
az vm list -d --output table
Any automation suggestion will be welcome.
az vm list -d --output table
TESTSXG VM running
I see multiple queries here.
To identify if someone created any resource (say VM) and has forgot to deallocate it.
To check last login in VM if it is older than 30 days.
To check owner not used the VM(s) in the last 30 days.
If we don’t login to VM since a while and if some services (like Jenkins, etc.) are running and untouched.
To audit actions on resources and to determine the operations that were taken on resources, you may use Activity Logs. For more information refer this (https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-audit) link.
For #1, You may execute the below command.
Get-AzureRmVM -Status|select Name, PowerState
For #2 and #3, below is the command which you can run manually in the VM.
Get-WmiObject -Class Win32_NetworkLoginProfile |
Sort-Object -Property LastLogon -Descending |
Select-Object -Property * -First 1 |
Where-Object {$_.LastLogon -match "(\d{14})"} |
Foreach-Object { New-Object PSObject -Property #{ Name=$_.Name;LastLogon=[datetime]::ParseExact($matches[0], "yyyyMMddHHmmss", $null)}}
But I know that we are looking for an automated way to validate all the VM’s under your subscription. So here the requirement is to automatically (i.e., remotely) connect to all the ‘running’ VM’s from Azure portal and then get the required output. If i am not wrong, most probably we can achieve this requirement in multiple ways i.e.,
i. Log Analytics
ii. DSC
iii. Functions
iv. Runbook
v. Logic App
i. Create a Log Analytics OMS workspace and install OMS agent on the VM(s) as instructed here (https://learn.microsoft.com/en-us/azure/azure-monitor/learn/quick-collect-azurevm). Then add Azure Security Center (Security and Audit) solution in OMS so that the security events will be pushed to OMS repository. Then goto Log Analytics -> OMSworkspaceName -> Logs and run the below Kusto query to get the required output.
SecurityEvent
| where EventID == 4624
| sort by TimeGenerated desc
Note that the Event ID 4624 is the ID for the event log of any account logged on to a machine.
ii. Onboard Azure DSC on the VM(s) as instructed here (https://learn.microsoft.com/en-us/azure/automation/automation-dsc-onboarding) and write a DSC configuration script using ‘script’ DSC resource which will run the above mentioned Get-WmiObject…. command remotely on the DSC nodes (i.e., VM’s) and fetch us the required output.
iii. Write a HTTP trigger PowerShell function which will run the above mentioned Get-WmiObject…. command remotely (i.e., may be try a new ps session and invoke command) on the VM’s and fetch us the required output. You may refer this (https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function) link to learn about Functions.
iv. Write a PowerShell runbook which will run the above mentioned Get-WmiObject…. command remotely (i.e., may be try new ps session and invoke command) on the VM’s and fetch us the required output.
v. Currently Azure Logic Apps seems not support to run PowerShell and CLI script. However, we may try to use available Logic Apps Functions connector or any similar connector and internally try to call PowerShell to execute above mentioned Get-WmiObject…. command remotely. Just FYI here (https://feedback.azure.com/forums/287593-logic-apps/suggestions/33913552-run-a-powershell-code-within-a-logic-app-action) is a voice in Azure feedback regarding running PowerShell code within a Logic App, you could vote if you are interested in this option.
For #4, Install OMS agent on the VM’s so that the events details get stored in OMS repository. For example, if no one is logging in to a VM but Jenkins service is running on that VM then in that case you may want to not disturb that VM. So, to validate if Jenkins service is running on a VM or not you may have to run a Kusto query something like this.
Event
| where (EventLog == "System")
| where (RenderedDescription has "jenkins" and RenderedDescription has "stopped")
Hope this helps!!