Volumes.psm1
#region Functions #region Get-DockerVolume function Get-DockerVolume { [CmdletBinding()] param ( [Parameter()] [String] $Name ) Begin { # Check if docker is installed and running try { Check-Docker } catch { throw "Could not find docker." } } Process { #region Get the volumes # Form the arguments $arguments = @("volume", "ls", "--format", '"{{json .}}"') # Display the command to be executed Write-Verbose ("Executing: " + ($arguments -join " ")) $pinfo = New-Object System.Diagnostics.ProcessStartInfo $pinfo.FileName = "docker" $pinfo.RedirectStandardError = $true $pinfo.RedirectStandardOutput = $true $pinfo.CreateNoWindow = $true $pinfo.UseShellExecute = $false $pinfo.Arguments = $arguments $p = New-Object System.Diagnostics.Process $p.StartInfo = $pinfo $p.Start() | Out-Null # Get the output [string]$stdout = ""; [string]$stderr = ""; while ( !$p.HasExited ) { $stdout += $p.StandardOutput.ReadToEnd(); $stderr += $p.StandardError.ReadToEnd(); } #Write-Verbose $stdout #endregion # Check if there were any errors if($p.ExitCode -ne 0) { Write-Error $stderr return } # Convert the output to objects $data = "[" + $stdout.Replace("}`n{", "}, {") + "]" | ConvertFrom-Json #region Filter out any unwanted containers if($Name) { foreach($d in $data) { if($Name -eq $d.Names) { Write-Output $d } } } else { Write-Output $data } #endregion } End {} } #endregion #region New-DockerVolume function New-DockerVolume { [CmdletBinding()] param ( [Parameter()] [ValidateNotNullOrEmpty()] [String] $Name ) Begin { # Check if docker is installed and running try { Check-Docker } catch { throw "Could not find docker." } } Process { #region Create the volume # Form the arguments $arguments = @("volume", "create", $Name) # Display the command to be executed Write-Verbose ("Executing: " + ($arguments -join " ")) # Run the command $pinfo = New-Object System.Diagnostics.ProcessStartInfo $pinfo.FileName = "docker" $pinfo.RedirectStandardError = $true $pinfo.RedirectStandardOutput = $true $pinfo.CreateNoWindow = $true $pinfo.UseShellExecute = $false $pinfo.Arguments = $arguments $p = New-Object System.Diagnostics.Process $p.StartInfo = $pinfo $p.Start() | Out-Null # Get the output [string]$stdout = ""; [string]$sterr = ""; while ( !$p.HasExited ) { $stdout += $p.StandardOutput.ReadToEnd(); $stderr += $p.StandardError.ReadToEnd(); } # Write command output as verbose #Write-Verbose $stdout # Check if there were any errors if($p.ExitCode -ne 0) { Write-Error $stderr return } #endregion } End {} } #endregion #region Remove-DockerVolume function Remove-DockerVolume { [CmdletBinding()] param ( [Parameter()] [ValidateNotNullOrEmpty()] [String] $Name, [Parameter()] [ValidateNotNullOrEmpty()] [Switch] $Force ) Begin { # Check if docker is installed and running try { Check-Docker } catch { throw "Could not find docker." } } Process { #region Remove the volume # Form the arguments $arguments = @("volume", "rm") if($Force) { $arguments += "--force" } $arguments += $Name # Display the command to be executed Write-Verbose ("Executing: " + ($arguments -join " ")) # Run the command $pinfo = New-Object System.Diagnostics.ProcessStartInfo $pinfo.FileName = "docker" $pinfo.RedirectStandardError = $true $pinfo.RedirectStandardOutput = $true $pinfo.CreateNoWindow = $true $pinfo.UseShellExecute = $false $pinfo.Arguments = $arguments $p = New-Object System.Diagnostics.Process $p.StartInfo = $pinfo $p.Start() | Out-Null # Get the output [string]$stdout = ""; [string]$sterr = ""; while ( !$p.HasExited ) { $stdout += $p.StandardOutput.ReadToEnd(); $stderr += $p.StandardError.ReadToEnd(); } # Write command output as verbose #Write-Verbose $stdout # Check if there were any errors if($p.ExitCode -ne 0) { Write-Error $stderr return } #endregion } End {} } #endregion #region Helper Functions function Check-Docker { try { $pinfo = New-Object System.Diagnostics.ProcessStartInfo $pinfo.FileName = "docker.exe" $pinfo.RedirectStandardError = $true $pinfo.RedirectStandardOutput = $true $pinfo.CreateNoWindow = $true $pinfo.UseShellExecute = $false $pinfo.Arguments = @("--version") $p = New-Object System.Diagnostics.Process $p.StartInfo = $pinfo $p.Start() | Out-Null <# $p.WaitForExit() $stdout = $p.StandardOutput.ReadToEnd() $stderr = $p.StandardError.ReadToEnd() #> [string]$stdout = ""; while ( !$p.HasExited ) { $stdout += $p.StandardOutput.ReadToEnd(); } [string]$stderr = ""; while ( !$p.HasExited ) { $stderr += $p.StandardError.ReadToEnd(); } } catch { throw "Could not find docker." } } #endregion #endregion #region Exports Export-ModuleMember -Function "Get-DockerVolume" Export-ModuleMember -Function "Remove-DockerVolume" Export-ModuleMember -Function "New-DockerVolume" #endregion |