Dictionaries/Dict.Unix/Dict.Unix.psm1

<#

    .SYNOPSIS
    Dictionnary of Unix functions

    .DESCRIPTION

    .NOTES
        Author: Charles-Antoine Degennes <cadegenn@gmail.com>
        Version: 0.0.1
        Changelog:
            2018.07.30, DCA - added Test-UserIsAdmin() function
            2018.02.07, DCA - inital version
        Manifest created with command line:
        New-ModuleManifest PwSh.Unix.psd1 -RootModule PwSh.Unix.psm1 -ModuleVersion "0.0.1" -Author "Charles-Antoine Degennes <cadegenn@gmail.com>"

#>


<#
.SYNOPSIS
Test if a user (or current user) is admin

.DESCRIPTION
This function can take an uid or a username as argument.

.EXAMPLE
Test-UserIsAdmin
Test if currently logged on user have admin rights (= is root)

.EXAMPLE
Test-UserIsAdmin -username root
Test is user 'root' have admin rights

.OUTPUTS
$true is provided user is admin, $false otherwise
#>

function Test-UserIsAdmin {
    [CmdletBinding(DefaultParameterSetName="username")][OutputType([Boolean])]Param (
        [Parameter(ParameterSetName = "username")]
        [Alias("user", "name", "login")]
        [string]$Username = $env:USER,
        [Parameter(ParameterSetName = "id")]
        [Alias("uid", "sid")]
        [string]$id = (id -u)
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'username' {
                return ((id -u $username) -eq 0)
            }
            'id' {
                return ((id -u $id) -eq 0)
            }
            default {
                # unknown parametersetname
                return $false
            }
        }

        return $false
   }

    End {
        Write-PwShFwOSLeaveFunction
    }
}

<#

  ####### ######
 ## ## ## ##
 ## ## ##
 ## ## ######
 ## ## ##
 ## ## ## ##
  ####### ######

#>


<#

  ###### ####### ## ## ######## ## ## ######## ######## ########
 ## ## ## ## ### ### ## ## ## ## ## ## ## ##
 ## ## ## #### #### ## ## ## ## ## ## ## ##
 ## ## ## ## ### ## ######## ## ## ## ###### ########
 ## ## ## ## ## ## ## ## ## ## ## ##
 ## ## ## ## ## ## ## ## ## ## ## ## ##
  ###### ####### ## ## ## ####### ## ######## ## ##

#>


<#
.SYNOPSIS
Get the computer name

.DESCRIPTION

.PARAMETER localhost
request localhost's ComputerName

.EXAMPLE
$name = Get-ComputerName -Localhost

.NOTES
This function comes from Dict.Unix module.
#>

function Get-ComputerName {
    [CmdletBinding()]Param (
        [switch]$localhost
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    Process {
        if ($Localhost) {
            return hostname
        }
    }

    End {
        Write-PwShFwOSLeaveFunction
    }
}

function Get-ComputerDomain {
    hostname -d
}

function Get-ComputerManufacturer {
    [CmdletBinding()][OutputType([String])]Param (
        [switch]$localhost
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

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

    End {
        Write-PwShFwOSLeaveFunction
    }
}

function Get-ComputerModel {
    [CmdletBinding()]Param (
        [switch]$localhost
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    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-PwShFwOSLeaveFunction
    }
}

function Get-ComputerSerialNumber {
    [CmdletBinding()]Param (
        [switch]$localhost
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

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

    End {
        Write-PwShFwOSLeaveFunction
    }
}

<#

  ####### ######## ## ## ######## ########
 ## ## ## ## ## ## ## ##
 ## ## ## ## ## ## ## ##
 ## ## ## ######### ###### ########
 ## ## ## ## ## ## ## ##
 ## ## ## ## ## ## ## ##
  ####### ## ## ## ######## ## ##

#>



<#
.SYNOPSIS
Reload environment variables from system

.DESCRIPTION
Powershell compute environment variables at startup of powershell process. Update-Environment update variables from system.

.EXAMPLE
Update-Environment

.NOTES
Source found @url https://stackoverflow.com/questions/14381650/how-to-update-windows-powershell-session-environment-variables-from-registry
#>


function Update-Environment {
    [CmdletBinding()]Param (
        # [Parameter(Mandatory = $true,ValueFromPipeLine = $true)][string]$string
    )
    Begin {
        Write-PwShFwOSEnterFunction
    }

    Process {
        [Environment]::GetEnvironmentVariables().GetEnumerator() | ForEach-Object {
            # For Path variables, append the new values, if they're not already in there
            # Write-PwShFwOSDevel $_.Name + " = " + $_.Value
            if($_.Name -match 'Path$') {
                $_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select-Object -unique) -join ';'
            }
            $_
        } | Set-Content -Path { "Env:$($_.Name)" }
    }

    End {
        Write-PwShFwOSLeaveFunction
    }
}

New-Alias -Name userIsAdmin -Value Test-UserIsAdmin -Force
New-Alias -Name isAdmin     -Value Test-UserIsAdmin -Force
New-Alias -Name IAmAdmin    -Value Test-UserIsAdmin -Force
New-Alias -Name AmIAdmin    -Value Test-UserIsAdmin -Force