Skip to content

Instantly share code, notes, and snippets.

@grenade
Last active April 13, 2016 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grenade/17751e9ce0e3e6656d19 to your computer and use it in GitHub Desktop.
Save grenade/17751e9ce0e3e6656d19 to your computer and use it in GitHub Desktop.
function Write-Log {
<#
.Synopsis
Logs to the userdata run log file, with timestamps.
.Parameter message
The body of the log message
.Parameter severity
The severity of the message, to enable filtering in log aggregators or reporting.
.Parameter path
The full path to the log file.
#>
param (
[string] $message,
[string] $severity = 'INFO',
[string] $path = ('{0}\log\userdata-run.log' -f $env:SystemDrive)
)
if (!(Test-Path $path)) {
[Environment]::SetEnvironmentVariable('OutputToConsole', 'true', 'Process')
}
$formattedMessage = ('{0} [{1}] {2}' -f [DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss zzz"), $severity, $message)
Add-Content -Path $path -Value $formattedMessage
switch ($severity)
{
#{Error | Warning | Information | SuccessAudit | FailureAudit}
'DEBUG' {
$foregroundColor = 'DarkGray'
$entryType = 'SuccessAudit'
$eventId = 2
break
}
'WARN' {
$foregroundColor = 'Yellow'
$entryType = 'Warning'
$eventId = 3
break
}
'ERROR' {
$foregroundColor = 'Red'
$entryType = 'Error'
$eventId = 4
break
}
default {
$foregroundColor = 'White'
$entryType = 'Information'
$eventId = 1
break
}
}
if ($env:OutputToConsole -eq 'true') {
Write-Host -Object $formattedMessage -ForegroundColor $foregroundColor
}
$logName = 'Application'
$source = 'Userdata'
if (!([Diagnostics.EventLog]::Exists($logName)) -or !([Diagnostics.EventLog]::SourceExists($source))) {
New-EventLog -LogName $logName -Source $source
}
Write-EventLog -LogName $logName -Source $source -EntryType $entryType -Category 0 -EventID $eventId -Message $message
}
function Prep-Spot {
param (
[switch] $force
)
begin {
Write-Log -message ("{0} :: Function started" -f $($MyInvocation.MyCommand.Name)) -severity 'DEBUG'
}
process {
if (!$force -and (Get-EventLog -logName 'Application' -source 'Userdata' -message 'Prep-Spot :: Function ended' -newest 1 -ErrorAction SilentlyContinue)) {
Write-Log -message ("{0} :: detected prior run. skipping spot setup" -f $($MyInvocation.MyCommand.Name)) -severity 'DEBUG'
} else {
$mountPath = ('{0}\mnt' -f $env:SystemDrive) # if this folder exists and is empty and a Z: drive exists, mount the Z: drive to the folder.
if ((Test-Path -Path $mountPath -PathType Container -ErrorAction SilentlyContinue) -and ((Get-ChildItem $mountPath | Measure-Object).Count -eq 0)) {
Mount-EphemeralDisks -path $mountPath
}
}
}
end {
Write-Log -message ("{0} :: Function ended" -f $($MyInvocation.MyCommand.Name)) -severity 'DEBUG'
}
}
# this function will not work on instances that have more than one, non-ephemeral disk (eg: c:, d: where both are gp2 or iops).
# it will likely do nasty things if that is the case.
function Mount-EphemeralDisks {
param (
[string] $path = ('{0}\mnt' -f $env.SystemDrive)
)
begin {
Write-Log -message ("{0} :: Function started" -f $($MyInvocation.MyCommand.Name)) -severity 'DEBUG'
}
process {
$outfile = ('{0}\log\{1}.diskpart.stdout.log' -f $env:SystemDrive, [DateTime]::Now.ToString("yyyyMMddHHmmss"))
$errfile = ('{0}\log\{1}.diskpart.stderr.log' -f $env:SystemDrive, [DateTime]::Now.ToString("yyyyMMddHHmmss"))
$ephemeralVolumeCount = @(Get-WmiObject Win32_Volume | ? { ($_.DriveLetter -and !($_.SystemVolume) -and (-not ($_.DriveLetter -ieq $env:SystemDrive))) }).length
$volumeOffset = ($ephemeralVolumeCount - (@(Get-WmiObject Win32_Volume).length)
$diskpartscript = @(
'',
"select volume 1`nremove all dismount`nselect disk 1`nclean`nconvert gpt`ncreate partition primary`nformat quick fs=ntfs`nselect volume 1`nassign mount=C:\mnt",
"select disk 1`nclean`nconvert dynamic`nselect disk 2`n clean`nconvert dynamic`ncreate volume stripe disk=1,2`nselect volume $volumeOffset`nformat quick fs=ntfs`nassign mount=C:\mnt"
)
if (($ephemeralVolumeCount -gt 0) -and ($volumeOffset -gt 0) -and ($ephemeralVolumeCount -lt $diskpartscript.length)) {
New-Item -path ('{0}\mnt.dp' -f $env:Temp) -value $diskpartscript[$ephemeralVolumeCount] -itemType file -force
Start-Process 'diskpart' -ArgumentList @('/s', ('{0}\mnt.dp' -f $env:Temp)) -Wait -NoNewWindow -PassThru -RedirectStandardOutput $outfile -RedirectStandardError $errfile
if (-not ((Get-Item $errfile).length -gt 0kb)) {
Write-Log -message ('{0} :: ephemeral volume(s) mounted at {1}. {2}' -f $($MyInvocation.MyCommand.Name), $path, (Get-Content $outfile)) -severity 'INFO'
} else {
Write-Log -message ("{0} :: failed to mount ephemeral volume(s) at {1}. {2}" -f $($MyInvocation.MyCommand.Name), $path, (Get-Content $errfile)) -severity 'ERROR'
}
} else {
Write-Log -message ('{0} :: mount skipped. volume count was: {1}' -f $($MyInvocation.MyCommand.Name), $ephemeralVolumeCount) -severity 'DEBUG'
}
}
end {
Write-Log -message ("{0} :: Function ended" -f $($MyInvocation.MyCommand.Name)) -severity 'DEBUG'
}
}
Prep-Spot #-force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment