Cannot create Azure AD B2C. How to register Azure resource providers?

I have a MSDN subscription and I tried to create an Azure Active Directory B2C tonight. After entering all the information and clicked “Create” button, it returns me an error,

The subscription is not registered to use namespace ‘Microsoft.AzureActiveDirectory’

Huh, not registered a namespace? Am I creating an application and need such reference to the related namespace? MSDN subscription cannot create AAD?

Well, yes, you will need the related namespace. No matter what tasks you are applying, they are actually executing CLI or Azure PowerShell in the background. The commands also need some namespaces in order to complete the tasks. And you may encounter such errors when using a resource provider that you haven’t previously used in your subscription. And the message might look like,

Message: No registered resource provider found for location {location} and API version {api-version} for type {resource-type}.

OR

Message: The subscription is not registered to use namespace {resource-provider-namespace}

You receive these errors for one of these reasons:

  • The required resource provider hasn’t been registered for your subscription
  • API version not supported for the resource type
  • Location not supported for the resource type
  • For auto-shutdown of VMs, the Microsoft.DevTestLab resource provider must be registered.

If you found these kind of error message, you may do the following in your Azure Portal to register (or unregister) a resource provider namespace.

  1. From the portal, select All Services.
  2. Select Subscriptions.
  3. From the list of subscriptions, select the subscription you want to use for registering the resource provider.
  4. From your subscription, select Resource providers.
  5. Look at the list of resource providers, and if necessary, select the Register button at the top menu to register the resource provider of the type you are trying to deploy.




  6. Now you could repeat your last creation of the Azure Services.  In my case, I re-create the Azure Active Directory B2C without any error this time.


[PowerShell] How to create new instance and remove variable

In the last post, “[PowerShell] How to create simple class“, I used the New-Object to create a new instance of my custom class “Vehicle”. The New-Object cmdlet creates an instance of a .NET Framework or COM object.

$myVehicle = New-Object Vehicle

Using the New-Object cmdlet, we could also do the property assignments on the same line when creating the new instance, Just like C#, as shown here:

    $my2ndVehicle = New-Object Vehicle -Property @{RegPlate=”MVPKenLin_2″; NoOfDoors=5; Year=”10/6/2021″; Model=”BMW X3″}
    $my2ndVehicle

 

There is another simplify way, it is new() static method from the Vehicle class. The syntax is shown here:

$PaulVehicle = [Vehicle]::new()

This code and the returned object are shown here:

Now I can assign values to the new created instance (variable which named “PaulVehicle”).

    $PaulVehicle.RegPlate = “PaulV”
    $PaulVehicle.NoOfDoors = 3
    $PaulVehicle.Year = “10/06/2021”
    $PaulVehicle.Model = “Mercedes”
    $PaulVehicle
Now the returned result are shown here:

 

TIPS!

Now if we could like to clear all contents of the instance, we could use Clear-Variable. The Clear-Variable cmdlet deletes the data stored in a variable, but it does not delete the variable. As a result, the value of the variable is NULL (empty). If the variable has a specified data or object type, this cmdlet preserves the type of the object stored in the variable.

If we would like to remove/delete it, we could use Remove-Variable. The Remove-Variable cmdlet deletes a variable and its value from the scope in which it is defined, such as the current session. You cannot use this cmdlet to delete variables that are set as constants or those that are owned by the system.

The code and the result shown here:

Clear-Variable -name PaulVehicle
Remove-Variable -name PaulVehicle

It is always a good practice to clear or delete the variables that you are no longer access them if your script will consume lots of memory .

[PowerShell] How to Create Simple Class

As a C# developer, I think that I should need to know how to create a class in PowerShell. Since Windows PowerShell 5.0, it adds the ability to create a class. With a class, we could do the coding more like C# (this is a good news to a C# developer like me, lol). We could create methods properties, enums, and other things to Class in PowerShell.

The first thing we need is, use the Class keyword. This is a sample on it,

Class Vehicle {

}

Within the brace, we could add our items in it. Be careful when we create object in PowerShell. The Windows PowerShell automatic type system works remarkably. it allow us create any variable without specify its type. Everything will simply be a System.Object. if you have a string, an int, or a DataTime object, the best practice (or you must) is to specify that type so that things will work well.

Because I am creating a class for a vehicle, I could create something like, Vehicle Registration Plate, Model, Year, Number of Doors, Number of Wheels, Seating Capacity, Engine Displacement, Engine Max Torque….etc

[String]$RegPlate
[int]$NoOfDoors
[datetime]$Year
[String]$Model
[int]$numberOfWheels = 4
[int]$numberOfDoors = 5

I can then use the New-Object cmdlet to create an instance of the Vehicle class,
$myVehicle = New-Object Vehicle

I can now get the default values of all variables (class properties) in $myVehicle.

Now I can simply assign values to $myVehicle,

Now I could see all the updated values,

That is how you create a simple class in Windows PowerShell 5.0. Let’s talk about adding more stuff to the class later. Hope you enjoy and learn the PowerShell with me.

 

Next version of Visual Studio, “VS2022”, Public Preview will be released this summer

Amanda Silver, CVP of Product-Developer Division announce that the next version of Visual Studio will be called VS2022. And its Public Preview will be available to download this summer. Let me do a short summary about the changes and share with you.

  • IDE will be running faster
    • VS2022 will be in 64-bit
  • New Design
    • Better clarity, legibility, and contrast
    • Cascadia Code
    • Personalization, you could make your own “IDE” just right for you.
  • Develop Modern Apps
    • Azure, better integration with GitHub for CI/CD, and with Azure
    • VS2022 will have full support for .NET 6.
    • .NET Multi-platform App UI (MAUI) will be included. You could build client apps on Windows, Android, macOS, iOS. You could also use ASP.NET Blazor for Web App.
  • Innovation 
    • Performance improved for Diagnostics and Debugging
    • Using Live Share to enable Real-Time Collaboration
    • AI IntelliCode engine provide more and deeper integrations into your daily workflows.
    • New support for Git and GitHub.
    • Improved Code Search
    • refreshing VS for Mac

Well, let’s wait and test this new VS2022 in the coming summer time.  Meanwhile, you could read from the original post here.

[PowerShell] How to Join Data in faster way?

Last week, one of Hong Kong ex-MVP asked a question to our ex-MVPs private group. He has a PowerShell script and 2 csv data files. Both csv data file has a common column and he could like to join them and combine into a single csv data file. his code  like this,

Get csv1 | ForEach-Object -Parallel Where-Object $_.id -match $csv2.id

He complaints that it will take a very long time to handle large amount of data. And there are some comments,

  • change from -match to -eq
  • try to use hash
  • something about add-member
    …etc

From me, I just say, why not do it with LINQ? So I try to write 2 PowerShell scripts, one will be using traditional PowerShell command and another one will be using LINQ.

First, in my testing code, I will try to create 2 arrays and then generate some data into it. Here is my code,

#Create empty arrays
$datasetA = @()
$datasetB = @()
#Initialize "status" arrays to pull random values from
$genderArray = @('Male','Female','Decline to Answer')
#Loop 10 times to populate our separate datasets
1..10 | Foreach-Object {
    #Set the name with the current iteration attached
    $thisName = "Person_$_" 
    #Create an object with the name property and a random gender
    $rowA = @{
        Name = $thisName
        Gender = $genderArray[(Get-Random -Minimum 0 -Maximum 3)]
    }
    $datasetA += New-Object -Type PSObject -Property $rowA
    #Create a second object with the same name and a random age
    $rowB = @{
        Name = $thisName
        Age = Get-Random -Minimum 1 -Maximum 120
    }
    $datasetB += New-Object -Type PSObject -Property $rowB
}

It will create 2 array, each contains 10 rows. The common column will be the Name column.  One array contains a Gender column, another array contains Age column. Simple data, right?

In traditional PowerShell, we might iterate through one of the arrays while doing a filter on the second array. And then either add property members to the first array or create a new objects with a combination of properties from both arrays. My code then will be something like this:

$joinedDataset =@()
foreach($rowA in $datasetA) {
    $rowB = $datasetB | Where-Object Name -eq $rowA.Name
    $joinedRow = @{
        Name = $rowA.Name
        Gender = $rowA.Gender
        Age = $rowB.Age
    }
    $joinedDataset += New-Object -Type PSObject -Property $joinedRow
}

It works fine and it only takes 32.6 milliseconds to complete. But if I increase the iterate number from 10 to 100, it then takes 174.1 milliseconds. And then 7025.7 milliseconds for 1000 records, 638429.8 milliseconds (10 min 38 sec) for 10000 records.
(P.S., it include the time it take to generate iteration arrays)

Now, let’s do the same with using LINQ:

$linqJoinedDataset = [System.Linq.Enumerable]::Join(
    $datasetA,
    $datasetB,
    [System.Func[Object,string]] {param ($x);$x.Name},
    [System.Func[Object,string]]{param ($y);$y.Name},
    [System.Func[Object,Object,Object]]{
        param ($x,$y);
        New-Object -TypeName PSObject -Property @{
        Name = $x.Name;
        Gender = $x.Gender;
        Age = $y.Age}
    }
)

$OutputArray = [System.Linq.Enumerable]::ToArray($linqJoinedDataset)

The time it takes will be as following,

  • 37.8 milliseconds for 10 records
  • 79.8 milliseconds for 100 records
  • 809.4 milliseconds for 1000 records
  • 16026.2 milliseconds for 10000 records

In this code, I am calling the Join method on System.Linq.Enumerable and then passing it with 5 parameters,

  1. The first dataset that it is going to join
  2. The second dataset that it joins
  3. The delegate which defines the key to compare against on the first dataset
  4. The delegate which defines the key to compare against on the second dataset
  5. Finally, we pass in the delegate which defines what the output should be

Because the Join method is a Deferred Execution. it is not actually joining the data at that time. It is just building an expression tree which defines the relational algebra needed to perform the join. So the above example code, I will need to call “ToArray()”, so that the execution could start. If you have any further codes that you will handle with the result data, you could skip the “ToArray()” code. For example, calling like this,

$linqJoinedDataset.Where({($_.Age -gt 20) -and ($_.Gender -eq "Male")})

Then the join query would execute at that time and then “Where()” would filter down to just the objects that matching the conditions.

Here is the table on the execution time on each way,

# iterate Traditional PS LINQ
10 32.6 37.8
100 174.1 79.8
1000 7025.7 809.4
1000 638429.8 16026.2

So I can say, LINQ saved more than 40 times of timing to complete the join. With this figure, I am sure that you will love to update and use LINQ if you need a join operation on data.

Hope this blog could help you to get a faster execution script in PowerShell.