Link to home
Start Free TrialLog in
Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

powershell case instead of if statements

Hi All,

I have this powershell script , instead of using if statements i want to use case, or anything better:

$VMs = Get-AzureRmResource -ResourceType Microsoft.Compute/virtualMachines


foreach ($vm in $VMs) {

write-host -ForegroundColor Red "Processing $($vm.name) "

     

if ($vm.tags.keys -eq "displayname") 

    {

    write-host -ForegroundColor green "Tag Key $($vm.tags.keys) Exists in $($vm.name)"

    }

else {

     write-host -ForegroundColor white "Tag Key $($vm.tags.keys) DOES NOT Exists in $($vm.name)"
     #create key
     #try {}
     #catch {}


     }

If ($vm.tags.Values -eq "KayTestVM" ) 

    {

        write-host -ForegroundColor Green "Tag Value $($vm.tags.Values) Exists in $($vm.name)"



    }

Else {


        write-host -ForegroundColor White "Tag Value $($vm.tags.Values) DOES NOT Exists in $($vm.name)"
        #create value
        #try {}
        #catch {}

            
            
      }

} #End Foreach

Open in new window



$VMs = Get-AzureRmResource -ResourceType Microsoft.Compute/virtualMachines


foreach ($vm in $VMs) {

write-host -ForegroundColor Red "Processing $($vm.name) "

$tags = $vm.tags

switch ($tags)

    {

    $tags.keys ( )
    $tags.values ( ) 



    }



} #End Foreach

Open in new window



and is there a better way of doing this?

thank you in advance
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kelly Garcia

ASKER

Thank you odba, how do i add a log for ones that already exists
Avatar of oBdA
oBdA

You just define a log file and add whatever content you want.
$LogFile = "C:\Temp\whatever.log"
$VMs = Get-AzureRmResource -ResourceType Microsoft.Compute/virtualMachines

ForEach ($vm in $VMs) {
	Write-Host -ForegroundColor Red "Processing $($vm.name) "
	
	If ($vm.tags.Keys -contains "Displayname") {
		Write-Host -ForegroundColor Green "Tag Key $($vm.tags.keys) exists in $($vm.name)"
		"Tag Key $($vm.tags.keys) exists in $($vm.name)" | Add-Content $LogFile
	} Else {
		Write-Host -ForegroundColor White "Tag Key $($vm.tags.keys) DOES NOT exist in $($vm.name)"
		#create key
		#try {
		#} catch {
		#}
	}

	If ($vm.tags.Values -contains "KayTestVM") {
		Write-Host -ForegroundColor Green "Tag Value $($vm.tags.Values) exists in $($vm.name)"
		"Tag Value $($vm.tags.Values) exists in $($vm.name)" | Add-Content $LogFile
    } Else {
		Write-Host -ForegroundColor White "Tag Value $($vm.tags.Values) DOES NOT exist in $($vm.name)"
		#create value
		#try {
		#}catch {}
		#}
	}
} #End Foreach

Open in new window