Libraries/OS.Debian/OS.Debian.psm1

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
$Script:NS = (get-item $PSCommandPath).basename

<#
.SYNOPSIS
Get Debian-based OS architecture
 
.DESCRIPTION
 
 
.PARAMETER Online
Specify you want to get the archtiecture of the running OS. It is equivalent of specifying -Root /
 
.PARAMETER Root
Specify to inquire the OS located at the specified mountpoint
 
.EXAMPLE
Get-OSArch -Online
 
.EXAMPLE
Get-OSArch -Root /mnt/sda3
 
.NOTES
 
#>


function Get-OSArch {
    [CmdletBinding()][OutputType([String])]Param (
        [switch]$Online,
        [string]$Root = "/"
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        if ($Online) { $Root = '/' }

        if (fileExist "$Root/var/lib/dpkg/arch") {
            $x86 = Get-Content "$Root/var/lib/dpkg/arch" | Select-String "^i386$"
            $x64 = Get-Content "$Root/var/lib/dpkg/arch" | Select-String "^amd64$"
            $arm32 = Get-Content "$Root/var/lib/dpkg/arch" | Select-String "^armhf$"
            $arm64 = Get-Content "$Root/var/lib/dpkg/arch" | Select-String "^arm64$"
        } elseif (fileExist "$Root/var/lib/dpkg/status") {
            $x86 = Get-Content "$Root/var/lib/dpkg/status" | Select-String "^Architecture: i386" | Select-Object -First 1
            $x64 = Get-Content "$Root/var/lib/dpkg/status" | Select-String "^Architecture: amd64" | Select-Object -First 1
            $arm32 = Get-Content "$Root/var/lib/dpkg/status" | Select-String "^Architecture: armhf" | Select-Object -First 1
            $arm64 = Get-Content "$Root/var/lib/dpkg/status" | Select-String "^Architecture: arm64" | Select-Object -First 1
        }

        if ($x86) {    $arch = "x86" }
        if ($x64) {    $arch = "x64" }
        if ($arm32) {    $arch = "arm32" }
        if ($arm64) {    $arch = "arm64" }

        return $arch
    }

    End {
        Write-LeaveFunction
    }
}

function Get-OSPackages {
    [CmdletBinding()][OutputType([Hashtable])]Param (
        [switch]$Online,
        [string]$Root = "/"
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        if ($Online) { $Root = '' }
        $File = "$Root/var/lib/dpkg/status"
        # $aPackages = @()
        $hPackages = @{}
        # get a raw string (not an array of strings)
        $status = Get-Content $File -Raw
        # extract each paragraph to a single match
        $aStatus = $status | select-string -pattern '(?sm)^Package: .*?^$' -AllMatches
        ForEach ($m in $aStatus.Matches) {
            # extract paragraphs one-by-one
            $sPackage = $m.Value
            # escape offending \[a-z] characters
            $sPackage = $sPackage -replace '(?sm)\\', '\\\\'
            # convert multilines to a single line with '\n' chars
            $sPackage = $sPackage -replace '(?sm)\r?\n ', '\n '
            # convert "key: value" to "key = value"
            $sPackage = $sPackage -replace '(?sm)^([A-Z].*?):', '$1 ='
            # convert to data
            $oPackage = $sPackage | ConvertFrom-StringData
            # add to hastable
            if (-not($hPackages.Contains($oPackage.Package))) {
                $hPackages.Add($oPackage.Package, $oPackage)
            }
        }

        return ($hPackages | Sort-HashTable)
        # return $hPackages
    }

    End {
        Write-LeaveFunction
    }
}

function Get-OSPackagenameFromFilename {
    [CmdletBinding()][OutputType([String])]Param (
        [switch]$Online,
        [string]$Root = "/",
        [string]$Filename
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        if ($Online) { $Root = '' }
        $infoDir = "$Root/var/lib/dpkg/info"
        $found = Select-String -Path $($infoDir + "/*.list") -Pattern $("^" + $Filename + "$") | Select-Object Path
        if ($found) {
            $dpkgInfoFile = Get-Item ($found).Path
            return $dpkgInfoFile.BaseName
        } else {
            return $null
        }
    }

    End {
        Write-LeaveFunction
    }
}