PowerShell–Find and replace files

Scenario – A file is present at many locations in folder and its subfolders. We need to find the file in all the locations of given path and replace it with the new updated file. The new updated file name may be same as old one or may be different too…

Above scenario is implemented using PowerShell script –

——————————————————————————

<#


.Synopsis
     Search and Replace File


.Discription
     It searches and then replaces file.


#>


param ([parameter(Mandatory=$True,Position=0)]
         [string][ValidateNotNullOrEmpty()]$FileLocationToSearch,


        [Parameter(Mandatory=$true, Position=1)]
         [string][ValidateNotNullOrEmpty()]$FileName,
        
         [Parameter(Mandatory=$true, Position=1)]
         [string][ValidateNotNullOrEmpty()]$FilelocationForReplace)       
      



$FilePaths = Get-ChildItem -Path $FileLocationToSearch -Recurse -File | Where-Object {$_.Name -eq $FileName} | Select -ExpandProperty FullName


foreach ($File in $FilePaths)
{
     $DestFilePath = Split-Path -Path $File
     Write-Host “—————————————————–”
     Write-Host $File  
     Remove-Item $File  
     Write-Host $File “DELETED!!!”
     Copy-Item -Path $FilelocationForReplace -Destination $DestFilePath -Force
     Write-Host $FilelocationForReplace ” — Copied Successfully to $DestFilePath”
     Write-Host “—————————————————–“
}

————————————————————————————————————–

Flow of program –

1) It is finding file from all folders and subfolders from a given location as input

2) In foreach loop, for every location found, it is deleting the found file and then copying the updated file back on that location

             —–End of Article—–

Advertisements

TFS Release Definition – Custom variables and values

Scenario – Sometimes we need to use some logic based data in a variable in Release definition task like datetime value. Example, You want to create backup folder with datetime folder to preserve the backups.

There is no direct way in TFS for achieving it. TFS does have some user defined variables that can be used for defined values but for such requirements in release, it does not have any.

To implement it, I did below steps –

  1. I defined variable in release definition and set the value to “none”. Example here is – backupfoldername and value is none.
  2. I added task for PowerShell script and added below Inline script to set the above defined variable with date time value –

$date= (Get-Date).ToString(‘MM.dd.yyyy-hhmmsstt’)

Write-Host “##vso[task.setvariable variable=backupfoldername]$date”

  1. Finally I added the release task where I wanted to use it and used it as $(variablename) like $(backupfoldername). It worked!

TFS–MAC build–Git error

I was trying to build one of my iOS based application in MAC from TFS. While trying to do so, I was getting error –

“error setting certificate verify locations:

  CAfile:/etc/ssl/certs/ca-xxxxx.crt

  CApath:/etc/ssl/certs/..”

Error screenshot is given below as well…

image

I was struggling to resolve it and I have not found much information on internet.

Finally, I am able to resolve it and wanted to share it here so that it can help all…

I looked into “system” and “global” git files and they were setup properly with all the required values of the certificates.

You can view these files using below commands –

System file – “git config –system –list

Global file – “git config –global –list

I have to give full access on the certs folder (/etc/ssl/certs) using the command –

sudo chmod –R 777 .

With it, it worked!

Important note – Many have given solution to turn off the SSLVerify which is absolutely not recommended way from security perspective.

How to publish build artifacts from iOS builds to Windows based drop location

The requirement is to publish the TFS build artifacts (output) of Xamarin.iOS or any iOS based application to the file share (drop location) based on windows environment.

As per the MS documentation, Publishing artifacts from a Linux or macOS agent to a file share (drop location) is not supported, we have to use some other way to achieve the same behavior.

Here is the option we have implemented and can be used –

1) In the build definition, add the task to publish the artifacts.  Select the Artifact Type as “Server” as shown below –

image

Note – This will save all the artifacts on the TFS database (SQL Server) and so it is recommended to have the retention policy in place to delete the unwanted artifacts from the TFS database.

2) Now, when you build the application using above publish option, once the application is build successfully, you can download all the Artifacts from the TFS web portal as shown below –

image

3) Now, if you want to have the build Artifact on some file share, you can not use the above task in iOS as said before that it is not supported for MAC agent. The workaround would be to have a release definition just to copy the build artifacts to the drop location with “Continuous Deployment” as a trigger so that as soon as build is done successfully in MAC, its build artifacts will be copied to the file share. This way you will have your build output in the file share just like the way you do for windows based agents.

image

a) Release definition creation screenshot

image

b) Release Definition with Task screenshot

TFS – Release automation – IIS based Websites

Here is the best practice for the release automation of IIS based applications –

Scenario 1 –

New Web applications –

Add below tasks in the given order –

  1. Copy task – Copy the website published code from build drop location to web server.
  2. Tokenization task – task for replacing required values in different environments.
  3. Any other requirement on the web folder like subfolder creation or folder permissions
  4. IIS Website creation task – Task to create website and its application pool. It also configures various required settings in IIS like authentication etc.
  5. Application pool start/stop task – Select the “Restart” option to restart the application pool.

Scenario 2 –

Upgrade Web applications –

Add below tasks in the given order –

  1. Application pool start/stop task – Select the “STOP” option to stop the application pool.
  2. Backup utility task – Take the backup of the existing web folder.
  3. Copy task – Copy the website updated published code from build drop location to web server.
  4. Tokenization task (If there are changes in config files)– task for replacing required values in different environments.
  5. Any other requirement on the web folder like subfolder creation or folder permissions
  6. Application pool start/stop task – Select the “START” option to start the application pool.

Please use below tasks for –

  1. IIS Website Application pool start/stop/restart (https://marketplace.visualstudio.com/items?itemName=mohdaslamansari.build-task)
  2. Folder permissions (https://marketplace.visualstudio.com/items?itemName=mohdaslamansari.FolderPermissions)
  3. Backup utility (https://marketplace.visualstudio.com/items?itemName=mohdaslamansari.backuputility)

clip_image001

 

            —–End of Article—–

Microsoft Marketplace Extension – Backup Utility

******Making Automation easy – Plug & Play Plugins********

In many releases, we get requirement to take the backup of all the files and folders of website before performing any updates on them. I have implemented this requirement using PowerShell in TFS. It needs source location from where to take the backup and the destination location where the backup will be placed.

To make it simple and easy to use, I created MS marketplace extension and published the same in the Microsoft marketplace.

It is now available publically to everyone to download and use.

Direct link – https://marketplace.visualstudio.com/items?itemName=mohdaslamansari.backuputility

Marketplace link – https://marketplace.visualstudio.com/

I have copied the sourcecode to GitHub for getting code contribution from public.

GitHub link – https://github.com/mdaslamansari/backuputility

clip_image001

clip_image001[4]

Task (UI) in release definition –

clip_image001[6]

Here is the PowerShell script at its core –

image

 

    —End of Article—-

Ansible–Windows Firewall Module

Many times, we have requirements to enable some ports (let’s say 2222) on inbound/outbound windows server firewall.

I have implemented it using “win_firewall_rule” windows module in Ansible as shown in below playbook –

 

clip_image001

On running the above playbook as shown below, below are the output from both LINUX console and the windows machine –

image

Note – refer to the article (https://mdaslam.wordpress.com/2017/08/24/tfs-ansible-integration/) for inventry file and the variables for winrm.

From LINUX –

image

image

From Windows –

clip_image001[9]

clip_image001[11]

clip_image001[13]

              ————End of Article————