Category Archives: PowerShell

Introducing PowerShell Core 6.0

Few Days back Microsoft has announced the general availability of PowerShell Core 6.0. It is a new edition of PowerShell that enables you to work with PowerShell regardless of your operating system, be it Windows, MacOS or Linux. It’s runtime is built on top of .NET Core 2.0 and it also exposes the APIs offered by .NET Core 2.0, so that you can use the same APIs in your PowerShell scripts, cmdlets etc…

PowerShell vs. PowerShell Core

 

Below are the key differences between the two editions of PowerShell.

 

Windows PowerShell

PowerShell Core

It is the edition of PowerShell built on top of .NET Framework

(sometimes referred to as “FullCLR”):

It is the edition of PowerShell built on top of .NET Core

(sometimes simplified to “CoreCLR”).

Because of its dependency on the .NET Framework, Windows PowerShell is only available on Windows (hence the name).

PowerShell Core is launched as pwsh.exe on Windows and pwsh on macOS and Linux

The released versions of Windows PowerShell include 1.0, 2.0, 3.0, 4.0, 5.0, and 5.1.

On PowerShell Core, $PSVersionTable.PSEdition is set to Core.

Windows PowerShell is available as a built-in component in Windows client and Windows Server.

Although PowerShell Core 6.0 is cross-platform, there is also a PowerShell Core 5.0/5.1 released exclusively as part of Microsoft Nano Server.

Windows PowerShell is launched as powershell.exe.

