How can we ignore the tags with the hidden-link while we import tags of an Azure Application Insights - azure

while importing the tags of azure resources (key,value pair) is it possible to avoid the hidden tags.How can we achieve this ?
I used the below piece of code to get the tags.
$tag=""
foreach($keys in $arr){
$tag = $tag +$keys +":"+$tags[$keys]+"`n"
}
$report | Add-Member -MemberType NoteProperty -name ResourceTags -Value $tag
$report | Add-Member -MemberType NoteProperty -name ResourceSubscription -Value $subscription_name
$report | export-csv $fileName -Append -NoTypeInformation
What does "hidden-link:" mean in Azure Resource Manager Tags
The above link shows the hidden-link for adding tags which hides a tag.
Can I get the hidden tags seperated from the list of all tags.

foreach($keys in $arr){
if($tags[$keys] -ne "Resource") {
$tag = $tag +$keys +":"+$tags[$keys]+"`n" }

Related

How to split different values in powershell by a line

With this script i am able to fetch all the Tags that a VM has but i want that in output the each key and its value should be separated by a line in the way that each key and its value appears on different lines like this
reference image
# Sign into Azure Portal
connect-azaccount
# Fetch the Virtual Machines from the subscription
$azureVMDetails = get-azvm
# Fetch the NIC details from the subscription
$azureNICDetails = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}
#Fetching Virtual Machine Details
$virtual_machine_object = $null
$virtual_machine_object = #()
#Iterating over the NIC Interfaces under the subscription
foreach($azureNICDetail in $azureNICDetails){
#Fetching the VM Name
$azureVMDetail = $azureVMDetails | ? -Property Id -eq $azureNICDetail.VirtualMachine.id
#Fetching the VM Tags
foreach($azureDetail in $azureVMDetails) {
$vm_tags = $azureVMDetail| Select-Object -Property (
#{name='Tags'; expression = {($_.tags.GetEnumerator().ForEach({ '{0} : {1}' -f $_.key, $_.value }) -join ';')}}
)
}
#VM Details export
$virtual_machine_object_temp = new-object PSObject
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "name" -Value $azureVMDetail.Name
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "comments" -Value ($vm_tags.Tags -join ';')
$virtual_machine_object += $virtual_machine_object_temp
}
#Report format and path
$virtual_machine_object | Export-Csv "C:\Users\JOHN\Desktop\Inventory\Final Scripts\VM_details_$(get-date -f dd.MM.yyyy).csv" -NoTypeInformation -Force
I tried to reproduce the same in my environment and got the results successfully by using the below PowerShell script:
$vmdeatil = Get-AzVm -Name testvm | Select -ExpandProperty Tags
$value = $vmdeatil
foreach($i in 0..($value.Count -1))
{
$ErrorActionPreference = ‘SilentlyContinue’
[array]$report += [pscustomobject] #{
key = $key[$i]
name = $value[$i]
}
}
$report | Export-Csv -Path "C:\Users\ruk1.csv" -NoTypeInformation
Response:
The output is successfully exported in the csv file like below:

how to use if/else statement to replace a value in PowerShell

