Found 989 Articles for Software & Coding

How to apply a tag to the azure resource group using PowerShell?

Chirag Nagrekar
Updated on 06-Apr-2021 07:54:56

597 Views

Like Azure VM, we can apply the azure tags to the resource group or any other resources. The tagging works on the resource ID and in azure, all the resources come with the resource ID property.To apply the tag to the azure resource group, we first need to use get the resource group details to use its resource ID. The below code shows that how we can apply the new tag to the azure resource group.ExamplePS C:\> $rg = Get-AzResourceGroup -Name AnsibleTestRG PS C:\> $tag = @{Owner='Chirag'; CostCenter='USFinance'} PS C:\> New-AZTag -ResourceId $rg.ResourceId -Tag $tag -VerboseOutput

What is the difference between the New-AZTag and Update-AZTag command in Azure PowerShell?

Chirag Nagrekar
Updated on 06-Apr-2021 07:53:31

282 Views

New-AZTagWhen we use the New-AZTag command, it removes all the tags from the particular resource and adds a new tag. This command is generally helpful when we are creating a new resource and we want to apply the tag for it.Update-AZTagWhen we use the Update-AZTag command, it updates, deletes, and replaces the Azure tags. We need to make sure that when we are applying the tag to any existing resource which is already tagged, the Update-AZTag command should be used otherwise New-AZTag command will remove all the previous applied tags and it will add new tags.

How to delete the specific tag of Azure VM using PowerShell?

Chirag Nagrekar
Updated on 06-Apr-2021 07:51:36

1K+ Views

To delete the specific tag of the Azure VM, we can use the Delete property of the Operation parameter in the Update-AZTag command. In the below example, we have the below tags of the Azure VM TestMachine2k12.Key             Value ---             ----- Patching_Day    Sunday Owner           ChiragWe need to delete the Patching_Day tag and for that, we also need its value. The below command will delete the specified tag from the Azure VM.PS C:\> $vm = Get-AzVM -VMName TestMachine2k12 PS C:\> $tag = @{'Patching_Day'='Sunday'} PS ... Read More

How to add the tag of Azure VM using PowerShell?

Chirag Nagrekar
Updated on 06-Apr-2021 07:49:46

2K+ Views

To add the tag to the Azure VM we need to use the Update-AZTag command. This command will merge the new tag to the existing tag(s) of the VM. If you are planning to add the entirely new VM tag, you can use the New-AZTag command. Once you use the New-AZTag command, other tags will be deleted for that particular VM and the New tag will be created so pls be careful with that command.We have the VM called TestMachine2k12 on Azure and there are few existing tags applied to the VM as shown below.ExampleGet-AzVM -Name TestMachine2k12 | Select -ExpandProperty ... Read More

How to import the tags in Azure?

Chirag Nagrekar
Updated on 06-Apr-2021 07:47:42

461 Views

In the previous article, we have seen that we can export the Azure resource tags in the JSON file or CSV format. There are some cases when you rebuild your resource and you might need to restore your tags or someone with the authorized person to have azure resource access and he accidentally deletes the tags and we need to restore them. In such cases, if we have the tags backup already we can import them to the resources.In the below example, we suppose we have the Azure VM tags backup stored in the CSV file format and after rebuilding ... Read More

How to Export the azure VM tags using PowerShell?

Chirag Nagrekar
Updated on 06-Apr-2021 07:45:32

2K+ Views

There are two ways to get the applied azure VM tags using PowerShell.Using Tags Property of the Azure VMUsing the Get-AZTag command.ExamplePS C:\> Get-AzVM -VMName Testmachine2k12 | Select -ExpandProperty Tags Key          Value ---          ----- Patching_Day Sunday Owner       ChiragAnother way is by using the Get-AZTag command.PS C:\> $vm = Get-AzVM -VMName TestMachine2k12 PS C:\> Get-AzTag -ResourceId $vm.Id | Select -ExpandProperty PropertiesOutputTagsProperty ------------ {[Owner, Chirag], [Patching_Day, Sunday]}We need to export this tag and the best way to store the tags is using the JSON file.Get-AzVM -VMName Testmachine2k12 | Select -ExpandProperty Tags ... Read More

How to add the new tag to Azure VM using PowerShell?

Chirag Nagrekar
Updated on 06-Apr-2021 07:43:33

500 Views

To add the new tag of Azure VM using PowerShell, we need to use the New-AZTag command. Please note: If you have already tags applied to the VM, you need to use the Update-AZTag command to merge with the available Azure Tags otherwise all the previous applied.For example, We have the below VM called TestMachine2k12 and after signing to the Azure account we need VMs resource ID to apply the tag to the VM.We will use a tag in the HastTable format so we will have its Key and a Value. We need to apply the below new tag.Example$tag = ... Read More

How to get the folder size using PowerShell?

Chirag Nagrekar
Updated on 30-Mar-2021 13:58:01

4K+ Views

We will first retrieve the content of the folder using the Get-ChildItem command and then pipeline the Measure-Object command as shown below.Get-ChildItem C:\Temp\ -Recurse | Measure-Object -Property Length -SumOutputCount : 1514 Average : Sum : 372060503 Maximum : Minimum : Property : LengthSo the above output shows that there is a total of 1514 files and folders and the sum shows the size of all the files and folders combined in KB. We can convert it to the MB as shown below.(Get-ChildItem C:\Temp\ -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB 354.824545860291We can get the round figure, [Math]::Round( ... Read More

How to run a PowerShell script from the command prompt?

Chirag Nagrekar
Updated on 30-Mar-2021 14:05:03

22K+ Views

To run the PowerShell script from the command prompt, we can use the below command.ExampleFor example, we have a script TestPS.ps1 which first starts the spooler service and then copies a file to a different location. We need to call this script using the command prompt.C:\> PowerShell.exe -command "C:\temp\TestPS.ps1"The above command is similar to running individual PowerShell commands. Here we are providing the path of the script.OutputC:\>PowerShell.exe -command "C:\temp\TestPS.ps1" VERBOSE: Performing the operation "Start-Service" on target "Print Spooler (Spooler)". Status Name DisplayName ------ ----- ---------- Running Spooler Print Spooler VERBOSE: Performing the operation "Copy File" on target "Item: C:\Temp\EnvVariable.txt Destination: ... Read More

How to run PowerShell commands from the command prompt?

Chirag Nagrekar
Updated on 30-Mar-2021 13:24:24

9K+ Views

To run Powershell commands from the command prompt or cmd, we need to call the PowerShell process PowerShell.exe.ExampleSee the sample example, C:\> Powershell.exe -Command "Write-Output 'Hello world'" Hello worldSimilarly, you can call any command. We will use another example to get service informationC:\> Powershell.exe -Command "Get-Service Spooler" Status   Name    DisplayName ------   ----    ----------- Running  Spooler Print SpoolerTo run multiple commands, C:\> Powershell.exe -Command "Stop-Service Spooler -verbose -passthru; Start-Service Spooler -verbose -passthru"OutputVERBOSE: Performing the operation "Stop-Service" on target "Print Spooler (Spooler)". Status Name DisplayName ------ ---- ----------- Stopped Spooler Print Spooler VERBOSE: Performing the operation "Start-Service" on ... Read More

Advertisements