Skip to content

Instantly share code, notes, and snippets.

View astaykov's full-sized avatar

Anton Staykov astaykov

  • Microsoft (former Microsoft Azure MVP)
  • Berlin, Germany
  • X @astaykov
View GitHub Profile
@astaykov
astaykov / AzureResetVMAdminPassword.ps1
Last active June 5, 2017 22:06
reset azure VM password
$cred = Get-Credential -UserName "user@domain.com" -Message "Enter your OrgID to manage Azure"
Add-AzureAccount -Credential $cred
Select-AzureSubscription -SubscriptionName "<Put actual subscription name here>"
$adminCredentials = Get-Credential -Message "Enter new Admin credentials"
$VM = Get-AzureVM -Name "<put azure VM name here>" -ServiceName "<put service name (subdomain) here>"
If ($VM.VM.ProvisionGuestAgent) {
Set-AzureVMAccessExtension -VM $VM `
-UserName $adminCredentials.UserName `
-Password $adminCredentials.GetNetworkCredential().Password `
@astaykov
astaykov / ReserveIPforVM.ps1
Created February 24, 2015 07:01
Move existing Azure VM to a Cloud Service with Reserved IP Address
$subName = "<your_subscription_name>"
$storageName = "<your_storage_account_name>"
$reservedIPname = "<pick_a_name_for_the_reserved_ip>"
$reservedIPlabel = "<pick_a_label_for_the_ip>"
$reservedIPlocation "<set_the_location>" # i.e. West Europe
$serviceName = "<cloud_service_name_of_the_deployed_vm>"
$vmName = "<namne_of_the_vm>"
$vnetSiteName = "<name_of_the_vnet_site>" # if the VM will be part of Virtual Network.
$pathToVMConfig "<enter local path where to save current vm configuration>"
Add-AzureAccount
Clear-AzureProfile -Force
cls
Add-AzureAccount
Select-AzureSubscription -SubscriptionName '<your subscription name here>'
Set-AzureSubscription -SubscriptionName '<your subscription name here>' -CurrentStorageAccountName '<storage account name where disks should be placed>'
$vm = Get-AzureVM | Where-Object { $_.Name -eq '<name of the VM to update>' }
Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel '<label of data disk 1>' -HostCaching ReadOnly -LUN 2 -VM $vm.VM
Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel '<label of data disk 2>' -HostCaching ReadOnly -LUN 3 -VM $vm.VM
Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -DiskLabel '<label of data disk 3>' -HostCaching ReadOnly -LUN 4 -VM $vm.VM
Update-AzureVM -Name $vm.Name -ServiceName $vm.ServiceName -VM $vm.VM
@astaykov
astaykov / azurewebsites_in_farm.ps1
Created March 31, 2015 23:42
Create Azure Web Sites in given Farm
Switch-AzureMode -Name AzureResourceManager
Add-AzureAccount
Select-AzureSubscription "Your Subscription Name"
New-AzureResource -Name NewWebSite `
-Location "West Europe" `
-ResourceGroupName 'Default-Web-WestEurope' `
-ResourceType 'Microsoft.Web/sites' `
-ApiVersion '2014-04-01-preview' `
-PropertyObject @{ 'ServerFarm' = 'DefaultServerFarm' }
Function GetToken
{
param(
[String] $authority = "https://login.microsoftonline.com/lab09.onmicrosoft.com/oauth2/token",
[String] $clientId = "0bc190c9-e84e-4df3-9ef8-xyxyxyxyxyxyxyx",
[String] $clientSecret = "RJ30qUKSyRMhLICJRsqxxxCX01yyUMeF8xxyoo9xL7ltY=",
[String] $resourceId = "https%3a%2f%2fgraph.windows.net"
)
$body = "grant_type=client_credentials&resource=$($resourceId)&client_id=$($clientId)&client_secret=$($clientSecret)"
@astaykov
astaykov / GetToken.ps1
Created October 29, 2015 20:56
Get access token from Azure AD OAuth 2.0 endpoint
Function GetToken
{
param(
[String] $authority = "https://login.microsoftonline.com/lab09.onmicrosoft.com/oauth2/token",
[String] $clientId = "<your_client_id_here>",
[String] $clientSecret = "<client_secret>",
[String] $resourceId = "https%3a%2f%2fgraph.windows.net"
)
$body = "grant_type=client_credentials&resource=$($resourceId)&client_id=$($clientId)&client_secret=$($clientSecret)"
Function GetToken
{
param(
[String] $authority = "https://login.microsoftonline.com/<your_tenant>/oauth2/token",
[String] $clientId = "<client_id>",
[String] $clientSecret = "<client_secret>",
[String] $resourceId = "https%3a%2f%2fgraph.windows.net"
)
$body = "grant_type=client_credentials&resource=$($resourceId)&client_id=$($clientId)&client_secret=$($clientSecret)"
@astaykov
astaykov / UpdateUser.ps1
Created October 29, 2015 23:48
Update user properties in Azure AD Graph API
Function UpdateUser
{
param(
[String] $token
)
$headers = @{"Authorization" = "Bearer $($token)"; "Content-Type" = "application/json"}
$updateUserUri = "https://graph.windows.net/dayzure.onmicrosoft.com/users/maxm@dayzure.onmicrosoft.com?api-version=1.5"
$updateBody = @"
{
"department":"Super Devs"
Connect-MsolService
# Get Service Principal to add the role to
$sp = Get-MsolServicePrincipal -ServicePrincipalName "<URI_OF_YOUR_AZURE_AD_APP>"
# Get role object ID
# Alternatively, you can list all the roles (in order to get a different role name) using just `Get-MsolRole`
$roleId = (Get-MsolRole -RoleName "Company Administrators").ObjectId
# Add role to service principal