0

I have a powershell script which is a function that accepts parameters to enable the script copy existing azure NSG rules from one NSG to another.

I noticed that when i run the script and supply the values to the parameters on the same line i get the error below.

Error when the parameters are passed on the same line

however the script works fine when i don't pass any parameters on the command line and I just run the script. I am prompted to enter the necessary values.

enter image description here

I would like to know why the error happens saying the script is not a cmdlet. Please what am I missing.

I have tried looking for solution everywhere but I can't find anything at the moment.

see script below

Function Copy-AzNSGRules { <# .SYNOPSIS

Copies Azure NSG security rules from one NSG to another

.DESCRIPTION

Copies all the Azure Network Security Group security rules 
from one Network Security Group to another Network Security Group.

It can also create new Network Security Group if the target Network Security 
doesn't exist.

.PARAMETER SourceResourceGroupName
Specify the source Resource Group Name

.PARAMETER SourceNSGName
Specify the source Network Security Group Name

.PARAMETER TargetNSGName
Specify the target Network Security Group Name

.PARAMETER SourceNSG
Specify the source Network Security Group

.PARAMETER TargetNSG
Specify the target Network Security Group

.PARAMETER TargetResourceGroupName
Specify the target Resource Group Name to create new Network Security Group

.PARAMETER TargetLocation
Specify the location to create new Network Security Group

.INPUTS
None

.OUTPUTS
System.String. Information

.EXAMPLE

 . .\Copy-AzNSGRules.ps1
PS C:\> Copy-AzNSGRules -SourceResourceGroupName 'rg1' -SourceNSGName 'nsg1' -TargetResourceGroupName 'rg2' -TargetNSGName 'nsg2'

To copy security rules from the existing source NSG to existing target NSG

Output:
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

.EXAMPLE

 . .\Copy-AzNSGRules.ps1
PS C:\> Copy-AzNSGRules -SourceResourceGroupName 'rg1' -SourceNSGName 'nsg1' -TargetNSGName 'nsg2' -TargetResourceGroupName 'rg2' -TargetLocation 'southindia'

To create a new NSG and then copy security rules from the existing source NSG

Output:
New NSG 'nsg2' has been created in resource group 'rg2' in 'southindia' location.
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

(If the target NSG is already existed)
Output:
The NSG 'nsg2' is already existed, so vomiting the '-TagetLocation' parameter value and skiping the NSG creation.
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

.EXAMPLE

 . .\Copy-AzNSGRules.ps1
PS C:\> $nsg1 = Get-AzNetworkSecurityGroup -ResourceGroupName 'rg1' -Name 'nsg1'
PS C:\> $nsg2 = Get-AzNetworkSecurityGroup -ResourceGroupName 'rg2' -Name 'nsg2'
PS C:\> Copy-AzNSGRules -SourceNSG $nsg1 -TargetNSG $nsg2

To copy security rules from the existing source NSG to existing target NSG (When direct NSG objects are provided)

Output:
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

.EXAMPLE

 . .\Copy-AzNSGRules.ps1
PS C:\> $nsg1 = Get-AzNetworkSecurityGroup -ResourceGroupName 'rg1' -Name 'nsg1'
PS C:\> Copy-AzNSGRules -SourceNSG $nsg1 -TargetNSGName 'nsg2' -TargetResourceGroupName 'rg2' -TargetLocation 'southindia'

To create a new NSG and then copy security rules from the existing source NSG (When direct source NSG object is provided)

Output:
New NSG 'nsg2' has been created in resource group 'rg2' in 'southindia' location.
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

(If the target NSG is already existed)
Output:
The NSG 'nsg2' is already existed, so vomiting the '-TagetLocation' parameter value and skiping the NSG creation.
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

.NOTES

This function will accept the following Parameter Sets...

Copy-AzNSGRules -SourceResourceGroupName <string> -SourceNSGName <string> -TargetResourceGroupName <string> -TargetNSGName <string> [<CommonParameters>]

Copy-AzNSGRules -SourceResourceGroupName <string> -SourceNSGName <string> -TargetResourceGroupName <string> -TargetNSGName <string> -TargetLocation <string> [<CommonParameters>]

Copy-AzNSGRules -SourceNSG <psobject> -TargetResourceGroupName <string> -TargetNSGName <string> -TargetLocation <string> [<CommonParameters>]

Copy-AzNSGRules -SourceNSG <psobject> -TargetNSG <psobject> [<CommonParameters>]

#>

# Default Parameterset Name is 'Name'
[CmdLetBinding(DefaultParameterSetName = 'Name')]
param
(
    # Source Resource Group Name
    [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [string] $SourceResourceGroupName,
    
    # Source NSG Name
    [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [string] $SourceNSGName,

    # Source NSG Object
    [Parameter(Mandatory = $true, ParameterSetName = 'NSG')]
    [Parameter(Mandatory = $true, ParameterSetName = 'NewNSG')]
    [psobject] $SourceNSG,
    
    # Target Resource Group Name
    [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [Parameter(Mandatory = $true, ParameterSetName = 'NewNSG')]
    [string] $TargetResourceGroupName,

    # Target NSG Name
    [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [Parameter(Mandatory = $true, ParameterSetName = 'NewNSG')]
    [string] $TargetNSGName,

    # Target NSG Object
    [Parameter(Mandatory = $true, ParameterSetName = 'NSG')]
    [psobject] $TargetNSG,

    # Target location, NSG to be created 
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [Parameter(Mandatory = $true, ParameterSetName = 'NewNSG')]
    [string] $TargetLocation

)

# Check for source NSG, value by name
if ($PSCmdlet.ParameterSetName -eq 'Name' -or $PSCmdlet.ParameterSetName -eq 'CreateNew')
{
    try 
    { 
        Write-Host "Info: Checking for source NSG '$SourceNSGName'..." -ForegroundColor Green
        $SourceNSG = Get-AzNetworkSecurityGroup -ResourceGroupName $SourceResourceGroupName -Name $SourceNSGName -ErrorAction Stop 
        Write-Host ("Info: Source NSG '{0}' is found and it has {1} following security rules...`n{2}" -f $SourceNSGName, $SourceNSG.SecurityRules.Count, ($SourceNSG.SecurityRules.Name -join ', ') ) -ForegroundColor Green

    }
    catch 
    { 
        Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red 
        return 
    }
}

# Check for source NSG, value by NSG object
if ($PSCmdlet.ParameterSetName -eq 'NSG' -or $PSCmdlet.ParameterSetName -eq 'NewNSG')
{
    Write-Host ("Info: Source NSG '{0}' has {1} following security rules...`n{2}" -f $SourceNSG.Name, $SourceNSG.SecurityRules.Count, ($SourceNSG.SecurityRules.Name -join ', ') ) -ForegroundColor Green
}

if ($SourceNSG.SecurityRules.Count -le 0)
{
    # When source NSG doesn't have any security rules
    Write-Host ("Error: No security rules found on source NSG {0}" -f $SourceNSG.Name ) -ForegroundColor Red
    return
}

# Check for target NSG, value by name
if ($PSCmdlet.ParameterSetName -eq 'Name')
{
    try 
    { 
        Write-Host "Info: Checking for target NSG '$TargetNSGName'..." -ForegroundColor Green
        $TargetNSG = Get-AzNetworkSecurityGroup -ResourceGroupName $TargetResourceGroupName -Name $TargetNSGName -ErrorAction Stop 
        Write-Host "Info: Target NSG '$TargetNSGName' is found and ready to copy security rules from source NSG." -ForegroundColor Green
    }
    catch
    { 
        Write-Host "Error: Since there is no NSG with the name '$TargetNSGName' in '$TargetResourceGroupName', please specify '-TargetLocation' parameter to create a new NSG and copy the security rules." -ForegroundColor Red 
        return
    }
}

# When target NSG doesn't exist, value by name and NSG object
if ($PSCmdlet.ParameterSetName -eq 'CreateNew' -or $PSCmdlet.ParameterSetName -eq 'NewNSG')
{
    # Check for target NSG, if it doesn't exist then create new else continue
    if ($null -eq ($TargetNSG = Get-AzNetworkSecurityGroup -ResourceGroupName $TargetResourceGroupName -Name $TargetNSGName -ErrorAction SilentlyContinue))
    { 
        Write-Host "Info: Target NSG '$TargetNSGName' doesn't exist in '$TargetResourceGroupName' and will be creating new NSG..."
        # Create Resource Group if it doesn't exist 
        try 
        { 
            Write-Host "Info: Checking for Resource Group '$TargetResourceGroupName'..."
            $null = Get-AzResourceGroup -Name $TargetResourceGroupName -Location $TargetLocation -ErrorAction Stop 
            Write-Host "Info: Found an existing Resource Group '$TargetResourceGroupName', and skiping the Resource Group creation."
        }
        catch 
        { 
            Write-Host "Info: Resource Group '$TargetResourceGroupName' doesn't exist and will be creating new Resource Group."
            $null = New-AzResourceGroup -Name $TargetResourceGroupName -Location $TargetLocation 
            Write-Host "Info: Resource Group '$TargetResourceGroupName' has been created in $TargetLocation location."
        }
        $TargetNSG = New-AzNetworkSecurityGroup -ResourceGroupName $TargetResourceGroupName -Name $TargetNSGName -Location $TargetLocation
        Write-Host ("Info: New NSG '{0}' has been created in resource group '{1}' in '{2}' location." -f $TargetNSGName, $TargetResourceGroupName, $TargetLocation ) -ForegroundColor Green
    }
    else 
    {
        Write-Host ("Warning: The NSG '{0}' is already existed, so vomiting the '-TagetLocation' parameter value and skiping the NSG creation." -f $TargetNSGName) -ForegroundColor Yellow
    }
}

# For all scenarios incluing when NSG objects are provided
try
{
    Write-Host ("Info: Copying security rules from the source nsg '{0}' to target nsg '{1}'..." -f $SourceNSG.Name, $TargetNSG.Name) -ForegroundColor Green
    # Add source NSG security rules to target NSG
    $TargetNSG.SecurityRules = $SourceNSG.SecurityRules

    # Update target NSG
    $null = Set-AzNetworkSecurityGroup -NetworkSecurityGroup $TargetNSG -ErrorAction Stop  # $null used to supress the output

    # Success information
    Write-Host ("Info: Following {0} security rule(s) is/are copied from source NSG '{1}\{2}' to target NSG '{3}\{4}'" -f $SourceNSG.SecurityRules.Count, $SourceNSG.ResourceGroupName, $SourceNSG.Name, $TargetNSG.ResourceGroupName, $TargetNSG.Name) -ForegroundColor Green
    Write-Host ($SourceNSG.SecurityRules.Name -join ', ') -ForegroundColor Green
}
catch
{
    Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red 
}

}

Copy-AzNSGRules

3
  • Remove Copy-AzNSGRules from the bottom line of the script, then dot-source the script file to persist the function definition in your shell: . .\Copy-AzNSGRules.ps1 <-- after that Copy-AzNSGRules should be discoverable in the given shell Commented Jan 24, 2023 at 17:21
  • Please (also) post your code, data, error messages as (properly formatted) text, not (just) as images. Commented Jan 24, 2023 at 17:53
  • Hello @MathiasR.Jessen your suggestion fixed the problem. Now I now what to look at should I ever have the problem again. Thank you! Commented Jan 25, 2023 at 18:37

1 Answer 1

1

I feel like this is a common error for people just starting out with PowerShell. You have a script that contains nothing but a function, and a call to that function. There's a couple ways that you can approach this to solve your issue.

First you can do exactly what Mathias R. Jessen suggested, which is what I would strongly recommend. To do that remove the call to the function from the script (just delete the last line entirely). After that dot source the script to load the function to your PowerShell session and call the function as needed. For example:

. .\CopyAzNSGRules.ps1
Copy-AzNSGRules -SourceResourceGroupName $SourceRG -SourceNSGName $SourceNSGName -TargetResourceGroupName $TargetRG -TargetNSG $TargetNSG

That will load the function to the session with the first line, then you can call the function with the second line declaring all of your parameters.

Alternatively you can remove the function part entirely so that the script itself has all of the parameters, and call the script with parameters like you have been. Then your script would look like this:

<# .SYNOPSIS
Copies Azure NSG security rules from one NSG to another

.DESCRIPTION

Copies all the Azure Network Security Group security rules 
from one Network Security Group to another Network Security Group.

It can also create new Network Security Group if the target Network Security 
doesn't exist.

.PARAMETER SourceResourceGroupName
Specify the source Resource Group Name

.PARAMETER SourceNSGName
Specify the source Network Security Group Name

.PARAMETER TargetNSGName
Specify the target Network Security Group Name

.PARAMETER SourceNSG
Specify the source Network Security Group

.PARAMETER TargetNSG
Specify the target Network Security Group

.PARAMETER TargetResourceGroupName
Specify the target Resource Group Name to create new Network Security Group

.PARAMETER TargetLocation
Specify the location to create new Network Security Group

.INPUTS
None

.OUTPUTS
System.String. Information

.EXAMPLE

 . .\Copy-AzNSGRules.ps1
PS C:\> Copy-AzNSGRules -SourceResourceGroupName 'rg1' -SourceNSGName 'nsg1' -TargetResourceGroupName 'rg2' -TargetNSGName 'nsg2'

To copy security rules from the existing source NSG to existing target NSG

Output:
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

.EXAMPLE

 . .\Copy-AzNSGRules.ps1
PS C:\> Copy-AzNSGRules -SourceResourceGroupName 'rg1' -SourceNSGName 'nsg1' -TargetNSGName 'nsg2' -TargetResourceGroupName 'rg2' -TargetLocation 'southindia'

To create a new NSG and then copy security rules from the existing source NSG

Output:
New NSG 'nsg2' has been created in resource group 'rg2' in 'southindia' location.
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

(If the target NSG is already existed)
Output:
The NSG 'nsg2' is already existed, so vomiting the '-TagetLocation' parameter value and skiping the NSG creation.
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

.EXAMPLE

 . .\Copy-AzNSGRules.ps1
PS C:\> $nsg1 = Get-AzNetworkSecurityGroup -ResourceGroupName 'rg1' -Name 'nsg1'
PS C:\> $nsg2 = Get-AzNetworkSecurityGroup -ResourceGroupName 'rg2' -Name 'nsg2'
PS C:\> Copy-AzNSGRules -SourceNSG $nsg1 -TargetNSG $nsg2

To copy security rules from the existing source NSG to existing target NSG (When direct NSG objects are provided)

Output:
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

.EXAMPLE

 . .\Copy-AzNSGRules.ps1
PS C:\> $nsg1 = Get-AzNetworkSecurityGroup -ResourceGroupName 'rg1' -Name 'nsg1'
PS C:\> Copy-AzNSGRules -SourceNSG $nsg1 -TargetNSGName 'nsg2' -TargetResourceGroupName 'rg2' -TargetLocation 'southindia'

To create a new NSG and then copy security rules from the existing source NSG (When direct source NSG object is provided)

Output:
New NSG 'nsg2' has been created in resource group 'rg2' in 'southindia' location.
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

(If the target NSG is already existed)
Output:
The NSG 'nsg2' is already existed, so vomiting the '-TagetLocation' parameter value and skiping the NSG creation.
Following 2 security rule(s) is/are copied from source NSG 'rg1\nsg1' to target NSG 'rg2\nsg2'
Deny_Internet, Allow_SqlServer

.NOTES

This function will accept the following Parameter Sets...

Copy-AzNSGRules -SourceResourceGroupName <string> -SourceNSGName <string> -TargetResourceGroupName <string> -TargetNSGName <string> [<CommonParameters>]

Copy-AzNSGRules -SourceResourceGroupName <string> -SourceNSGName <string> -TargetResourceGroupName <string> -TargetNSGName <string> -TargetLocation <string> [<CommonParameters>]

Copy-AzNSGRules -SourceNSG <psobject> -TargetResourceGroupName <string> -TargetNSGName <string> -TargetLocation <string> [<CommonParameters>]

Copy-AzNSGRules -SourceNSG <psobject> -TargetNSG <psobject> [<CommonParameters>]

#>

# Default Parameterset Name is 'Name'
[CmdLetBinding(DefaultParameterSetName = 'Name')]
param
(
    # Source Resource Group Name
    [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [string] $SourceResourceGroupName,
    
    # Source NSG Name
    [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [string] $SourceNSGName,

    # Source NSG Object
    [Parameter(Mandatory = $true, ParameterSetName = 'NSG')]
    [Parameter(Mandatory = $true, ParameterSetName = 'NewNSG')]
    [psobject] $SourceNSG,
    
    # Target Resource Group Name
    [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [Parameter(Mandatory = $true, ParameterSetName = 'NewNSG')]
    [string] $TargetResourceGroupName,

    # Target NSG Name
    [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [Parameter(Mandatory = $true, ParameterSetName = 'NewNSG')]
    [string] $TargetNSGName,

    # Target NSG Object
    [Parameter(Mandatory = $true, ParameterSetName = 'NSG')]
    [psobject] $TargetNSG,

    # Target location, NSG to be created 
    [Parameter(Mandatory = $true, ParameterSetName = 'CreateNew')]
    [Parameter(Mandatory = $true, ParameterSetName = 'NewNSG')]
    [string] $TargetLocation

)

# Check for source NSG, value by name
if ($PSCmdlet.ParameterSetName -eq 'Name' -or $PSCmdlet.ParameterSetName -eq 'CreateNew')
{
    try 
    { 
        Write-Host "Info: Checking for source NSG '$SourceNSGName'..." -ForegroundColor Green
        $SourceNSG = Get-AzNetworkSecurityGroup -ResourceGroupName $SourceResourceGroupName -Name $SourceNSGName -ErrorAction Stop 
        Write-Host ("Info: Source NSG '{0}' is found and it has {1} following security rules...`n{2}" -f $SourceNSGName, $SourceNSG.SecurityRules.Count, ($SourceNSG.SecurityRules.Name -join ', ') ) -ForegroundColor Green

    }
    catch 
    { 
        Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red 
        return 
    }
}

# Check for source NSG, value by NSG object
if ($PSCmdlet.ParameterSetName -eq 'NSG' -or $PSCmdlet.ParameterSetName -eq 'NewNSG')
{
    Write-Host ("Info: Source NSG '{0}' has {1} following security rules...`n{2}" -f $SourceNSG.Name, $SourceNSG.SecurityRules.Count, ($SourceNSG.SecurityRules.Name -join ', ') ) -ForegroundColor Green
}

if ($SourceNSG.SecurityRules.Count -le 0)
{
    # When source NSG doesn't have any security rules
    Write-Host ("Error: No security rules found on source NSG {0}" -f $SourceNSG.Name ) -ForegroundColor Red
    return
}

# Check for target NSG, value by name
if ($PSCmdlet.ParameterSetName -eq 'Name')
{
    try 
    { 
        Write-Host "Info: Checking for target NSG '$TargetNSGName'..." -ForegroundColor Green
        $TargetNSG = Get-AzNetworkSecurityGroup -ResourceGroupName $TargetResourceGroupName -Name $TargetNSGName -ErrorAction Stop 
        Write-Host "Info: Target NSG '$TargetNSGName' is found and ready to copy security rules from source NSG." -ForegroundColor Green
    }
    catch
    { 
        Write-Host "Error: Since there is no NSG with the name '$TargetNSGName' in '$TargetResourceGroupName', please specify '-TargetLocation' parameter to create a new NSG and copy the security rules." -ForegroundColor Red 
        return
    }
}

# When target NSG doesn't exist, value by name and NSG object
if ($PSCmdlet.ParameterSetName -eq 'CreateNew' -or $PSCmdlet.ParameterSetName -eq 'NewNSG')
{
    # Check for target NSG, if it doesn't exist then create new else continue
    if ($null -eq ($TargetNSG = Get-AzNetworkSecurityGroup -ResourceGroupName $TargetResourceGroupName -Name $TargetNSGName -ErrorAction SilentlyContinue))
    { 
        Write-Host "Info: Target NSG '$TargetNSGName' doesn't exist in '$TargetResourceGroupName' and will be creating new NSG..."
        # Create Resource Group if it doesn't exist 
        try 
        { 
            Write-Host "Info: Checking for Resource Group '$TargetResourceGroupName'..."
            $null = Get-AzResourceGroup -Name $TargetResourceGroupName -Location $TargetLocation -ErrorAction Stop 
            Write-Host "Info: Found an existing Resource Group '$TargetResourceGroupName', and skiping the Resource Group creation."
        }
        catch 
        { 
            Write-Host "Info: Resource Group '$TargetResourceGroupName' doesn't exist and will be creating new Resource Group."
            $null = New-AzResourceGroup -Name $TargetResourceGroupName -Location $TargetLocation 
            Write-Host "Info: Resource Group '$TargetResourceGroupName' has been created in $TargetLocation location."
        }
        $TargetNSG = New-AzNetworkSecurityGroup -ResourceGroupName $TargetResourceGroupName -Name $TargetNSGName -Location $TargetLocation
        Write-Host ("Info: New NSG '{0}' has been created in resource group '{1}' in '{2}' location." -f $TargetNSGName, $TargetResourceGroupName, $TargetLocation ) -ForegroundColor Green
    }
    else 
    {
        Write-Host ("Warning: The NSG '{0}' is already existed, so vomiting the '-TagetLocation' parameter value and skiping the NSG creation." -f $TargetNSGName) -ForegroundColor Yellow
    }
}

# For all scenarios incluing when NSG objects are provided
try
{
    Write-Host ("Info: Copying security rules from the source nsg '{0}' to target nsg '{1}'..." -f $SourceNSG.Name, $TargetNSG.Name) -ForegroundColor Green
    # Add source NSG security rules to target NSG
    $TargetNSG.SecurityRules = $SourceNSG.SecurityRules

    # Update target NSG
    $null = Set-AzNetworkSecurityGroup -NetworkSecurityGroup $TargetNSG -ErrorAction Stop  # $null used to supress the output

    # Success information
    Write-Host ("Info: Following {0} security rule(s) is/are copied from source NSG '{1}\{2}' to target NSG '{3}\{4}'" -f $SourceNSG.SecurityRules.Count, $SourceNSG.ResourceGroupName, $SourceNSG.Name, $TargetNSG.ResourceGroupName, $TargetNSG.Name) -ForegroundColor Green
    Write-Host ($SourceNSG.SecurityRules.Name -join ', ') -ForegroundColor Green
}
catch
{
    Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red 
}

Then you call it like you have been:

PS C:\Users\TMTech> . .\Copy-AzNSGRules.ps1 -SourceResourceGroupName $SourceRG -SourceNSGName $SourceNSGName -TargetResourceGroupName $TargetRG -TargetNSG $TargetNSG
Sign up to request clarification or add additional context in comments.

1 Comment

Your initial Assumption was correct, I am new at PowerShell. Your suggestion which was similar to Mathias Solved the Problem. Thank you guys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.