Skip to content

Migratemsgraph #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ The scripts install the required PowerShell module (AzureAD) for the current use
1. Open PowerShell as admin (On Windows, Search Powershell in the search bar, right click on it and select Run as administrator).
2. Type:
```PowerShell
Install-Module AzureAD
Install-Module Microsoft.Graph
```

or if you cannot be administrator on your machine, run:
```PowerShell
Install-Module AzureAD -Scope CurrentUser
Install-Module Microsoft.Graph -Scope CurrentUser
```

### Run the script and start running
Expand Down Expand Up @@ -105,20 +105,7 @@ Here are the details on how to do this.

Note that the script will choose the tenant in which to create the applications, based on the user. Also to run the `Cleanup.ps1` script, you will need to re-sign-in.

#### Option 2 (non-interactive)

When you know the indentity and credentials of the user in the name of whom you want to create the applications, you can use the non-interactive approach. It's more adapted to DevOps. Here is an example of script you'd want to run in a PowerShell Window

```PowerShell
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
. .\Cleanup.ps1 -Credential $mycreds
. .\Configure.ps1 -Credential $mycreds
```

Of course, in real life, you might already get the password as a `SecureString`. You might also want to get the password from KeyVault.

#### Option 3 (Interactive, but create apps in a specified tenant)
#### Option 2 (Interactive, but create apps in a specified tenant)

if you want to create the apps in a particular tenant, you can use the following option:
- open the [Azure portal](https://portal.azure.com)
Expand All @@ -132,15 +119,3 @@ $tenantId = "yourTenantIdGuid"
. .\Cleanup.ps1 -TenantId $tenantId
. .\Configure.ps1 -TenantId $tenantId
```

#### Option 4 (non-interactive, and create apps in a specified tenant)

This option combines option 2 and option 3: it creates the application in a specific tenant. See option 3 for the way to get the tenant Id. Then run:

```PowerShell
$secpasswd = ConvertTo-SecureString "[Password here]" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("[login@tenantName here]", $secpasswd)
$tenantId = "yourTenantIdGuid"
. .\Cleanup.ps1 -Credential $mycreds -TenantId $tenantId
. .\Configure.ps1 -Credential $mycreds -TenantId $tenantId
```
37 changes: 15 additions & 22 deletions PersonalAccessTokenAPIAppSample/AppCreationScripts/Cleanup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ param(
[string] $tenantId
)

if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
Install-Module "AzureAD" -Scope CurrentUser
if ($null -eq (Get-Module -ListAvailable -Name "Microsoft.Graph")) {
Install-Module "Microsoft.Graph" -Scope CurrentUser
}
Import-Module AzureAD
Import-Module Microsoft.Graph
$ErrorActionPreference = "Stop"

Function Cleanup
Expand All @@ -21,49 +21,42 @@ This function removes the Azure AD applications for the sample. These applicatio
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.

# Login to Azure PowerShell (interactive if credentials are not already provided:
# Login to Microsoft Graph PowerShell (interactive if credentials are not already provided:
# you'll need to sign-in with creds enabling your to create apps in the tenant)
if (!$Credential -and $TenantId)
if ($TenantId)
{
$creds = Connect-AzureAD -TenantId $tenantId
$creds = Connect-MgGraph -TenantId $tenantId
}
else
{
if (!$TenantId)
{
$creds = Connect-AzureAD -Credential $Credential
}
else
{
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential
}
$creds = Connect-MgGraph
}

if (!$tenantId)
{
$tenantId = $creds.Tenant.Id
$tenantId = (Get-MgOrganization).Id
}
$tenant = Get-AzureADTenantDetail
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
$tenant = Get-MgOrganization
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_.IsDefault -eq $True }).Name

# Removes the applications
Write-Host "Cleaning-up applications from tenant '$tenantName'"

Write-Host "Removing 'pythonwebapp' (python-webapp) if needed"
Get-AzureADApplication -Filter "DisplayName eq 'python-webapp'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
$apps = Get-AzureADApplication -Filter "DisplayName eq 'python-webapp'"
Get-MgApplication -Filter "DisplayName eq 'python-webapp'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
$apps = Get-MgApplication -Filter "DisplayName eq 'python-webapp'"
if ($apps)
{
Remove-AzureADApplication -ObjectId $apps.ObjectId
Remove-MgApplication -ApplicationId $apps.Id
}

foreach ($app in $apps)
{
Remove-AzureADApplication -ObjectId $app.ObjectId
Remove-MgApplication -ApplicationId $app.Id
Write-Host "Removed python-webapp.."
}
# also remove service principals of this app
Get-AzureADServicePrincipal -filter "DisplayName eq 'python-webapp'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
Get-MgServicePrincipal -Filter "DisplayName eq 'python-webapp'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}

}

Expand Down
Loading