Skip to content

Instantly share code, notes, and snippets.

@fjudith
Last active June 8, 2017 22:23
Show Gist options
  • Save fjudith/7149c07033defcdfabc3608652d60207 to your computer and use it in GitHub Desktop.
Save fjudith/7149c07033defcdfabc3608652d60207 to your computer and use it in GitHub Desktop.
Powershell workflow script used to format and/or mount attached disk to a Coreos hosts in parallel.
Workflow Format-CoreOSDisk{
PARAM(
[parameter(mandatory=$true,position=0)]
[string[]]
$ComputerName,
[parameter(mandatory=$false,position=1)]
[hashtable]
$HardDrive, #@{'/dev/sdb'='xfs@/mnt/data1';'/dev/sdc'='btrfs@/mnt/data2'}
[parameter(mandatory=$false,position=2)]
[string]
$User,
[parameter(mandatory=$false)]
[string]
$Password,
[parameter(mandatory=$false)]
[PSCredential]
$Credential,
[parameter(mandatory=$false)]
[string]
$Keyfile = "$env:userprofile\.ssh\id_rsa.key"
)
Set-StrictMode -Version 3
# https://github.com/darkoperator/Posh-SSH
Import-Module -Name Posh-SSH
Foreach -parallel ($Computer in $Computername)
{
If($Password)
{
$SecureHostPassword = ConvertTo-SecureString "${Password}" -AsPlainText -Force
$SSHCredential = New-Object System.Management.Automation.PSCredential ("${User}", $SecureHostPassword)
$SSHSessionID = New-SSHSession -Force -Computername "${Computer}" -Credential $Credential
}
ElseIF($User)
{
If(-not $(Test-Path -Path $KeyFile)){Throw "${Computer}: SSH key file `"${$KeyFile}`" not found"}
$SessionID = New-SSHSession -Force -Computername "${Computer}" -KeyFile $KeyFile -Credential "${User}"
}
Else
{
$SessionID = New-SSHSession -Force -Computername "${Computer}" -Credential $Credential
}
Foreach($Device in $Hardrive.Keys)
{
$Format = $($HardDrive[$Device]).Split('@')[0]
$Mount = $($HardDrive[$Device]).Split('@')[1]
Invoke-SSHCommand -Session $SessionID -Command "sudo parted -s $($Device) mklabel gpt mkpart primary ${Format} 0% 85%"
Invoke-SSHCommand -Session $SessionID -Command "sudo mkfs.${Format} $($Device) -f"
Invoke-SSHCommand -Session $SessionID -Command "sudo blkid $($Device)"
If($Mount)
{
Invoke-SSHCommand -SessionID $SessionID -Command "sudo mount -t ${Format} $($Device) ${Mount}"
}
}
Remove-SSHSession -SessionID $SessionID
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment