Search code examples
azurepowershellazure-storageazure-powershellwhitelist

Name of module to be imported to run this Powershell Command


I am trying to whitelist some IP addresses to my Azure storage account by using below powershell command

Add-AzStorageAccountNetworkRule -ResourceGroupName $rgName -Name $storageAccountName -IPRule $rule

Now this $rule object is an Array of type Microsoft.Azure.Commands.Management.Storage.Models.PSIpRule. Link for reference is here

So I am trying to create these objects using the below command

 New-Object -TypeName Microsoft.Azure.Commands.Management.Storage.Models.PSIpRule

I am getting below error while trying to create this object

New-Object : Cannot find type [Microsoft.Azure.Commands.Management.Storage.Models.PSIpRule]:
Verify that the assembly containing this type is loaded.

I tried googling to import the module that has this assembly, but couldn't find any. What is the module to be imported or is there any other way to fix this error?


Solution

  • #Load Assembly https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_using?view=powershell-7.2

    using asssembly [path]
    using assembly "C:\Users\administrator\Documents\PowerShell\Modules\Az.Storage\4.9.0\Microsoft.Azure.PowerShell.Cmdlets.Storage.Management.dll"
    

    After that you can create the object

    $ipRule = New-Object -TypeName Microsoft.Azure.Commands.Management.Storage.Models.PSIpRule
    

    which gives you an object with the properties Action and IpAddressOrRange.

    The import-module way (module must be stored in a location known by system variable PsModulePath, otherwise you have to specify the path):

    Import-Module az.storage
    $ipRule = New-Object -TypeName Microsoft.Azure.Commands.Management.Storage.Models.PSIpRule
    

    To identify which module to load for a specific cmdlet:

    #Ask get command about the cmdlet
    get-command [cmdlet]
    get-command Add-AzStorageaccountnetworkrule
    #Source tells you where it is coming from
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Cmdlet          Add-AzStorageAccountNetworkRule                    4.9.0      Az.Storage