Search This Blog

Monday, 16 September 2019

Quick reference to install Powershell on Linux

Quick reference to install Powershell on Linux (i.e. core)

 curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -  
   
 sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/18.04/prod.list  
   
 sudo apt-get update  
   
 sudo apt-get install -y powershell  
   
 pwsh  


Quick reference to install Azure CLI

Quick reference to install Azure CLI

Linux


 AZ_REPO=$(lsb_release -cs)  
 echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | \  
 sudo tee /etc/apt/sources.list.d/azure-cli.list  
   
 curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -  
   
 sudo apt-get install apt-transport-https  
 sudo apt-get update && sudo apt-get install azure-cli  
   
 az --version  


Windows


https://aka.ms/installazurecliwindows

 az --version  





Tuesday, 10 September 2019

Azure Cloudshell powershell for Azure Resource tagging

Force tag onto all resources in an Azure ResourceGroup.  Useful for cost tracking.

CAUTION - overwrites existing tags on resource.

foreach ($r in (Get-AzResource -ResourceGroupName YOUR_RG_NAME)){ Set-AzResource -Tag @{myTag="MyValue"} -ResourceId $r.ResourceId -Force }




UPDATE : remove tag while leaving other intact

Get-AzResource -TagName "MyTagKey" | % {
   $_.Tags.Remove("MyTagKey")
   $_.Tags
   Set-AzResource -ResourceId $_.ResourceId -Tag $_.Tags -Force
}

Maintain manually added IP rules when website is re-created

Maintain manually added IP rules when website is re-created


   
   
 #cache IP rules into variable so they can be re-added to the replacement site later on  
 if(Get-ChildItem -Path IIS:\Sites | Where-Object {$_.Name -eq $WebsiteName}){  
   $ipRules = Get-WebConfiguration system.webServer/security/ipSecurity/* -PSPath "IIS:/sites/$WebsiteName"  
 }  
   
   
   
 #re-create website. -Force causes the existing site to be overwritten and all IP rules lost, including any manually configured ones  
 New-Website -Name $WebsiteName -ApplicationPool $WebsiteName -PhysicalPath "c:\whatever" -HostHeader "MyWebsite" -Force  
   
 # ... configure website and transform web.config with any default IP rules  
   
   
   
 #get any default IP rules that have been added to the site via web.config transforms  
 $defaultIpRules = Get-WebConfiguration system.webServer/security/ipSecurity/* -PSPath "IIS:/sites/$WebsiteName"  
   
 #put any custom IP rules back into "new" site. Comparison avoids duplication between cached rules and default rules  
 if($ipRules) {  
   $rules = compare-object $iprules $defaultIpRules -PassThru -Property "ipAddress" | where {$_.sideindicator -eq "<="}  
   Add-WebConfiguration system.webServer/security/ipSecurity -PSPath "IIS:/sites/$WebsiteName" -Value $rules  
 }  

Invoke-WebRequest auth headers

Add auth headers when downloading files.  Useful if pulling files directly from an Azure Devops git repo.


 $user = "UsernameLoadedFromSecureStore"  
 $pass = "PasswordLoadedFromSecureStore"  
   
 $pair = "$($user):$($pass)"  
 $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))  
 $basicAuthValue = "Basic $encodedCreds"  
 $Headers = @{Authorization = $basicAuthValue}  
   
 Invoke-WebRequest -Uri $uri -OutFile $filename -Headers $Headers  

Powershell rough and ready variable substitution

A very rough and ready way to transform a file based on an array of keys/values



  $example = @"  
   
  <servers>  
   <server name="AAA" value="{{valA}}" />  
   <server name="BBB" value="{{valB}}" />  
   <server name="CCC" value="{{valC}}" />  
 </servers>  
   
 "@  
   
   
 $configVals= @{ "valA"='blah1'; "valB"="whatever2";}  
   
   
 $configVals.GetEnumerator() | % {  
   $key = "{{"+$_.key+"}}"  
   $example = $example.Replace($key,$_.value)  
 }  
   
 Write-Host $example  
   

Force Powershell to use TLS1.2 (defaults to 1.0)

Force Powershell to use TLS1.2 (defaults to 1.0)

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12