VisualStudi2015.psm1
function RetrievePackages { param($path, $registry) $packages = @() $key = $registry.OpenSubKey($path) $subKeys = $key.GetSubKeyNames() |% { $subKeyPath = $path + "\\" + $_ $packageKey = $registry.OpenSubKey($subKeyPath) $package = New-Object PSObject $package | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($packageKey.GetValue("DisplayName")) $package | Add-Member -MemberType NoteProperty -Name "UninstallString" -Value $($packageKey.GetValue("UninstallString")) $packages += $package } return $packages } function Get-InstalledSoftwares { $installedSoftwares = @{} $path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" $registry32 = [microsoft.win32.registrykey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Registry32) $registry64 = [microsoft.win32.registrykey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, [Microsoft.Win32.RegistryView]::Registry64) $packages = RetrievePackages $path $registry32 $packages += RetrievePackages $path $registry64 $packages.Where({$_.DisplayName}) |% { if(-not($installedSoftwares.ContainsKey($_.DisplayName))) { $installedSoftwares.Add($_.DisplayName, $_) } } $installedSoftwares.Values } $ErrorActionPreference = "Stop" function Test-CurrentUserAdmin { [CmdletBinding()] param() $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = $identity -as [Security.Principal.WindowsPrincipal] if($principal -eq $null) { throw "Failed to intiailize the windows principal instance from windows identity of the current user" } if(-not($principal.IsInRole("Administrators"))) { throw "The current user is not added to the Adminstrators group. Start the installation using an Admin account" } else { "The current user is an administrator. Installation will start now" | Write-Verbose } } function Get-VisualStudio2015 { [CmdletBinding()] param ( [Parameter(Mandatory=$false, Position=0)] [Path] $Path = $Env:Temp ) $uri = "http://download.microsoft.com/download/0/B/C/0BC321A4-013F-479C-84E6-4A2F90B11269/vs_community.exe" if(-not(Test-VisualStudio2015)) { if(-not(Test-CurrentUserAdmin)) { throw "An administrator session is required for installation" } "Downloading VS2015 to $Path" | Write-Verbose $fileName = $uri.SubString($uri.LastIndexOf("/") + 1) Invoke-WebRequest -Uri $uri -OutFile (Join-Path $Path $fileName) "Installing VS2015" | Write-Verbose $adminDeployment = (Join-Path $PSScriptRoot 'AdminDeployment.xml') $args = "/Quiet /NoRestart /Log $Env:Temp\VisualStudio2015_Install.log" if(-not (Test-Path $adminDeployment)) { "An admin file not found. Skipping the admin deployment file" | Write-Warning } else { $args += " /AdminFile $adminDeployment " } Start-Process -FilePath (Join-Path $Path $fileName) -ArgumentList $args -Wait -NoNewWindow } else { "Visual Studio already installed on your machine. Skipping the installation" | Write-Warning } } function Test-VisualStudio2015 { $package = Get-InstalledSoftwares |? {$_.DisplayName.Contains('Microsoft Visual Studio Community 2015')} return $package -ne $null } function Remove-VisualStudio2015 { [CmdletBinding()] param ( [Parameter(Mandatory=$false, Position=0)] [Path] $Path = $Env:Temp ) $uri = "http://download.microsoft.com/download/0/B/C/0BC321A4-013F-479C-84E6-4A2F90B11269/vs_community.exe" if((Test-VisualStudio2015)) { if(-not(Test-CurrentUserAdmin)) { throw "An administrator session is required for uninstallation" } "Downloading VS2015 to $Path" | Write-Verbose $fileName = $uri.SubString($uri.LastIndexOf("/") + 1) Invoke-WebRequest -Uri $uri -OutFile (Join-Path $Path $fileName) "Removing VS2015" | Write-Verbose $args = "/Quiet /Force /Uninstall /Log $Env:Temp\VisualStudio2015_Uninstall.log" Start-Process -FilePath (Join-Path $Path $fileName) -ArgumentList $args -Wait -NoNewWindow } else { "Visual Studio is not installed on your machine. Skipping the uninstallation" | Write-Warning } } Export-ModuleMember *VisualStudio2015 |