Any usage of .NET-based functionality (e.g. C# cmdlets, Add-Type, and the invocation of static .NET Methods), relies on the .NET Core runtime. This means PowerShell Core is limited to the functionality exposed by .NET Core and .NET Standard.

On Windows PowerShell 5.0/5.1, $PSVersionTable.PSEdition is set to Desktop.

 

Any usage of .NET-based functionality (e.g. C# cmdlets, Add-Type, and the invocation of static .NET Methods), relies on the .NET Framework runtime. This means Windows PowerShell’s .NET usage is limited to the functionality exposed by the .NET Framework and .NET Standard.

 

Continues to be supported via critical bug fixes in the newest releases of Windows and Windows Server

 

 

You can Download PowerShell Core depending on your OS platform using below links.

 

PowerShell Core on Windows https://aka.ms/getps6-windows

PowerShell Core on macOS and Linux https://aka.ms/getps6-linux

Automatically Closing Old SCOM Alerts using PowerShell

If you a SCOM administrator, then you are familiar with seeing old alerts piling up in your SCOM console. No matter how much you clean up your monitoring environment, some alerts will be left over unless you manually remove them, depending on how you have configured alert resolutions.

Following PowerShell script from Microsoft SCOM blog will allow you to automatically close old SCOM alerts.The logic behind this script is, that it will traverse the active alerts and checks for alert age. If the alert age is greater than the specified number of days in the $alertsTobeClosedBefore variable, those alerts will be closed.

Remember to load the SCOM PowerShell Module before you run this script.

$alertsTobeClosedBefore = 5

$currentDate = Get-Date

Get-SCOMAlert | Where-Object {(($_.ResolutionState -ne 255) -and (($currentDate – $_.TimeRaised).TotalDays -ge $alertsTobeClosedBefore))} |Resolve-SCOMAlert

 

Removing SCOM MPs like a Boss

It’s not like everyday you might want to remove certain management packs from your SCOM management group. The most  painful task is removing the dependent MPs as you need to manually track all of those and delete them first in order to successfully remove the parent management pack.

Microsoft Senior Software Engineer Chandra Bose has written a PowerShell script that can  identify and remove all of the dependent management packs automatically in such situations. Lets explore that script a little in this post.

How to get started?

First of all you need to run the Operations Manager Command shell as an administrator,  which should be a member of the Operations Manager Administrators group as well.

When you execute the script you can either provide the ID or the System Name of the parent management pack as below. You can find the MP ID by visiting Administration > Management Packs > Right click the desired MP and select properties > Look for the ID field in the General tab which shows the MP ID. The System Name is the unique name of the MP (i.e Microsoft.SQLServer.2012.Discovery)

 .\RecursiveRemove.ps1 <ID or System Name of the MP>

You can download the RecursiveRemove.ps1 script from here.

Deploying SQL RP in Azure Stack | InvalidApiVersionParameter

Last week I was deploying a new Azure Stack POC deployment with my friend CDM MVP Nirmal Thewarathanthri. While we were working on the SQL RP deployment there was a strange issue. The problem was every time when the SqlRPTemplateDeployment.ps1 was  executed, it always failed with below error.

New-AzureRmResourceGroupDeployment : InvalidApiVersionParameter: The api-version ‘2015-11-01’ is invalid. The supported versions 
are ‘1.0,2.0,2015-01-01,2014-04-01-preview,2014-04-01,2014-01-01,2013-03-01,2014-02-26,2014-04’.
At D:\SQLRP\AzureStack.SqlRP.Deployment.5.11.61.0\Content\Deployment\SqlRPTemplateDeployment.ps1:207 char:5
+     New-AzureRmResourceGroupDeployment -Name “newSqlRPTemplateDeploym …
+

Wrong Azure PowereShell version

The Azure Stack documentation instructs us to update to the latest version of AzurePowerShell in the Client VM before deploying the SQL RP. But the latest version 1.2.2 released in March seems not supported in this scenario. When we downgraded that to version 1.2.1 (February version) we were able to continue with the SQL RP deployment.

If you are using the web installer for Azure PowerShell be mindful to avoid that at least for now. You can explore all the releases from GitHub and download  them as MSI packages.

PowerShell ISE Preview | A Sneak Peak

There is a new kid in the PowerShell block. Microsoft PowerShell team has announced a new preview ISE version which is separate from WMF 5.0. This product is uploaded to the PowerShell Gallery, to provide more agile builds rather than waiting to be packaged with WMF.  You can run both ISE Preview and OS built-in ISE version side by side without any conflict.

Getting Started

In order to run the ISE preview you should have WMF 5.0 installed. Unless you are using Windows 10 you need to download and install WMF 5.0 package.

Update

WMF 5.0 package has been removed from the Download Center due to a bug. Please refer the announcement here. Therefore you will have to stick with Windows 10 as of the writing of this post.

  • Open a PowerShell window with elevated privileges and execute below cmdlet. Alternatively you can use -Scope CurrentUser parameter if you wish to install the preview only for your user account.

Install-Module -Name PowerShellISE-preview

  • Following cmdlet will install Start menu shortcuts for ISE preview.

Install-ISEPreviewShortcuts

  •  To start the ISE preview type isep and press enter.
  • To update the ISE module simply execute below cmdlet.
Update-Module -Name PowerShellISE-preview

Limitations

  • Currently the ISE Preview only works with PowerShell v5 installed, either with Windows 10  or with WMF 5.0
  • This release is English-only.
  • There is a possibility for existing add-ons to have issues with the new ISE Preview.  PowerShell team states that ISE Steroids and Azure Automation Authoring Toolkit seems to be working fine with the new preview according to their testing.

 

Office 365 Management APIs prevent deleting an Azure AD Tenant

Have you ever tried deleting an Azure Active Directory Tenant? Sometimes you may need to do this if you have multiple test directories in your Azure tenant. Today I’m going to discuss one specific issue which had prevented me from deleting couple of test Azure AD tenants I had in my Azure subscription.

Scenario

I’ve had two Azure AD tenants which I’ve deployed for testing and wanted to delete from my subscription. As for the preparations I deleted all users, groups and application in both directories except the default user (Microsoft Account). As soon as I hit DELETE it was prompting below error.

“Directory contains one or more applications that were added by a user or administrator.”

Now I was pretty much sure that I deleted all the SaaS applications from both directories but I went ahead and checked the application list just to be sure.

Office365 API (1)

I can see one application called “Office 365 Management APIs” in the list which cannot be deleted and none of the directories were originated from Office 365 subscriptions.

Office365 API (2)

Fix

I created a new global administrator user in each directory additional to the default Microsoft Account I had. (user@domain.onmicrosoft.com). Then I signed into my Azure AD tenant using Azure AD PowerShell. Here I’ve used the Connect-MsolService cmdlet and used the credentials of the new admin account to sign in.

I’ve executed following cmdlet to remove all SaaS applications from Azure AD. Note that there may be failures  because some of the applications simply can’t be removed from Azure AD but it shouldn’t be a problem to delete the particular Azure AD tenant.

Get-MsolServicePrincipal | Remove-MsolServicePrincipal

When I switched back to Azure portal after exiting the PowerShell Session I could still see the Office 365 Management APIs application, but I decided to delete the global administrator for each directory and hit the DELETE button. Guess what I could successfully remove both Azure AD tenants without any issue.

This TechNet article came in very handy to troubleshoot this issue and contains more of the deletion scenarios for an Azure AD tenant.

Installing & Configuring Azure PowerShell

Do you know that Microsoft Azure offers a flexible management options rather than the Azure Portal? With Windows PowerShell you can perform most of the routine tasks that you do in your cloud tenants, from creating VMs to scaling your applications. This comes pretty much handy if you have scheduled or predefined cloud workloads that you need to perform on Azure. Lets take a look on how to install and configure Azure PowerSehll for your cloud tenant.

Prerequisites

  1. An Azure Subscription
  2. A computer that is either running Windows 7 or Windows Server 2008 R2 upwards.

Installing Azure PowerShell

Azure PowerShell comes as a redistributable running in Microsoft Web Platform installer. You can download the setup from here. When prompted, select Azure PowerShell in the feature selection stage. You’ll notice that  the new Azure PowerShell when you do a search or in Al  Programs in your computer.

Connecting to your subscription

In order to manage a tenant first the Azure PowerShell needs to be connected to an active subscription. There are two methods for this. Using a downloaded management certificate which include the subscription information, or by logging into Microsoft Azure using your Microsoft Account associated to that subscription. Note that the  Azure AD will perform the credential authentication in the latter method.

OK I’m all set. Now what can I do with Azure PowerShell?

Azure PowerShell provides a large number of cmdlets that can be used to provision, deploy, manage & maintain Azure services. These includes creating, modifying & deleting of VMs, VM networks, cloud services, storage, web sites etc… Much like in Windows PowerShell there is a comprehensive help content of each and every one of these cmdlets.

I’ve included some articles that I found on how to use Azure PowerShell. Also you can create PowerShell scripts and locally run them in your on premise infrastructure to manage your cloud tenant. The power is up-to you to automate.

Resources

  1. Configuring Azure PowerShell
  2. Provisioning VMs with Azure cmdlets

Introducing Automation | Microsoft Azure

It’s been a long time from my last post and I sincerely apologize for that. I was busy with an Office 365 migration project last month and had all my eyes and ears focused to it. Today I’m going to explain how you can automate your cloud workflows with Azure Automation.

Azure Automation is a preview feature (upcoming GA) that allows us to automate creation, deployment, monitoring, and maintenance of Azure resources. It allows you to create Runbooks to orchestrate time consuming and repetitive workloads that you have in you cloud tenant. These Runbooks are indeed powered by Windows PowerShell workflows.

Currently Azure automation supports the automation of following services.

  • Web Sites (management)
  • Cloud Services (management)
  • Virtual Machines (management and WinRM support)
  • Storage (management)
  • SQL Server (management and SQL support)

Now if you know your way around PowerShell, Automation saves you a great time of time and labour. Imagine that you’ve been assigned to create 100 VMs in the cloud with exact same properties. Rather than writing a standard PowerShell script you can play with a PowerShell workflow that triggers an Azure Automation Job.

When we look at the pricing factor, as of right now this feature is still in preview stage. So you won’t be charged up to 20 Runbooks in the Free tier.

Here are some great resources that you can learn more about Azure Automation.

  1. Creating a sample Runbook in Azure
  2. Getting started with Azure Automation by Keith Mayer
  3. Automation Library