function Get-vmstatus {
# Sign into Azure Portal
connect-azaccount
# Fetch the Virtual Machines from the subscription
$azureVMDetails = get-azvm
# Fetch the NIC details from the subscription
$azureNICDetails = Get-AzNetworkInterface | ? { $_.VirtualMachine -NE $null }
#Fetching Virtual Machine Details
$virtual_machine_object = $null
$virtual_machine_object = #()
#Iterating over the NIC Interfaces under the subscription
foreach ($azureNICDetail in $azureNICDetails) {
$azureVMDetail = $azureVMDetails | ? -Property Id -eq $azureNICDetail.VirtualMachine.id
$vm_status = get-azvm -ResourceGroupName $azureVMDetail.resourcegroupname -name $azureVMDetail.name -Status
$vm_tags = ($azureVMDetail.Tags.values) -join ';'
$vmsize = Get-AzVMSize -VMName $azureVMDetail.Name -ResourceGroupName $azureVMDetail.ResourceGroupName | ? { $_.Name -eq $azureVMDetail.HardwareProfile.VmSize }
$OsDisksize = $azureVMDetail.StorageProfile.OsDisk.DiskSizeGB
#Fetching the private IP
$private_ip_address = ($azureNICDetail.IpConfigurations | select-object -ExpandProperty PrivateIpAddress) -Join ';'
#VM Details export
$virtual_machine_object_temp = new-object PSObject
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "Name" -Value $azureVMDetail.Name
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "VCPUs" -Value $vmsize.NumberOfCores
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "Status" -Value $vm_status.Statuses[1].DisplayStatus
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "Memory" -Value $vmsize.MemoryInMB
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "Disk" -Value $OsDisksize
$virtual_machine_object_temp | add-member -membertype NoteProperty -name "Comments" -Value $vm_tags
$virtual_machine_object += $virtual_machine_object_temp
}
$virtual_machine_object | Export-Csv "C:\Users\mouj\Desktop\Inventory\Final Scripts\VM_details_$(get-date -f dd.MM.yyyy).csv" -NoTypeInformation -Force
}
After running the script the Status of the Azure VMs are shown as VM running, VM stopped, VM deallocated. Now i want to replace the VM running with "Active" and VM stopped, VM deallocated with "Offline". How can i do with conditional statement or is there any other way to do it ? Thanks in advance
Use Add-Member to add a ScriptProperty - a dynamic property that can reference the value(s) of other properties on the object:
$virtual_machine_object_temp | Add-Member -MemberType ScriptProperty -Name State -Value { if($_.Status -eq 'Running'){ 'Active' }else{ 'Offline' } }
Now, whenever you look at the new State property, it will resolve to Active whenever the Status property is Running, otherwise Offline

Programatically Get ADF pipeline consumption report

I'm interested in querying the pipeline consumption report that is available from the Data Factory monitor. Is there a table on Log Analytics or PowerShell cmdlet that would return this information? I checked the ADFv2 PowerShell module but couldn't find any. My goal is to aggregate the information available in this report to identify what are the most costly pipelines.
reference: https://techcommunity.microsoft.com/t5/azure-data-factory/new-adf-pipeline-consumption-report/ba-p/1394671
Thank you
Doing more research someone pointed me to a GitHub page where the product team posted a PowerShell script to find part of what I was looking for {1}. So I did some modifications to the script to have the output that I needed. With the output below I can extract the values from the MS calculator to get an estimated cost for each pipeline run. {2}
$startTime = "21/6/2021 7:00:00"
$endTime = "21/6/2021 10:00:00"
$adf = '<data factory name>'
$rg = '<resrouce group name>'
$outputObj = #()
$pipelineRuns = Get-AzDataFactoryV2PipelineRun -ResourceGroupName $rg -DataFactoryName $adf -LastUpdatedAfter $startTime -LastUpdatedBefore $endTime
# loop through all pipelines and child activities to return billable information
foreach ($pipelineRun in $pipelineRuns) {
$activtiyRuns = Get-AzDataFactoryV2ActivityRun -ResourceGroupName $rg -DataFactoryName $adf -pipelineRunId $pipelineRun.RunId -RunStartedAfter $startTime -RunStartedBefore $endTime
foreach ($activtiyRun in $activtiyRuns) {
if ($null -ne $activtiyRun.Output -and $null -ne $activtiyRun.Output.SelectToken("billingReference.billableDuration")) {
$obj = #()
$obj = $activtiyRun.Output.SelectToken("billingReference.billableDuration").ToString() | ConvertFrom-Json
$obj | Add-Member -MemberType NoteProperty -Name activityType -value $activtiyRun.Output.SelectToken("billingReference.activityType").ToString()
$obj | Add-Member -MemberType NoteProperty -Name pipelineName -value $pipelineRun.PipelineName
$obj | Add-Member -MemberType NoteProperty -Name activtiyRuns -value $activtiyRuns.Count
$outputObj += $obj
}
else {}
}
}
# output aggregated result set as table
$groupedObj = $outputObj | Group-Object -Property pipelineName, activityType, meterType
$groupedObj | ForEach-Object {
$value = $_.name -split ', '
New-Object psobject -Property #{
activityType = $value[1];
meterType = $value[2];
pipelineName = $value[0];
executionHours = [math]::Round(($_.Group | Measure-object -Property duration -sum).Sum, 4)
orchestrationActivityRuns = $groupedObj.group.activtiyRuns[0]
}
} | Sort-Object -Property meterType | Format-Table
Output sample:
Consumption report from the Data Factory monitor
reference:
https://github.com/Azure/Azure-DataFactory/tree/main/SamplesV2/PastRunDetails#simple-script-that-prints--activity-level-run-details-in-45-day-range {1}
https://azure.microsoft.com/en-us/pricing/calculator/?service=data-factory%2F {2}

