1

I've been trying to run a script to create a lock on azure resource to prevent resources being deleted inadvertently. I get an error message and I can't figure out why it's showing me this error message.

Script:

    #Sign in to Azure account
Login-AzAccount

#Select the subscription you want to work on
Select-AzSubscription -Subscription "test.subscription"

#Get All Resources in a resource group
$Resources = Get-AzResource -ResourceGroupName dummy_rg | Format-Table

# Create lock "delete" on each Resource if it doesn't exist
foreach($Resource in $Resources) {

    $ResourceName = $Resource.Name
    $lck = Get-AzResourceLock -ResourceGroupName $Resource.ResourceGroupName -ResourceName $ResourceName -ResourceType $Resource.ResourceType
    
    if ($null -eq $lck)
    {
    Write-Host "$ResourceName has no lock"
    
    New-AzResourceLock -resourceGroupName $rg -ResourceName $ResourceName -ResourceType $Resource.ResourceType -LockName "$ResourceName-lck" -LockLevel CanNotDelete -Force
    
    Write-Host "$ResourceName has been locked"
    
    }
    else 
    {
    Write-host "$ResourceName already locked"
    }
    
    }

Error message:

enter image description here

Gaurav request result:

enter image description here

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Leanne Kami
  • 79
  • 1
  • 13
  • It looks like `$Resource.ResourceGroupName` is empty. Can you please check that? – Martin Brandl Oct 21 '21 at 13:43
  • How are you fetching the resources? – Gaurav Mantri Oct 21 '21 at 14:02
  • My bad I forgot to add a part of my code at the beginning. I fixed it just now. That 1st line might be the issue tho. – Leanne Kami Oct 21 '21 at 14:04
  • I am able to run your code just fine. Can you check if you're getting ResourceGroupName back in the properties when you list resources? Please do something like `Get-AzResource -ResourceGroupName dummy_rg | ft`. – Gaurav Mantri Oct 21 '21 at 14:33
  • You have a typo there in the error logs $Resource.ResouceGroupName (Forgot an "r") – Nadine Raiss Oct 21 '21 at 15:32
  • @GauravMantri I've updated with the full script, the command you suggested is not null yet I still have the error message. – Leanne Kami Oct 21 '21 at 16:10
  • Instead of screenshot, please paste actual code. Easy to copy . – Gaurav Mantri Oct 21 '21 at 16:12
  • @GauravMantri Just did, I realized it makes more sense. – Leanne Kami Oct 21 '21 at 16:12
  • Can you try this simple script: `Login-AzAccount #Select the subscription you want to work on Select-AzSubscription -Subscription "test.subscription" #Get All Resources in a resource group $Resources = Get-AzResource -ResourceGroupName dummy_rg | Format-Table $Resources`. This is to ensure that you're seeing resource group name in the output of list resources. – Gaurav Mantri Oct 21 '21 at 16:21
  • Just did, see error message2 in the post for full result @GauravMantri – Leanne Kami Oct 21 '21 at 16:28
  • Stack overflow’s formatting sucks in comments . Can you please have $Resources in a separate line? So first line should be just $Resources = Get-AzResource -ResourceGroupName dummy_rg | Format-Table and the next line should be $Resources. – Gaurav Mantri Oct 21 '21 at 16:34
  • My bad I should've figured, see edited picture in post haha - Btw correct subscription and resource group are being selected here. – Leanne Kami Oct 21 '21 at 16:41

1 Answers1

0
#Start logging
Start-Transcript -Path "C:\Windows\Logs\Lock - $(((get-date).ToUniversalTime()).ToString("yyyy-MM-dd_hh-mm-ss")).log" -Force

#Connect to Azure account
Login-AzAccount

#Select Azure subscription
Set-AzContext -Subscription "subscription_id_numbers"
#Deny rule on Azure Data Factory and Azure Machine Learning
$Resources = Get-AzResource | Where-Object {$_.Name -NotLike '*adf*' -and $_.Name -NotLike '*aml*'}

# Create lock "delete" on each Resource if it doesn't exist

foreach($Resource in $Resources) {

$ResourceName = $Resource.Name
$lck = Get-AzResourceLock -ResourceGroupName $Resource.ResourceGroupName -ResourceName $ResourceName -ResourceType $Resource.ResourceType

if ($lck -eq $null)
{
Write-Host "$ResourceName has no lock"

Set-AzResourceLock -ResourceGroupName $Resource.ResourceGroupName -ResourceName $ResourceName -ResourceType $Resource.ResourceType -LockName "$ResourceName-lck" -LockLevel CanNotDelete -Force

Write-Host "$ResourceName has been locked"

}
else 
{
Write-host "$ResourceName already locked"
}

}

#Stop Logging
Stop-Transcript

This will loop on every ressources except azure data factory in the tenant and create a "delete" type lock to make sure resources aren't deleted inadvertently. Read comments in each section to understand the code.

Leanne Kami
  • 79
  • 1
  • 13
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 23 '22 at 01:58