Category Archives: PowerShell

Azure Active Directory PowerShell v2

Maybe you’ve already heard about Microsoft Graph and the Graph API. Microsoft Graph is the way resources in the Microsoft cloud are connected to each other. The Graph API is an API you can use to access Microsoft Graph, and browse (or traverse) through all the resources.

image

You can use the Graph API when building your own applications, but Microsoft is moving all their apps, tools etc. to the Graph API as well.

Azure Active Directory PowerShell v2 is moving from the Azure AD API’s to the Graph API as well. This automatically implies that Azure AD PowerShell v2 comes with new cmdlets and new options. The output of these cmdlets should be similar of course (creating a new domain, group or user in Azure Active Directory), but that these cmdlets are in no way compatible with the old Azure AD PowerShell.

Unfortunately, you have no choice then moving to Azure AD PowerShell v2. The existing PowerShell v1 will of course be supported for quite some time as it is impossible for everyone to convert their processes, cmdlets, scripts etc. from one version to another.

Note. We’ve seen similar when Microsoft moved from Azure ASM to Azure ARM. It has been taken years for Microsoft to move everything to ARM, so no worries for end-of-support scenarios anytime soon.

Installing Azure AD PowerShell v2 is easy, just install the module using the Install-Module command. This will download the module from the PowerShell repository.

Install-Module AzureAD

When executed you will receive a notification about an untrusted repository. Click [Y] or [A] to continue. The module will be downloaded and installed.

image

image

image

You can use the following commands to store the credentials of your Office 365 and/or Azure tenant administrator account and use it to login to Azure Active Directory:

$AzureADCred = Get-Credential &lt;your tenant admin&gt;<p>Connect-AzureAD -Credential $AzureADCred

image

You’ve now installed the Azure Active Directory PowerShell v2 module and are logged on to your tenant. If you want to retrieve a list of all new v2 PowerShell commands use can use the Get-Command command:

Get-Command *AzureAD*

image

In future blogposts I will continue with the Azure AD PowerShell v2 module.

More information

<updated on March 21, 2018>

Resolve-DnsName PowerShell command

Every now and then you find these small little things that turn out to be very useful. The Resolve-DnsName cmdlet in PowerShell is such a brilliant thing, and it is a nice replacement for NSLOOKUP. Since it is a PowerShell cmdlet you can easily use it in scripts. It looks like this command was introduced in Windows Server 2012 R2, but I found out only a couple of days ago.

As an Exchange consultant I can use the Resolve-DnsName for retrieving my MX records, or my DMARC record:

image

Using the Get-Help Resolve-DnsName command you can get more information, if you need some examples on how to use it you can use the -Examples option, or the -Detailed and -Full options.

image

Very useful for an Exchange admin, and it can be used in PowerShell scripts.

Rename filenames containing strange characters with PowerShell

Recently I had a computer with tons of file on it which I had to backup and upload to OneDrive for Business (OdfB). For some unknown reason there were lots of file that had the HTML representation of a space character in it (%20), there were also filenames containing a # character.

Needless to say, the OdfB client didn’t like it, and kept complaining about not being able to upload files, it wanted to try again and again…. Renaming these files (in hundreds of directories) was not something I fancied, but this is great for PowerShell to figure out.

To find all files that contained the %20 in it I used the following command:

Dir -Recurse | Where-Object {$_.Name -like "*%20*" }

image

Now it’s just a matter of renaming the “%20” with another character, for example an underscore character “_”, like this:

Dir -Recurse | Where-Object {$_.Name -like "*%20*" } | Rename-Item -NewName { $_.Name -replace "%20","_" }

Et voila, all “%20” characters are now removed from the filenames, and ready to be uploaded to OneDrive for Business

image

The requested HTTP URL was not available

While trying to setup Remote PowerShell to an Exchange 2013 Server I ran into a couple of issues. The most obvious was that the Exchange server only accepts SSL request, so you have to specify ‘https’ in the ConnectionUri, so I used these commands:

$Credential = Get-Credential administrator@posh.local
$Session = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri https://webmail.posh-workshop.com/PowerShell -Authentication Basic -Credential $Credential -AllowRedirection
Import-PSSession $Session

The following error was returned:
New-PSSession : [webmail.posh-workshop.com] Connecting to remote server webmail.posh-workshop.com failed with the following error message : The WinRM client sent a request to an HTTP server and got a response saying the requested HTTP URL was not available. This is usually returned by a HTTP server that does not support the WS-Management protocol. For mor e information, see the about_Remote_Troubleshooting Help topic.

image

When checking the PowerShell virtual directory using the Get-PowerShellVirtualDirectory | fl command I got the following response:

image

When you want to use Remote PowerShell from a non domain member (or via the Internet) you have to use Basic Authentication (as specified in my request), you also have to set Basic Authentication on the Virtual Directory as well, like this:

Get-PowerShellVirtualDirectory –Server MAIL01 | Set-PowerShellVirtualDirectory –BasicAuthentication:$TRUE

Once changed Remote PowerShell works as expected.

Remove Million Log Files from Exchange Server

Customer is running Exchange 2010 with NetBackup for backup. At some point the backup stalled and the Exchange server (passive node) froze. Only hard reboot turned the node back to life.

After lots of troubleshooting it turned out that there were several million (small) log files located in C:\Program Files\Veritas\NetBackup\online_util\ which caused the backup to freeze.

The problem is… how do you remove so many files from a (local) hard disk? A command prompt or Windows Explorer is generally speaking not a good idea, but you can do this with PowerShell.

First load the file into an array with this command:

$Logfiles = [System.IO.Directory]::GetFiles("C:\Program Files\VERITAS\NetBackup\online_util\_fi_cntl", "*.*")

It took approx. 3 minutes to load all files in the $Logfiles array.

To remove the first 100,000 files and determin the time needed the following commands was used:

a=get-date;$Logfiles[0..99999]|%{[System.IO.file]::delete($_)};
$b=get-date

This took only 61 seconds. Disk activity never came over 15%

As a side note: The admins also tried it using a command prompt and a standard del command, this took approx. 53 minutes to remove 100,000 files.

A second batch with 2,000,000 logfiles took 1500 seconds (15 minutes) to remove. Disk activity however kept raising during this 15 minutes up to 100%. To prevent too many issues with (disk) performance customer decided to remove the logfiles in batches of 500,000 items until all logfiles are removed.

Special thanks to Kees de Groot (Big-IT.nl and ex2013.com)