Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Wednesday, 18 July 2018

Install Azure AD & Office 365 Powershell Modules

How to install the latest Azure AD and Office 365 Powershell modules, please note that the Office 365 Sign In Assistant is also required on the workstation.

Set-ExecutionPolicy Unrestricted
Install-Module AzureAD
Install-Module MSOnline
Import-Module AzureAD
Import-Module MSOnline
Connect-MsolService

Wednesday, 17 January 2018

How to convert Azure "unmanaged" Operating System disks to Managed Disks using Powershell

It is very easy to convert an "unmanaged" disk to a Managed Disk inside Azure, if the source VM is standalone and not part of an Availability Set. I recently had to do this after I had "lifted and shifted" a handful of VM's to Azure for a POC. The idea was that I would create new VM's from a specialised VHD's which were uploaded to Azure Storage. I did some research to see what the process was like for moving migrated VM's to Managed Disks.

The following PowerShell code can be used to convert a VM's unmanaged disk to a Managed Disk, please note that this will automatically convert all the associated data disks attached to the VM.


$rgName = "myResourceGroup"
$vmName = "myVM"

Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Force

ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $rgName -VMName $vmName

Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName

The process takes some time to complete, mainly because Azure copies the VHD from the Azure Storage Account. However, it is very quick, my 45GB VM was converted in less than 5 minutes. Once the conversion is completed, the VM will start and the lease on the original VHD will be released. 

When you convert a VHD to a Managed Disk, the Managed Disk is automatically labelled the same name as the VM. It discounts the name of the source VHD from the storage account.

Wednesday, 27 January 2016

Installing and configuring Active Directory entirely with PowerShell

Creating a new Active Directory for a test environment is something I seem to have to do multiple times a week. I hope to never have to do it the long way with the GUI again.

The following PowerShell commands installed AD DS and creates a new forest/domain called “domain.co.uk”. You are prompted for the AD Restore Password, the rest is automated.

Import-Module ServerManager
Add-WindowsFeature AD-Domain-Services
ADD-WindowsFeature RSAT-Role-Tools


Install-AddsForest -DomainName domain.co.uk -CreateDnsDelegation:$false -DatabasePath "C:\Windows\NTDS" -DomainMode "Win2012R2" -DomainNetbiosName "DOMAIN" -ForestMode "Win2012R2" -InstallDns:$true -LogPath "C:\Windows\NTDS" -NoRebootOnCompletion:$false -SysvolPath "C:\Windows\SYSVOL" -Force:$true

Wednesday, 16 December 2015

Azure PowerShell "Your Azure credentials have not been set up or have expired, please run Add-AzureAccount to set up your Azure credentials"

Azure PowerShell throws the error "Your Azure credentials have not been set up or have expired, please run Add-AzureAccount to set up your Azure credentials" when you try to run a PS command against an Azure service. This is using both the Get-AzurePublishSettingsFile and the Add-AzureAccount methods to authenticate to the Azure tenancy.


This is because there are a number of stale entries related to your account cached on the administrative device you are using, to display all of the cached accounts use the following command;

Get-AzureAccount


The following command can be used to delete all of the cached Azure credentials from the device;

foreach ($AzureAccount in (Get-AzureAccount).ID) {Remove-AzureAccount $AzureAccount}


Then try Add-AzureAccount, enter your credentials.


And you should re-authenticate to the Azure tenancy properly. You can now run any commands without being faced with the same error.


Monday, 7 September 2015

Configure Office 365 Regional and Language Settings Globally

If you are in the process of adopting or migrating to Office 365, you will probably be looking to automate as much of the process as possible. When a user initially logs in to Office 365 they will be prompted to set their Regional and Language settings.

The following command can be used to set the time zone and language settings across all of the mailboxes. 

Get-mailbox | Set-MailboxRegionalConfiguration -Language en-gb -TimeZone "GMT Standard Time"

By piping the Get-Mailbox string into the command it sets it for all mailboxes, you could of course use the -Identity switch and and point to individual mailboxes/users.

Thursday, 19 March 2015

Microsoft Azure Uploading and Configuring a Linux VHD, Configure VHD as Azure Disk and Attach to Virtual Machine

If you have existing VM’s you are running on-site, it’s possible to upload them to Azure and configure them as disks or images. Disks can be attached to VM’s which then allow you to effectively migrate your on premise VM’s to the Azure cloud. With Azure Images, you can customize operating systems with your preferred configurations, SysPrep them and then upload to Azure to provision multiple VM’s from.
You should bare the following things in mind when you are uploading existing virtual hard disks to Azure
·       Azure only supported VHD files and not VHDX files (which is likely to change)
·       Azure only supported fixed size VHD files
·       Azure Operating System disks can be a maximum of 127GB’s
·       Azure Data disks can be a maximum of 1TB
To upload VM’s to Azure you must have an Azure Storage account in place to act as a target for the VHD files. You can provision a new Storage Account by clicking New, Data Services, Storage and Quick Create. The URL you choose must be globally unique, also choose a region which is closest to your physical location. There are four options for Replication, I have chosen Geo-Redundant. With Geo-Redundant there is 6 copies of the data maintained, 3 replica’s in your current region and 3 replica’s in another region. Further information on Storage Account options can be found here

Once the Storage Account has been created, now you must make an Azure PowerShell connection from your device, use the following commands with your variables to upload an already existing VHD file from the computer you have made the PowerShell connection from. The first three statements are setting variables that are then called by the Add-AzureVHD statement.
$storage = “rbetts92”
$sourcePath = “C:\Users\R Betts\Desktop\AzureExample.vhd”
Add-AzureVHD –Destination $storagePath –LocalFilePath $sourcePath

You get a graphical representation of the VHD being uploaded to Azure, an MD5 hash is generate as a checksum to confirm the VHD is not corrupt when it reaches Azure.

When it completes you will be presented by confirmation screen with a summary of the file upload.

If you return to the Azure Management Portal, and browse to your Storage Account you should see the VHD that has just been uploaded to the Storage Account.

The next step is to change the disk type to an Azure Disk, this is different from an Azure Image, which would have to be SysPrep’d. Use the following PowerShell command to tag/label the uploaded disk as an Azure disk. The first two statements are setting variables.
$diskName = “AzureDisk”
$label = “MyAzureDisk”
Add-AzureDisk –DiskName $diskName –Label $label –MediaLocation $storagePath –OS Linux


Now you can create a new Azure Virtual Machine from your uploaded image, use the Create New VM wizard, but instead of clicking on a pre-define click My Disks from the list on the left hand side.

Select the new VHD and run through the wizard to provision a machine from an existing hard disk.


As this VM I uploaded is Linux and has the Azure Linux Tools installed, I click on The VM Agent that Supports the Extensions is Already Installed.

Now that the disk is attached to an Azure VM, the next step that is required is to create endpoints to allow remote connections to the machine, for example SSH etc.