powershellv2 - add new parameter to custom object

I have created a custom Object using the code below, but I now need to add an additional property.
Reading the online documentation and help files suggests that I need to use add-member (as per my example below).
But When I run this I get:
Add-Member : A positional parameter cannot be found that accepts argument 'newvar'.
What is the correct syntax to add a new parameter to an Object.
Also, if I wanted to update a parameter with a different value, can I use the the same add-member? (there doesn't appear to be an 'update-member' cmdlet)
$TestList = #(
"item1"
"item2"
)
$TESTObject = #()
foreach($a in $TestList)
{
$dItem = $a
$TESTObject += New-Object PSObject -property #{
item = "$dItem";
}
}
FOREACH($a in $TESTObject)
{
#DO STUFF HERE
$newVar = 1234
$a | Add-Member newvar $newVar
}
The way I have found to do this is:
Add a member to a custom (PSObject) Object
Add-Member -InputObject $TESTObject -MemberType NoteProperty -Name newVar -Value $newVar
And to update an existing member I use
Add-Member -InputObject $TESTObject -MemberType NoteProperty -Name newVar -Value $newVar -force
whether this is the best way to do this (or even the correct way) I'm not sure. But it seems to work.

Powershell Script to loop through folders checking security permissions

I have part of the code: at the moment its coming empty in the CSV file. But i need a command to specify the path/folders to look at, how do i modify this for that purpose.
Param(
[String]$path,
[String]$outfile = ".\outfile.csv"
)
$output = #()
ForEach ($item in (Get-ChildItem -Path $path -Recurse -Directory)) {
ForEach ($acl in ($item.GetAccessControl().Access)){
$output += $acl |
Add-Member `
-MemberType NoteProperty `
-Name 'Folder' `
-Value $item.FullName `
-PassThru
}
}
$output | Export-Csv -Path $outfile -NoTypeInformation
Ok, let's do this. I've made it into a function, and removed the OutFile part of it. If you want to output it to a file, pipe it to Export-CSV. If you want it saved as a variable, assign it to a variable. Just simpler this way.
Function Get-RecursiveACLs{
Param(
[String]$Path=$(Throw "You must specify a path")
)
$Output = GCI $Path -Recurse -Directory|%{
$PathName=$_.FullName
$_.GetAccessControl().Access|%{
Add-Member -InputObject $_ -NotePropertyName "Path" -NotePropertyValue $PathName -PassThru
}
}
}
Then it's a simple matter of storing it in a variable like:
$ACLList = Get-RecursiveACLs "C:\Example\Path"
Or piping it to output to a CSV if you would prefer:
Get-RecursiveACLs "C:\Example\Path" | Export-CSV "C:\Results.csv" -NoType
Put the function at the top of your script and call it as needed.

Resources