Dictionaries/Computer.Dict.Linux/Computer.Dict.Linux.psm1

<#
 
  ###### ####### ## ## ######## ## ## ######## ######## ########
 ## ## ## ## ### ### ## ## ## ## ## ## ## ##
 ## ## ## #### #### ## ## ## ## ## ## ## ##
 ## ## ## ## ### ## ######## ## ## ## ###### ########
 ## ## ## ## ## ## ## ## ## ## ## ##
 ## ## ## ## ## ## ## ## ## ## ## ## ##
  ###### ####### ## ## ## ####### ## ######## ## ##
 
#>


<#
.SYNOPSIS
Get the computer name
 
.DESCRIPTION
 
.PARAMETER localhost
request localhost's ComputerName
 
.EXAMPLE
$name = Get-ComputerName
 
.NOTES
This function comes from Dict.Unix module.
#>

function Get-ComputerName {
    [CmdletBinding()]Param (
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        # delete everything after 1st dot to get only hostname (e.g. without domainname)
        return (hostname) -replace "\..*"
    }

    End {
        Write-LeaveFunction
    }
}

function Get-ComputerDomain {
    hostname -d
}

function Get-ComputerManufacturer {
    [CmdletBinding()][OutputType([String])]Param (
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        return (New-HashtableFromCommand -Command "dmidecode | awk '/^System Information/,/^$/'" -sep ":" -SkipFirstLines 1).manufacturer
    }

    End {
        Write-LeaveFunction
    }
}

function Get-ComputerModel {
    [CmdletBinding()]Param (
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        return (dmidecode | awk '/^System Information/,/^$/' | Select-Object -Skip 1 | ConvertFrom-StringData -Delimiter ":")."product name"
        # return (New-HashtableFromCommand -Command "dmidecode | awk '/^System Information/,/^$/'" -sep ":" -SkipFirstLines 1)."product name"
    }

    End {
        Write-LeaveFunction
    }
}

function Get-ComputerSerialNumber {
    [CmdletBinding()]Param (
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        return (New-HashtableFromCommand -Command "dmidecode | awk '/^System Information/,/^$/'" -sep ":" -SkipFirstLines 1)."serial number"
    }

    End {
        Write-LeaveFunction
    }
}

<#
 
 ######## #### ###### ## ##
 ## ## ## ## ## ## ##
 ## ## ## ## ## ##
 ## ## ## ###### #####
 ## ## ## ## ## ##
 ## ## ## ## ## ## ##
 ######## #### ###### ## ##
 
#>


<#
.SYNOPSIS
List local disks
 
.DESCRIPTION
Fetch all informations available about local disks. Linux lacks the 'Get-Disk' of the Storage module available on Windows.
 
.EXAMPLE
An example
 
.NOTES
General notes
#>

function Get-ComputerDisk {
    [CmdletBinding()]
    [OutputType([hashtable])]
    Param (
        # Disk number
        [Parameter(Mandatory = $true, ParameterSetName = 'DISKNUMBER')][string]$DiskNumber,

        # Disk device name (without /dev)
        [Parameter(Mandatory = $true, ParameterSetName = 'DISKNAME')][string]$DiskName,

        # Full device address like in '/dev/sda'
        [Parameter(Mandatory = $true, ParameterSetName = 'DEVICE')][string]$Device,

        # Partition of the disk given its ID
        [Alias('UniqueId')]
        [Parameter(Mandatory = $true, ParameterSetName = 'DISKID')][string]$DiskId
    )
    Begin {
        Write-EnterFunction
        $disks = ((lsblk --output-all --json --tree) | convertfrom-json).blockdevices | Where-Object { $_.type -eq "disk" }
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'DISKNUMBER' {
                return $disks[$DiskNumber]
            }
            'DISKNAME' {
                return $disks | Where-Object { $_.name -eq $DiskName }
            }
            'DEVICE' {
                return $disks | Where-Object { $_.path -eq $Device }
            }
            'DISKID' {
                return $disks | Where-Object { $_.ptuuid -eq $DiskId }
            }
            default {
                return $disks
            }
        }
    }

    End {
        Write-LeaveFunction
    }
}

<#
.SYNOPSIS
List local partitions
 
.DESCRIPTION
Fetch all informations available about local partitions. Linux lacks the 'Get-Partition' of the Storage module available on Windows.
 
.EXAMPLE
An example
 
.NOTES
General notes
#>

