Public/Remove-Cygwin.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<#
.SYNOPSIS
    Short description
.DESCRIPTION
    Long description
.EXAMPLE
    Example of how to use this cmdlet
.EXAMPLE
    Another example of how to use this cmdlet
.INPUTS
    Inputs to this cmdlet (if any)
.OUTPUTS
    Output from this cmdlet (if any)
.NOTES
    General notes
.COMPONENT
    The component this cmdlet belongs to
.ROLE
    The role this cmdlet belongs to
.FUNCTIONALITY
    The functionality that best describes this cmdlet
#>

function Remove-Cygwin
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        [ValidateNotNull()]
        [Cygwin.InstallInfo] $CygwinEnvironment
    )

    process
    {
        Write-Verbose ($CygwinEnvironment | Format-List * | Out-String);

        if (-not($CygwinEnvironment.Exists))
        {
            Write-Warning ('Seems that directory [{0}] is already deleted.' -f $CygwinEnvironment.Path);
        }

        if (Test-Path -Path $CygwinEnvironment.Path -PathType Container)
        {
            if ($PSCmdlet.ShouldProcess(('{0}' -f $CygwinEnvironment.Path), 'Remove'))
            {
                try
                {
                    Remove-Item -Path $CygwinEnvironment.Path -Recurse -ErrorAction Stop
                }
                catch
                {
                    Write-Error ('Cannot remove: [{0}]' -f $CygwinEnvironment.Path);
                }
            }
            else
            {
                Write-Host ('Remove: [{0}]' -f $CygwinEnvironment.Path);
            }
        }

        if (-not($CygwinEnvironment.Exists))
        {
            $CygwinEnvironment.Registry | ForEach-Object {
                $reg = $_;
                $registryItemPath = $reg.Path -replace '^HKEY_CURRENT_USER', 'HKCU:' -replace '^HKEY_LOCAL_MACHINE', 'HKLM:';
                $registryItem = Get-ItemProperty -Path $registryItemPath -Name $reg.Name -ErrorAction SilentlyContinue;
                if ($null -ne $registryItem)
                {
                    if ($PSCmdlet.ShouldProcess(('{0}\{1}' -f $reg.Path, $reg.Name), 'Delete'))
                    {
                        try
                        {
                            Remove-ItemProperty -Path $registryItemPath -Name $reg.Name -ErrorAction Stop;
                        }
                        catch
                        {
                            Write-Warning ('Cannot remove from registry: [{0}] {1}' -f $reg.Path, $reg.Name);
                            if ($PSCmdlet.ShouldProcess("aa", "bb", "cc"))
                            {
                                try
                                {
                                    $scriptBlock = "Remove-ItemProperty -Path $registryItemPath -Name $($reg.Name) -ErrorAction Stop";
                                    Start-Process -FilePath (Get-Process -id $pid).Path -ArgumentList ('-Command &{{ {0} }}' -f $scriptBlock) -Verb RunAs -Wait -ErrorAction Stop
                                }
                                catch
                                {
                                    Write-Error ($_.Exception.Message);
                                }
                            }
                        }
                    }
                    else
                    {
                        Write-Host ('Remove: [{0}]' -f $CygwinEnvironment.Path);
                    }
                }
            }
        }
    }
}
# SIG # Begin signature block
# MIIFtAYJKoZIhvcNAQcCoIIFpTCCBaECAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUxo4JdZ1CWLpm2xjoByO5KQXr
# yoagggM9MIIDOTCCAiWgAwIBAgIQ0IShyb7pW4dHi1pXwpsXLzAJBgUrDgMCHQUA
# MCwxKjAoBgNVBAMTIVBvd2VyU2hlbGwgTG9jYWwgQ2VydGlmaWNhdGUgUm9vdDAe
# Fw0yMDAzMTUwOTEwNTBaFw0zOTEyMzEyMzU5NTlaMBUxEzARBgNVBAMTCmRvbWlu
# YXRpb24wggEiMA0GCSqGSIb3DQEBAQUA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