-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocker.Containers.Clear.ps1
66 lines (53 loc) · 1.95 KB
/
Docker.Containers.Clear.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<#
.SYNOPSIS
Clear all docker containers.
.DESCRIPTION
Clear all docker containers.
.PARAMETER DockerCiZapPath
Path to "docker-ci-zap.exe".
.PARAMETER DockerFilesLocation
Docker files folder path.
.PARAMETER Log
When specified, a log file is generated under "<Location of the current script>\Logs\".
.EXAMPLE
.\Docker.Containers.Clear.ps1 -DockerCiZapPath = 'C:\Program Files\docker-ci-zap\docker-ci-zap.exe' -DockerFilesLocation = 'C:\ProgramData\docker' -Log
.NOTES
Author: Ifthen CHERMAK
Version 0.0.1 (2020/10/09) : Initial version.
#>
Param (
[parameter(Mandatory=$False)] [String]$DockerCiZapPath = 'C:\Program Files\docker-ci-zap\docker-ci-zap.exe',
[parameter(Mandatory=$False)] [String]$DockerFilesLocation = 'C:\ProgramData\docker',
[parameter(Mandatory = $False)] [Switch]$Log
)
If ($Log) {
$LogFilePath = "$PSScriptRoot\Logs\" + $MyInvocation.MyCommand.Name + '_' + (Get-Date -UFormat '%Y%m%d').ToString() + (Get-Date -Format '%H%m%s').ToString() + '.log'
Start-Transcript -Path $LogFilePath
}
Try {
# Ensure the Docker Engine service is strated
Start-Service 'docker'
# Stop all running containers
docker ps -aq | ForEach-Object {docker stop $_}
# Remove all containers
docker ps -aq | ForEach-Object {docker rm $_}
# Remove all images
docker images -q | ForEach-Object {docker rmi $_}
# Use docker-ci-zap to clear all docker files
If (Get-Service 'docker' -ErrorAction SilentlyContinue) {
If ((Get-Service -Name 'docker').Status -eq "Running") {
Stop-Service 'docker'
}
}
$Command = """$DockerCiZapPath"" -folder ""$DockerFilesLocation"""
Invoke-Expression -Command:"cmd.exe /c $Command"
If (Get-Service 'docker' -ErrorAction SilentlyContinue) {
Start-Service 'docker'
}
}
Catch {
Write-Error "$($_.Exception.Message)`n$($_.ErrorDetails.Message)"
}
If ($Log) {
Stop-Transcript
}