function Get-ComputerDiskPartition {
    [CmdletBinding()]
    [OutputType([hashtable], [void])]
    Param (
        # Partition with UUID
        [Alias('UniqueId')]
        [Parameter(Mandatory = $true, ParameterSetName = 'PARTUUID')][string]$PartUUID,

        # Label of the partition to mount
        [Alias('Label', 'PartitionLabel')]
        [Parameter(Mandatory = $true, ParameterSetName = 'PARTLABEL')][string]$PartLabel,

        # Disk device name of the partition
        [Parameter(Mandatory = $true, ParameterSetName = 'DISKPART')][string]$Disk,

        # Partition number (can be several strings char since nvme disks. e.g. 'p1')
        [Alias('PartitionNumber')]
        [Parameter(Mandatory = $false, ParameterSetName = 'DISKPART')][string]$PartNum,

        # Full device address like in '/dev/sda1'
        [Parameter(Mandatory = $true, ParameterSetName = 'DEVICE')][string]$Device,

        # Mountpoint of a mounted partition
        [Alias('DriveLetter')]
        [Parameter(Mandatory = $true, ParameterSetName = 'MOUNTPOINT')][string]$Mountpoint,

        # Partition of the disk given its ID
        [Parameter(Mandatory = $true, ParameterSetName = 'DISKID')][string]$DiskId

    )
    Begin {
        Write-EnterFunction
        $disks = Get-ComputerDisk
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'PARTUUID' {
                return ($disks.children | Where-Object { $_.partuuid -eq "$PartUUID" })
            }
            'PARTLABEL' {
                return ($disks.children | Where-Object { $_.label -eq "$PartLabel" })
            }
            'DISKPART' {
                $partitions = ($disks | Where-Object { $_.name -eq $Disk }).children
                if ($PartNumber) {
                    return ($partitions | Where-Object { $_.name -eq "$Disk$PartNumber" })
                } else {
                    return $partitions
                }
            }
            'DEVICE' {
                return ($disks.children | Where-Object { $_.path -like "$Device*" })
            }
            'MOUNTPOINT' {
                return ($disks.children | Where-Object { $_.mountpoint -eq "$Mountpoint" })
            }
            'DISKID' {
                return ($disks.children | Where-Object { $_.ptuuid -eq "$DiskId" })
            }
            default {
                return disks.children
            }
        }
    }

    End {
        Write-LeaveFunction
    }
}

<#
.SYNOPSIS
Mount a partition into running operating system
 
.DESCRIPTION
Mount specified partition
 
.EXAMPLE
An example
 
.NOTES
General notes
 
.OUTPUTS
[string] mountpoint
#>

function Mount-ComputerDiskPartition {
    [CmdletBinding()]
    [OutputType([String])]
    Param (
        # UUID of the partition to mount
        [Parameter(Mandatory = $true, ParameterSetName = 'PARTUUID')][string]$PartUuid,

        # Label of the partition to mount
        [Alias('Label', 'PartitionLabel')]
        [Parameter(Mandatory = $true, ParameterSetName = 'PARTLABEL')][string]$PartLabel,

        # Disk device name of the partition
        [Parameter(Mandatory = $true, ParameterSetName = 'DISKPART')][string]$Disk,

        # Partition number (can be several strings char since nvme disks. e.g. 'p1')
        [Parameter(Mandatory = $true, ParameterSetName = 'DISKPART')][string]$PartNum,

        # Full device address like in '/dev/sda1'
        [Parameter(Mandatory = $true, ParameterSetName = 'DEVICE')][string]$Device,

        # options to pass
        [Parameter(Mandatory = $false)][string]$Options,

        # force mounting to the mountpoint
        [Parameter(Mandatory = $false)][string]$Mountpoint
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'PARTUUID' {
                $partition = Get-ComputerDiskPartition -PartUUID $PartUuid
            }
            'PARTLABEL' {
                $partition = Get-ComputerDiskPartition -PartLabel $PartLabel
            }
            'DISKPART' {
                $partition = Get-ComputerDiskPartition -Disk $Disk -PartNum $PartNum
            }
            'DEVICE' {
                $partition = Get-ComputerDiskPartition -Device $Device
            }
        }
        # partition is already mounted ? return mountpoint
        if ($partition.mountpoint) {
            Write-Debug "Partition $($partition.name) already mounted at '$partition.mountpoint'"
            return $partition.mountpoint
        }
        if (!$Mountpoint) {
            $Mountpoint = "/mnt/$($partition.name)"
        }
        $rc = New-Item $Mountpoint -ItemType Directory -Force
        Write-Debug "Mount partition $($partition.name) in $Mountpoint"
        switch ($partition.fstype) {
            'ntfs' {
                # options is an array of comma separated values
                $aOptions = $Options -split ","
                # probe for mountability
                $rc = Execute-Command "ntfs-3g.probe --readwrite $($partition.path)" -AsBool
                rc=$?
                switch (${rc}) {
                    14 {
                        Write-Warning "Windows is hibernating, ntfs-3g will refuse to mount it."
                        Write-Warning "WRN" "We'll try to mount removing hiberfile"
                        # if ${options} is defined, then OPTIONS="${options},ro" else, OPTIONS="ro"
                        $aOptions += "remove_hiberfile"
                    }
                }
                # Make sure the dirty flag is removed. If not, mounting the NTFS partition is impossible.
                $rc = Execute-Command "ntfsfix -d $($partition.path)"
                # if ${options} is defined, then OPTIONS="-o ${options}" else, OPTIONS=""
                $rc = Execute-Command "ntfs-3g $($partition.path) $MountPoint $($aOptions -join ",")"
            }
            Default {}
        }
        return $Mountpoint
    }

    End {
        Write-LeaveFunction
    }
}