Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Wednesday, December 24, 2014

List of Files Changed Between Changesets in Visual Studio Online using PowerShell

So recently I had the requirement of getting a list of files that changed between two different releases. We wanted to use this list to act as a verification to ensure that all artifacts were included in a release package.

I modified the code posted here in order to quickly write a console application to do the task. With the immediate problem solved, my colleges and I bounced the idea about porting the code into a PowerShell script which would allow us to enhance it better in the long run.

The solution would be built around the Visual Studio Online(VSO) REST service. This reduces any dependency on Team Foundation Server(TFS) specific client side assemblies or tools. The limitation is that, at the moment, it is only supported in Visual Studio Online and not all features are supported.

Pre-Requisites

Security and Credentials
In order make things simple, let's enable Alternate Authentication for access the account. This enables the script to use Basic Authentication when making request to the VSO REST service. This can be done by navigating to the profile page, selecting Credentials > Enable alternate credentials and providing new credential information. More instructions available here.

The credentials will be collected using the Get-Credentials cmdlet. This provides the standard windows credentials dialog for the user to enter information. Since this makes the script interactive, I debated about having the username and password as a parameter for the script, but in the end decided against it. Maybe the next improvement would be to include a silent version of the script.
The REST call
Invoke-RestMethod cmdlet will be used to make the actual call to the REST service. So what's the difference between Invoke-WebRequest and Invoke-RestMethod you may ask? While similar, the Invoke-RestMethod attempts to parse the returned JSON so that we do not have to do it manually within our script. Think of it as a super set of Invoke-WebRequest just like Invoke-WebRequest is a superset of System.Net.WebClient. Read more about it here and here.

I ran into strange issue when attempting to authenticate the request. The
Get-Credentials cmdlet would return a System.Management.Automation.PSCredential object as expected, but when passed into into the Invoke-RestMethod cmdlet, it was not generating the the basic authentication header token within the request. I still haven't figured out why this happens, but the workaround was to add the authentication header explicitly as shown here.

$basicAuth = ("{0}:{1}" -f $username,$password)
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization=("Basic {0}" -f $basicAuth)}

Making the call to the service

  1. First get a list of changesets related to the project within the timeframe that we're interested in.
    https://{account}.visualstudio.com/defaultcollection/_apis/tfvc/changesets?api-version=1.0&searchCriteria.fromId=100&searchCriteria.toId=200&searchCriteria.itemPath=$/{project}
    
    It took me a while to figure it out but you should notice this call is only allowed to be made against the entire Team Project Collection. So in order to filter out the project, provide the project path via the searchCriteria.itemPath filter. That is searchCriteria.itemPath=$/{projectname} where {projectname} is the one that you are interested in.
  2. Next iterate through each of the results to retrieve the detailed information on of each of the changesets. This result would include a collection of all the files that were affected.
    https://{account}.visualstudio.com/defaultcollection/_apis/tfvc/changesets/{changesetId}/changes?api-version=1.0
  3. Again, iterate through each of the changes and extract the path property of the json result set. This is the path and name of the file.
  4. Remove duplicates entries and folder creation entries as necessary.

Final Thoughts

You can download my implementation here.

The next steps would make this to its own cmdlet in order to make it more reusable in other scripts. Also check out the Curah! page that I created while working on this.

References

Friday, July 03, 2009

Adding outbound Windows Firewall rules using Powershell

This is something I wrote a while back, thought I’d post it here. It does exactly what the title says. Its basically a port of the following:
http://msdn.microsoft.com/en-us/library/aa364695(VS.85).aspx

Saturday, June 06, 2009

Creating “App Paths” using Powershell

I’m a big fan of Slickrun and its a tool I recommend everyone should have in their toolbox.

Unfortunately, working onsite on the client’s computers, installing third-party software is usually frowned upon. So I’ve got to make due by using the app paths.

The App Path technique of launching applications have have been around for a while. Read More:

http://www.tweakxp.com/article36950.aspx

The purpose of the script is to make the whole registry editing process a bit easier.

You can download the script here:

And execute it as such:

appPath.ps1 -key "notepad2.exe" "c:\windows\notepad2.exe"
or
appPath.ps1 -key "notepad2.exe" -path "c:\windows\notepad2.exe"

Where –key is the entry you would type in the Run dialog (please note that the extension is required).  And –Path being the actual path of the file.

Disclaimer: This script modifies the registry. Use at your own risk, I will not responsible for anything that breaks as a result.

Friday, December 12, 2008

Download YouTube videos using Powershell

Now that I'm back home in Sri Lanka, I'm getting re-accustomed to the slow Internet speeds over here. Slow internet speeds was the reason for me to write the YouTube downloader back in April.

So I spent a lazy afternoon today trying to port that code into Powershell. And here's the result:

How it works

The script first figures out the direct download link and then hands it off to a download manager which performs the actual download. You should be able to plug-in any download manager that supports command line arguments but I prefer to use wget for its simplicity.

Information on how the direct link is extracted can be found in my original post.

Use $downloaderPath and $downloaderArguments to configure the path of wget and its arguments respectively. The script also supports traversal through proxies and can be configured using the $proxy and $proxyPort variables.

The script accepts one parameter; the watch url of the video.

.\psVDownloader.ps1 http://www.youtube.com/watch?v=N_c60Sp7Gtc

.

Please be aware that this script is purely for research purposes only. And I believe that you will be violating the YouTube TOS by trying to use its streams in ways other than intended.

Tuesday, August 05, 2008

Using Powershell to Query and Integrate MP3 Album Art

I've been trying to update my mp3 collection with album art since I got the P2. There are many tools out there that allow you to achieve this, but none that provide an easy and flexible way to automate the whole tagging process. That's why I wrote a quick and dirty Powershell script to do the job for me. Plus, it gives me a chance to play around with Powershell.

How it works:
The script can be executed in the following manner:
.\Mp3AlbumArt.ps1 .\mp3\*def*.mp3
or
.\Mp3AlbumArt.ps1 .\mp3\*\*.mp3
(recurse through sub folders)

The only parameter takes in the paths of the files that need to be processed. The script uses Greg Keogh's excellent NTag library for integrating the album art into the ID3 Tag. The album art itself is retrieved by querying the Amazon Web Service using the Artist and Album Tags. Obviously it doesn't work very well if the Meta tags are not set up properly.

Some assembly required:
You would need to register for an Amazon Web Service Account to get an key to query the service. Replace your key in the commented area to get stared.

Future:
I was in a rush to get something working so didn't spend too much time error handling or on any best practices. As I gain more experience with powershell, I will eventually re-write this script to take advantage of powershell functionality. But for now it serves is purpose.

References:

Download:

Comments and constructive criticism appreciated.