unable to run remove scirpt in Powershell

alanlam123
alanlam123 used Ask the Experts™
on
Hi expert

good day

I am encountered an issued ,when i tried to execute the following command " Remove-20533DEnvironment" and error messages show in the screenshot.

I have attach the log files for your reference.
Remove-20533DEnvironment-1.log
Capture-for-powershell-issue-250418.PNG
Remove-script.docx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018
Commented:
You are cleaning up variables in the global scope that are in the "local" scope. A global variable in the global scope must be with the "$global:<variablename>".
then you can use
Remove-Variable -scope Global <variablename>

You would probably try it as a script 1st, then you can transform it to a function, you can run it on ISE.
try{
    # We want to see any errors...
    $ErrorActionPreference='Continue'

    # Store the start time
    $startTime = Get-Date

    Add-AzureRmAccount

    # Select the target subscription    
    Select-20533DSubscriptionARM

    $subscriptionName = (Get-AzureRmSubscription -SubscriptionId $Global:20533DsubscriptionIdGlobal -ErrorAction SilentlyContinue).Name
    If (!($subscriptionName)) {
        $subscriptionName = (Get-AzureRmSubscription -SubscriptionId $global:20533DsubscriptionIdGlobal -ErrorAction SilentlyContinue).SubscriptionName
    }

    if (!($Global:20533DlabNumberGlobal)) {
        Do {
            Write-Host -NoNewline "Which lab environment do you want to remove? Type a number from 1 - 11:   " -ForegroundColor Magenta
            $labNumber = Read-Host 
        } While ((1..11) -notcontains $labNumber)

        $global:20533DlabNumberGlobal = $labNumber
    }
    Do {
        # Confirm with user before proceeding
        $labNumberTwoDigit = ([int]$Global:20533DlabNumberGlobal).ToString("00")
        Write-Host "This script will remove 20533D lab $Global:20533DlabNumberGlobal environment from the subscription $subscriptionName" -ForegroundColor Magenta
        Write-Host "The script deletes all resource groups with names starting with 20533D$labNumberTwoDigit and their resources" -ForegroundColor Magenta 
        Write-Host "To remove 20533D environment for a different lab, press D when prompted " -ForegroundColor Magenta 
        Write-Host "Do you want to proceed? Y/N/D?: "  -ForegroundColor Magenta
        $answer = read-host
        Switch ($answer)
        {
            Y {Write-Host "Deleting all objects..." -ForegroundColor Yellow}
            N {Write-Host "Terminating the script..."; Start-Sleep -Seconds 2; Return }
            D {# Get the lab number
                Write-Host
                Do {
                    Write-Host -NoNewline "Which lab environment do you want to remove? Type a number from 1 - 11:   " -ForegroundColor Magenta
                    $labNumber = Read-Host 
                } While ((1..11) -notcontains $labNumber)
                $global:20533DlabNumberGlobal = $labNumber
                $labNumberTwoDigit = ([int]$Global:20533DlabNumberGlobal).ToString("00")
                continue
            }  
            Default {continue}
        }
    } While ($answer -notmatch "[YN]")

    $rootPath = (Get-Item $PSScriptRoot).Parent.Parent.FullName
    $transcriptPath = ""
    If ($Global:20533DlabNumberGlobal) {
        $transcriptPath = Join-Path -Path $rootPath -ChildPath "Logs\Remove-20533DEnvironment-$Global:20533DlabNumberGlobal.log"
    } 
    else {
        $transcriptPath = Join-Path -Path $rootPath -ChildPath "Logs\Remove-20533DEnvironment-0.log"
    }

    Start-Transcript -Path $transcriptPath -IncludeInvocationHeader -Append -Force

    foreach ($resourceGroupName in (Get-AzureRMResourceGroup).ResourceGroupName) {
        if ($resourceGroupName -like "20533D$labNumberTwoDigit*") {
             Write-Host "Deleting $resourceGroupName resource group..."
             Remove-AzureRMResourceGroup -Name $resourceGroupName -Force -InformationAction SilentlyContinue
        }
    }

    # Display time taken for script to complete
    $endTime = Get-Date

    Write-Host "Started at $startTime" -ForegroundColor Magenta
    Write-Host "Ended at $endTime" -ForegroundColor Yellow
    Write-Host

    $elapsedTime = $endTime - $startTime

    If ($elapsedTime.Hours -ne 0){
        Write-Host "Total elapsed time is $($elapsedTime.Hours) hours $($elapsedTime.Minutes) minutes" -ForegroundColor Green
    } else {

        Write-Host "Total elapsed time is $($elapsedTime.Minutes) minutes" -ForegroundColor Green
    }

    Remove-Variable -Scope Global  20533DlabNumberGlobal, 20533DsubscriptionIdGlobal #,  20533DlocationGlobal
    
    Write-Host
    Stop-Transcript
}
catch{
    Write-Error $($_.Exception.Message)
}

Open in new window

Associate - Sys Admin
Commented:
As per your .PNG file below command will helpful to you.

Install-AzureRM. 
Get-AzureRmResourceGroup | select resourcegroupname

Open in new window




Source:
https://stackoverflow.com/questions/41592445/get-azurermresourcegroup-the-term-get-azurermresourcegroup-is-not-recognized

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial