Skip to content

Instantly share code, notes, and snippets.

@petemounce
Created October 4, 2012 10:04
Show Gist options
  • Save petemounce/3832687 to your computer and use it in GitHub Desktop.
Save petemounce/3832687 to your computer and use it in GitHub Desktop.
Install ruby on Windows 2008r2 sp1 x64 Server Core
## Get-WebFile (aka wget for PowerShell)
##############################################################################################################
## Downloads a file or page from the web
## History:
## v3.6 - Add -Passthru switch to output TEXT files
## v3.5 - Add -Quiet switch to turn off the progress reports ...
## v3.4 - Add progress report for files which don't report size
## v3.3 - Add progress report for files which report their size
## v3.2 - Use the pure Stream object because StreamWriter is based on TextWriter:
## it was messing up binary files, and making mistakes with extended characters in text
## v3.1 - Unwrap the filename when it has quotes around it
## v3 - rewritten completely using HttpWebRequest + HttpWebResponse to figure out the file name, if possible
## v2 - adds a ton of parsing to make the output pretty
## added measuring the scripts involved in the command, (uses Tokenizer)
##############################################################################################################
function Get-WebFile {
param(
$url = (Read-Host "The URL to download"),
$fileName = $null,
[switch]$Passthru,
[switch]$quiet
)
$req = [System.Net.HttpWebRequest]::Create($url);
$res = $req.GetResponse();
if($fileName -and !(Split-Path $fileName)) {
$fileName = Join-Path (Get-Location -PSProvider "FileSystem") $fileName
}
elseif((!$Passthru -and ($fileName -eq $null)) -or (($fileName -ne $null) -and (Test-Path -PathType "Container" $fileName)))
{
[string]$fileName = ([regex]'(?i)filename=(.*)$').Match( $res.Headers["Content-Disposition"] ).Groups[1].Value
$fileName = $fileName.trim("\/""'")
if(!$fileName) {
$fileName = $res.ResponseUri.Segments[-1]
$fileName = $fileName.trim("\/")
if(!$fileName) {
$fileName = Read-Host "Please provide a file name"
}
$fileName = $fileName.trim("\/")
if(!([IO.FileInfo]$fileName).Extension) {
$fileName = $fileName + "." + $res.ContentType.Split(";")[0].Split("/")[1]
}
}
$fileName = Join-Path (Get-Location -PSProvider "FileSystem") $fileName
}
if($Passthru) {
$encoding = [System.Text.Encoding]::GetEncoding( $res.CharacterSet )
[string]$output = ""
}
if($res.StatusCode -eq 200) {
[int]$goal = $res.ContentLength
$reader = $res.GetResponseStream()
if($fileName) {
$writer = new-object System.IO.FileStream $fileName, "Create"
}
[byte[]]$buffer = new-object byte[] 4096
[int]$total = [int]$count = 0
do
{
$count = $reader.Read($buffer, 0, $buffer.Length);
if($fileName) {
$writer.Write($buffer, 0, $count);
}
if($Passthru){
$output += $encoding.GetString($buffer,0,$count)
} elseif(!$quiet) {
$total += $count
if($goal -gt 0) {
Write-Progress "Downloading $url" "Saving $total of $goal" -id 0 -percentComplete (($total/$goal)*100)
} else {
Write-Progress "Downloading $url" "Saving $total bytes..." -id 0
}
}
} while ($count -gt 0)
$reader.Close()
if($fileName) {
$writer.Flush()
$writer.Close()
}
if($Passthru){
$output
}
}
$res.Close();
if($fileName) {
ls $fileName
}
}
# from the initial cmd prompt that server core obnoxiously gives you on RDP:
Dism /online /enable-feature /featurename:ServerCore-WOW64
start /i powershell
# in the new powershell:
mkdir $PSHome\Modules\Get-WebFile
notepad $PSHome\Modules\Get-WebFile\Get-WebFile.psm1
write-host "hit a key once you have pasted the content into the Get-WebFile.psm1 file in the system modules directory"
pause
set-executionpolicy remotesigned
Add-Content $PSHome\profile.ps1 'Import-Module Get-Webfile'
# to get a new powershell with the new profile
start /i powershell
get-webfile http://rubyforge.org/frs/download.php/76054/rubyinstaller-1.9.3-p194.exe
.\rubyinstaller-1.9.3-p194.exe
# click through the prompts - add ruby to path. I used c:\ruby193 as install path, the default, and I bound rb and rbw to ruby
# If you added ruby to PATH, start a new session by logging off and back on again, or some cleverer way of starting a new session that means the environment variables are re-read (please let me know if you're less brutal than me! :-) )
ruby -v
# -> ruby 1.9.3p194 (2012-04-20) [i386-mingw32]
get-webfile https://github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe
.\DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe
# extracted to c:\devkit
cd c:\devkit
ruby .\dk.rb init
ruby .\dk.rb review
# ->
# Based upon the settings in the 'config.yml' file generated
# from running 'ruby dk.rb init' and any of your customizations,
# DevKit functionality will be injected into the following Rubies
# when you run 'ruby dk.rb install'.
ruby .\dk.rb install
# ->
# [INFO] Updating convenience notice gem override for 'C:/Ruby193'
# [INFO] Installing 'C:/Ruby193/lib/ruby/site_ruby/devkit.rb'
# test it:
gem install rdiscount --platform=ruby
# -> should see "Temporarily enhancing PATH to include DevKit..."
ruby -rubygems -e "require 'rdiscount'; puts RDiscount.new('**Hello RubyInstaller**').to_html"
# -> should see "<p><strong>Hello RubyInstaller</strong></p>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment