# Az Powershell

## <mark style="color:$primary;">Instalación</mark>

### Az PowerShell

```powershell
Install-Module Az
```

## <mark style="color:$primary;">Conexión</mark>

Una vez se hayan obtenido credenciales válidas, se podría utilizar la herramienta de Az PowerShell para conectarse al tenant y enumerar Azure AD y recursos (Azure Resources).

### Popup

```powershell
Connect-AzAccount
```

```powershell
Account                      SubscriptionName TenantId                             Environment
-------                      ---------------- --------                             -----------
xtormin@xtormincorp.com                       10081781-e4e1-4302-bf67-43fae511bbe4 AzureCloud
```

### Línea de comandos con credenciales

{% code overflow="wrap" %}

```powershell
$passwd = ConvertTo-SecureString "<password>" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential("<email>", $passwd)
Connect-AzAccount -Credential $creds
```

{% endcode %}

### Línea de comandos con secreto de servicio principal

{% code overflow="wrap" %}

```powershell
$passwd = ConvertTo-SecureString "<secret>" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential("<App ID>", $passwd)
Connect-AzAccount -ServicePrincipal -Credential $creds -Tenant <Tenant ID>
```

{% endcode %}

### Conexión con token

{% code overflow="wrap" %}

```powershell
$AccessToken = '<token JWT>'
$AccountId = '063aaf57-30af-41f1-840a-0e51ed144916' # TenantID
Connect-AzAccount -AccessToken $AccessToken -AccountId $AccountId
```

{% endcode %}

## <mark style="color:$primary;">Token</mark>

#### ARM Token en texto plano

```powershell
$tokenObject = Get-AzAccessToken -ResourceUrl "https://management.azure.com"
$plainToken = (New-Object System.Net.NetworkCredential("", $tokenObject.Token)).Password
```

#### Graph Token

```powershell
(Get-AzAccessToken -ResourceUrl https://graph.microsoft.com).Token
```

## <mark style="color:$primary;">Comandos</mark>

* Comandos de AzureAD:

```powershell
Get-Command *azad*
```

* Comandos de *Azure Resources*:

```powershell
Get-Command *az*
```

* Buscar comandos de un recurso en particular:

```powershell
Get-Command *azvm*
Get-Command -Noun *vm* -Verb Get
Get-Command *vm*
```

## <mark style="color:$primary;">Información general</mark>

* Obtener información del contexto (tenant, cuenta, ...):

```powershell
Get-AzContext
```

* Obtener subscripciones accesibles por el usuario actual:

```powershell
Get-AzSubscription
```

* Obtener los recursos visibles por el usuario actual:

```powershell
Get-AzResource
Get-AzResourceGroup
```

* Obtener todos los Azure RBAC roles asignados:

```powershell
Get-AzRoleAssignment
```

* Obtener todas las máquinas virtuales donde el usuario actual tiene al menos el rol de lectura:

```powershell
Get-AzVM | fl
```

## <mark style="background-color:purple;">Usuarios</mark>

* Obtener todos los usuarios:

```powershell
Get-AzADUser
```

* Obtener información de un usuario:

```powershell
Get-AzADUser -UserPrincipalName
```

* Buscar un usuario que comience por una cadena específica:

```powershell
Get-AzADUser -SearchString "admin"
```

* Obtener los usuarios que contengan una cadena específica:

```powershell
Get-AzADUser |?{$_.Displayname -match "admin"}
```

## <mark style="background-color:purple;">Grupos</mark>

* Obtener todos los grupos:

```powershell
Get-AzADGroup
```

* Obtener detalles de un grupo en concreto:

```powershell
Get-AzADGroup -ObjectId ff7d0475-364b-4ad6-8f7d-7e8f831e93ce
```

* Obtener detalles de los grupos que empiecen por una cadena en concreto:

```powershell
Get-AzADGroup -SearchString "admin" | fl *
```

* Obtener detalles de los grupos que contengan una cadena en concreto:

```powershell
Get-AzADGroup |?{$_.Displayname -match "admin"}
```

* Obtener miembros de un grupo:

```powershell
Get-AzADGroupMember -ObjectId 9197ad11-c384-4289-bb2a-45d03cfb6d05
```

## <mark style="background-color:purple;">Aplicaciones</mark>

* Obtener todos los objetos de aplicaciones registradas en el *Tenant*:

```powershell
Get-AzADApplication
```

* Obtener todos los detalles de una aplicación:

```powershell
Get-AzADApplication -ObjectId fe5e6dcb-1c46-2abe-b964-9af5ba322494
```

* Obtener una aplicación según su *Display Name*:

```powershell
Get-AzADApplication | ?{$_.DisplayName -match "app"}
```

* Obtener todas las aplicaciones que tienen contraseña:

```powershell
Get-AzADApplication | %{if(Get-AzADAppCredential -ObjectID $_.ObjectID){$_}}
```

* Obtener aplicaciones web visibles:

```powershell
Get-AzWebApp | ?{$_.Kind -notmatch "functionapp"}
```

## *<mark style="background-color:purple;">Service Principals</mark>*

* Obtener todos los *Service Principals*:

```powershell
Get-AzADServicePrincipal
```

* Obtener todos los detalles de un *Service Principal*:

```powershell
Get-AzADServicePrincipal -ObjectId fe5e6dcb-1c46-2abe-b964-9af5ba322494
```

* Obtener los *Service Principal* según su *Display Name*:

```powershell
Get-AzADServicePrincipal | ?{$_.DisplayName -match "app"}
```

## <mark style="background-color:purple;">Roles</mark>

* Obtener roles asignados:

```powershell
Get-AzRoleAssignment
```

## *<mark style="background-color:purple;">Automation Accounts</mark>*

* Obtener grupo del *Automation Accounts*:

{% code overflow="wrap" %}

```powershell
Get-AzAutomationHybridWorkerGroup -AutomationAccountName HybridAutomation -ResourceGroupName Test
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.xtormin.com/pentesting-en-infraestructuras/cloud/azure/metodologia-de-azure-pentest/reconocimiento-interno/enum-con-herramientas-de-ms/az-powershell.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
