If you are creating PowerShell scripts, tools or modules today, you are most likely using Git. What? You're not? Is it because you haven't gotten around to installing it? I have some "quick and dirty" PowerShell hacks to help you out on Windows systems. Linux boys and girls already know what to do.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Naturally you could run a search to find the download link for the latest version of Git. Or if you are using Chocolatey you most likely know how to find and install packages. But perhaps you don't (or can't) use Chocolatey. That's ok. PowerShell can make quick work of this task.
#download the latest 64bit version of Git for Windows $uri = 'https://git-scm.com/download/win' #path to store the downloaded file $path = $env:temp #get the web page $page = Invoke-WebRequest -Uri $uri -UseBasicParsing #get the download link $dl = ($page.links | where outerhtml -match 'git-.*-64-bit.exe' | select -first 1 * ).href #split out the filename $filename = split-path $dl -leaf #construct a filepath for the download $out = Join-Path -Path $path -ChildPath $filename #download the file Invoke-WebRequest -uri $dl -OutFile $out -UseBasicParsing #check it out Get-item $out
My code assumes you are running on a 64bit system. The comments should explain how it all works. When run you'll get the latest setup executable. I recommend manually installing it so you can configure it as needed.
Related to this you might need a merge conflict tool. I use kdiff3. Here's similar code to download it.
$k = invoke-webrequest https://sourceforge.net/projects/kdiff3/files/kdiff3/0.9.98/KDiff3-64bit-Setup_0.9.98-2.exe/download -UseBasicParsing $dl = $k.links | where href -match downloads $dluri = $dl.href.split("?")[0] $filename = Split-Path $dluri -Leaf $out = Join-Path $path -ChildPath $filename Invoke-WebRequest -uri $dluri -OutFile $out -UseBasicParsing get-item $out
Again, go ahead and install it manually.
My last git hack is to use PowerShell to get the version. If you have git installed, you can easily run
git --version
In Windows you'll get something like git version 2.14.1.windows.1. But being the PowerShell geek that I am, I wanted to display just the version number. So I used a regular expression to extract the value.
$v = git --version [regex]$rx ="(\d+\.){1,}\d+" $rx.match($v).value
This same code will also work in open source PowerShell on Linux. Although Windows users can also check the registry:
Get-itemproperty HKLM:\SOFTWARE\GitForWindows | Select CurrentVersion,InstallPath
Want to check the latest git for Windows release to know if you need to download? Use this function.
Function Get-GitCurrentRelease { [cmdletbinding()] Param( [ValidateNotNullorEmpty()] [string]$Uri = "https://api.github.com/repos/git-for-windows/git/releases/latest" ) Begin { Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)" } #begin Process { Write-Verbose "[PROCESS] Getting current release information from $uri" $data = Invoke-Restmethod -uri $uri -Method Get if ($data.tag_name) { [pscustomobject]@{ Name = $data.name Version = $data.tag_name Released = $($data.published_at -as [datetime]) } } } #process End { Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)" } #end }
Fun, right? Now there's no excuse for setting up git on your Windows desktop.
The frustrating part is there are those of us that work at financial institutions and you don’t have the access to do anything like instal apps. Not to mention a lot of sites, and URL’s are blocked. heck I have been an some where WSMan is blocked so I had to do all kinds of workarounds.
If there were flat file alternatives rather than an app you have to install i may be able to get it and run it. Even then sites you connect to to get scripts, etc, often are blocked. It’s like having your hands and feet bound and then thrown into a pool and told to swim.
I feel your pain. I understand the business case for security and all that it might require, but at some point management has to weigh the cost of security with the cost of getting your job done.
Wow I had no idea such a thing as Chocolatey existed in a Windows environment. Thanks Jeff!