Kelly Garcia
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:
and is there a better way of doing this?
thank you in advance
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
$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
and is there a better way of doing this?
thank you in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER