Dictionaries/Computer.Dict.Windows/Computer.Dict.Windows.psm1

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


<#
.SYNOPSIS
Get the computer name
 
.DESCRIPTION
 
.EXAMPLE
$name = Get-ComputerName
 
.NOTES
This function comes from Dict.Windows module.
#>

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

    Process {
        return $env:COMPUTERNAME
    }

    End {
        Write-LeaveFunction
    }
}

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

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

    Process {
        if (fileExist "Env:\USERDNSDOMAIN") {
            return $env:USERDNSDOMAIN
        }

        return 'localdomain'
    }

    End {
        Write-LeaveFunction
    }
}

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

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

    Process {
        return (Get-CimInstance -ClassName Win32_BIOS).SerialNumber
    }

    End {
        Write-LeaveFunction
    }
}

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

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

    Process {
        return (Get-CimInstance -ClassName Win32_BIOS).Manufacturer
    }

    End {
        Write-LeaveFunction
    }
}

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

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

    Process {
        return (Get-CimInstance -ClassName Win32_BIOS).SMBIOSBIOSVersion
    }

    End {
        Write-LeaveFunction
    }
}

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

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

    Process {
        # deprecated in Powershell Core
        # return (Get-WmiObject -Class:Win32_ComputerSystem).Model
        return (Get-CimInstance -ClassName Win32_ComputerSystem).Model
    }

    End {
        Write-LeaveFunction
    }
}

function Get-ComputerEthernet {
    [CmdletBinding()]Param (
        [switch]$localhost,
        [ValidateSet('Index', 'Name')]
        [string]$ListAvailable = 'Index'
    )
    Begin {
        Write-EnterFunction
    }

    Process {
        switch ($ListAvailable) {
            'Index' {
                (Get-NetAdapter -Physical).ifIndex | Sort-Object
                # (Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration).Index | Sort-Object
            }
            'Name' {
                # (Get-NetAdapter -IncludeHidden).Name | Sort-Object
                (Get-NetAdapter -Physical).Name | Sort-Object
            }
        }
    }

    End {
        Write-LeaveFunction
    }
}

<#
    .Synopsis
    Determines underlying firmware (BIOS) type and returns an integer indicating UEFI, Legacy BIOS or Unknown.
    Supported on Windows 8/Server 2012 or later
 
    .DESCRIPTION
    This function uses a complied Win32 API call to determine the underlying system firmware type.
 
    .EXAMPLE
    If (Get-BiosType -eq 1) { # System is running UEFI firmware... }
 
    .EXAMPLE
    Switch (Get-ComputerFirmwareType) {
        "BIOS" { "Legacy BIOS" }
        "UEFI" { "UEFI" }
        Default { "Unknown" }
    }
 
    .OUTPUTS
    string "BIOS" or "UEFI"
 
    .FUNCTIONALITY
    Determines underlying system firmware type
 
    .LINK
    https://gallery.technet.microsoft.com/scriptcenter/Determine-UEFI-or-Legacy-7dc79488
 
    .NOTES
    Author : Chris J Warwick https://social.technet.microsoft.com/profile/chris%20j%20warwick/
 
#>

function Get-ComputerFirmwareType {
    [CmdletBinding()][OutputType([String])]Param (
        [switch]$localhost,
        [string]$string = ""
    )
    Begin {
        # eenter($MyInvocation.MyCommand)
    }

    Process {
        Add-Type -Language CSharp -TypeDefinition @'
 
        using System;
        using System.Runtime.InteropServices;
 
        public class FirmwareType
        {
            [DllImport("kernel32.dll")]
            static extern bool GetFirmwareType(ref uint FirmwareType);
 
            public static uint GetFirmwareType()
            {
                uint firmwaretype = 0;
                if (GetFirmwareType(ref firmwaretype))
                    return firmwaretype;
                else
                    return 0; // API call failed, just return 'unknown'
            }
        }
'@


        $fw = "BIOS"
        $firmwareTypeNum = [FirmwareType]::GetFirmwareType()
        Switch ($firmwareTypeNum) {
            1       { $fw = "BIOS" }
            2       { $fw = "UEFI" }
            Default { $fw = "Unknown" }
        }

        return $fw

    }

    End {
        # eleave($MyInvocation.MyCommand)
    }
}

function Get-ComputerArch {
    [CmdletBinding()][OutputType([String])]Param (
        [switch]$localhost,
        [string]$string = ""
    )
    Begin {
        # eenter($MyInvocation.MyCommand)
    }

    Process {
        # found specs @url https://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx
        $value = (Get-CimInstance -ClassName CIM_Processor).DataWidth
        switch ($value) {
            32 {
                return 'x86'
            }
            64 {
                return 'x64'
            }
            else {
                return 'unknown'
            }
        }
    }

    End {
        # eleave($MyInvocation.MyCommand)
    }
}

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


function Get-EthIndex {
    [CmdletBinding()]Param(
        [Parameter(ValueFromPipeLine = $true)]
        [string]$AdapterName = "",
        [ValidateSet('IPv4', 'IPv6')]
        [string]$Family = 'IPv4'
    )

    Begin {
        Write-EnterFunction
    }

    Process {
        # switch ($Family) {
        # 'IPv4' {
        # $inet = "inet"
        # }
        # 'IPv6' {
        # $inet = "inet6"
        # }
        # }

        $value = (Get-NetAdapter -Name $AdapterName).ifIndex
        return $value
    }

    End {
        Write-LeaveFunction
    }
}

function Get-EthName {
    [CmdletBinding()]Param(
        [Parameter(ValueFromPipeLine = $true)]
        [int]$AdapterIndex = 0,
        [ValidateSet('IPv4', 'IPv6')]
        [string]$Family = 'IPv4'
    )

    Begin {
        Write-EnterFunction
    }

    Process {
        # switch ($Family) {
        # 'IPv4' {
        # $inet = "inet"
        # }
        # 'IPv6' {
        # $inet = "inet6"
        # }
        # }

        $value = (Get-NetAdapter -InterfaceIndex $AdapterIndex).Name
        return $value
    }

    End {
        Write-LeaveFunction
    }
}

function Get-EthLinkStatus {
    [CmdletBinding(
        DefaultParameterSetName="INDEX"
    )][OutputType([Boolean])]Param(
        # Parameter help description
        [Parameter(ParameterSetName="INDEX",ValueFromPipeLine = $true)]
        [int]$AdapterIndex = 1,

        [Parameter(ParameterSetName="NAME",ValueFromPipeLine = $true)]
        [string]$AdapterName = "lo"
    )

    Begin {
        Write-EnterFunction
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'INDEX' {
                $AdapterName = Get-EthName -AdapterIndex $AdapterIndex
            }
            'NAME' {
            }
        }

        $status = (Get-NetAdapter -Name $AdapterName).Status
        switch ($status) {
            'Up' {
                return $true
            }
            default {
                return $false
            }
        }
    }

    End {
        Write-LeaveFunction
    }
}

<#
.SYNOPSIS
Get network adapter speed
 
.DESCRIPTION
Ask OS for network adapter speed
 
.PARAMETER AdapterIndex
Adapter index
 
.PARAMETER AdapterName
Adapter name
 
.EXAMPLE
Get-EthSpeed -AdapterIndex 0
 
.EXAMPLE
Get-EthSpeed -AdapterName Ethernet0
 
.OUTPUTS
[uint32] return link speed in bytes per seconds
 
.NOTES
General notes
#>


function Get-EthSpeed {
    [CmdletBinding(
        DefaultParameterSetName="INDEX"
    )]Param(
        # Parameter help description
        [Parameter(ParameterSetName="INDEX",ValueFromPipeLine = $true)]
        [int]$AdapterIndex = 0,

        [Parameter(ParameterSetName="NAME",ValueFromPipeLine = $true)]
        [string]$AdapterName = "lo"
    )

    Begin {
        Write-EnterFunction
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'INDEX' {
                $AdapterName = Get-EthName -AdapterIndex $AdapterIndex
            }
            'NAME' {
            }
        }

        [uint32]$value = (Get-NetAdapter -name $AdapterName).Speed

        return $value
    }

    End {
        Write-LeaveFunction
    }
}

function Get-EthMacAddress {
    [CmdletBinding(
        DefaultParameterSetName="INDEX"
    )]Param(
        # Parameter help description
        [Parameter(ParameterSetName="INDEX",ValueFromPipeLine = $true)]
        [int]$AdapterIndex = 0,

        [Parameter(ParameterSetName="NAME",ValueFromPipeLine = $true)]
        [string]$AdapterName = "lo",

        [ValidateSet('IPv4', 'IPv6')]
        [string]$Family = 'IPv4'
    )

    Begin {
        Write-EnterFunction
    }

    Process {
        # switch ($Family) {
        # 'IPv4' {
        # $inet = "inet"
        # }
        # 'IPv6' {
        # $inet = "inet6"
        # }
        # }
        switch ($PSCmdlet.ParameterSetName) {
            'INDEX' {
                $AdapterName = Get-EthName -AdapterIndex $AdapterIndex
            }
            'NAME' {
            }
        }

        $value = (Get-NetAdapter -name $AdapterName).MacAddress.ToLower().Replace('-',':')

        return $value
    }

    End {
        Write-LeaveFunction
    }
}

function Get-EthIPAddress {
    [CmdletBinding(
        DefaultParameterSetName="INDEX"
    )]Param(
        # Parameter help description
        [Parameter(ParameterSetName="INDEX",ValueFromPipeLine = $true)]
        [int]$AdapterIndex = 0,

        [Parameter(ParameterSetName="NAME",ValueFromPipeLine = $true)]
        [string]$AdapterName = "lo",

        [ValidateSet('IPv4', 'IPv6')]
        [string]$Family = 'IPv4'
    )

    Begin {
        Write-EnterFunction
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'INDEX' {
            }
            'NAME' {
                $AdapterIndex = Get-EthIndex -AdapterName $AdapterName
            }
        }

        try {
            $value = (Get-NetIPaddress -InterfaceIndex $AdapterIndex -AddressFamily $Family -ErrorAction:SilentlyContinue).IPAddress
        } catch {
            $value = [ipaddress]"0.0.0.0"
        }

        return $value
    }

    End {
        Write-LeaveFunction
    }
}

function Get-EthNetMask {
    [CmdletBinding(
        DefaultParameterSetName="INDEX"
    )]Param(
        # Parameter help description
        [Parameter(ParameterSetName="INDEX",ValueFromPipeLine = $true)]
        [int]$AdapterIndex = 0,

        [Parameter(ParameterSetName="NAME",ValueFromPipeLine = $true)]
        [string]$AdapterName = "lo",

        [ValidateSet('IPv4', 'IPv6')]
        [string]$Family = 'IPv4'
    )

    Begin {
        Write-EnterFunction
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'INDEX' {
            }
            'NAME' {
                $AdapterIndex = Get-EthIndex -AdapterName $AdapterName
            }
        }
        switch ($Family) {
            'IPv4' {
                try {
                    $mask = (Get-NetIPConfiguration -InterfaceIndex $AdapterIndex).ipv4address.prefixlength
                    $value = ConvertTo-Mask -MaskLength $mask
                } catch {
                    $value = [ipaddress]"255.255.255.255"
                }
            }
            'IPv6' {
                # $mask = (Get-NetIPConfiguration -InterfaceIndex $AdapterIndex).ipv6address.prefixlength
                ewarn("Module Indented.Net.IP does not provides IPv6 calculator yet !!")
            }
        }

        return $value
    }

    End {
        Write-LeaveFunction
    }
}

function Get-EthBroadcast {
    [CmdletBinding(
        DefaultParameterSetName="INDEX"
    )]Param(
        # Parameter help description
        [Parameter(ParameterSetName="INDEX",ValueFromPipeLine = $true)]
        [int]$AdapterIndex = 0,

        [Parameter(ParameterSetName="NAME",ValueFromPipeLine = $true)]
        [string]$AdapterName = "lo",

        [ValidateSet('IPv4', 'IPv6')]
        [string]$Family = 'IPv4'
    )

    Begin {
        Write-EnterFunction
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'INDEX' {
            }
            'NAME' {
                $AdapterIndex = Get-EthIndex -AdapterName $AdapterName
            }
        }
        $ip = Get-EthIPAddress -AdapterIndex $AdapterIndex -Family $Family
        $mask = Get-EthNetMask -AdapterIndex $AdapterIndex -Family $Family

        switch ($Family) {
            'IPv4' {
                $value = Get-BroadcastAddress -IPAddress $ip -SubnetMask $mask
            }
            'IPv6' {
                ewarn("Module Indented.Net.IP does not provides IPv6 calculator yet !!")
            }
        }

        return $value
    }

    End {
        Write-LeaveFunction
    }
}

function Get-EthGateway {
    [CmdletBinding(
        DefaultParameterSetName="INDEX"
    )]Param(
        # Parameter help description
        [Parameter(ParameterSetName="INDEX",ValueFromPipeLine = $true)]
        [int]$AdapterIndex = 0,

        [Parameter(ParameterSetName="NAME",ValueFromPipeLine = $true)]
        [string]$AdapterName = "lo",

        [ValidateSet('IPv4', 'IPv6')]
        [string]$Family = 'IPv4'
    )

    Begin {
        Write-EnterFunction
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'INDEX' {
            }
            'NAME' {
                $AdapterIndex = Get-EthIndex -AdapterName $AdapterName
            }
        }

        switch ($Family) {
            'IPv4' {
                try {
                    $value = (get-netroute -ifIndex $AdapterIndex -DestinationPrefix 0.0.0.0/0).NextHop
                } catch {
                    $value = [ipaddress]"0.0.0.0"
                }
            }
            'IPv6' {
                $value = [ipaddress]"0.0.0.0"
                # $value = (get-netroute -ifIndex $AdapterIndex -DestinationPrefix 0.0.0.0/0).NextHop
            }
        }

        return $value
    }

    End {
        Write-LeaveFunction
    }
}


function Get-ComputerDisks {
    [CmdletBinding()]Param (
        [switch]$localhost,
        [ValidateSet('Index', 'Name')]
        [string]$ListAvailable = 'Index'
    )
    Begin {
        Write-EnterFunction
        $precedence = @('Fixed')
    }

    Process {
        if ($Localhost) {
            foreach ($p in $precedence) {
                switch ($ListAvailable) {
                    'Index' {
                        (Get-Disk | Where-Object { $_.ProvisioningType -eq $p }).Number
                    }
                    'Name' {
                        (Get-Disk | Where-Object { $_.ProvisioningType -eq $p }).UniqeId
                    }
                }
            }
        }
    }

    End {
        Write-LeaveFunction
    }
}

function Get-ComputerDisks2 {
    [CmdletBinding(
        DefaultParameterSetName="ALL"
    )]Param(
        [switch]$localhost,
        [ValidateSet('Index', 'Name')]
        [string]$ListAvailable = 'Index',
        [Parameter(ParameterSetName="INDEX",ValueFromPipeLine = $true)]
        [int]$Index,

        [Parameter(ParameterSetName="NAME",ValueFromPipeLine = $true)]
        [string]$Name
    )

    Begin {
        $disks = @()
        # precedence tell the order search precedence of the type of disk. It aims to mimic BIOS / UEFI discovery order
        $list = Get-Disk
    }

    Process {
        ForEach ($hash in $list) {
            $disk = $null
            # $disk = [PSCustomObject]@{
            # Index = $i++
            # Name = $d
            # serial = $lsblk.serial
            # uuid = $lsblk.uuid
            # manufacturer = $lsblk.vendor
            # model = $lsblk.model
            # PhysicalSectorSize = $lsblk."phy-sec"
            # size = ($lsblk.size.replace(',','.') + "B")/1 | Convert-Size -From Bytes -To Bytes -Precision 0
            # PartitionStyle = ""
            # DriveType = ""
            # }
            $disk = @{}
            $disk.Index = $hash.Number
            $disk.name = $hash.FriendlyName
            $disk.serial = $hash.SerialNumber
            $disk.uuid = $hash.uuid
            $disk.manufacturer = $hash.Manufacturer
            $disk.model = $hash.Model
            $disk.PhysicalSectorSize = $hash.PhysicalSectorSize
            $disk.size = $hash.size | Convert-Size -From Bytes -To Bytes -Precision 0
            $disk.PartitionStyle = $hash.PartitionStyle
            $disk.DriveType = ""
            switch ($hash.ProvisioningType) {
                'Fixed' {
                    $disk.DriveType = 'Fixed'
                }
                'Removable' {
                    $disk.DriveType = 'Removable'
                }
            }
            $disks += $disk
        }

        switch ($PSCmdlet.ParameterSetName) {
            'INDEX' {
                return $disks[$Index]
            }
            'NAME' {
                $Index = ($disks | Where-Object { $_.Name -eq $Name }).Index
                if ($null -ne $Index) {
                    return $disks[$Index]
                } else {
                    return
                }
            }
            'ALL' {
                return $disks
            }
        }
    }

    End {

    }
}

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


function Get-DiskInfos {
    [CmdletBinding(
        DefaultParameterSetName="INDEX"
    )]Param(
        [switch]$localhost,
        [Parameter(ParameterSetName="INDEX",ValueFromPipeLine = $true)]
        [int]$DiskIndex,

        [Parameter(ParameterSetName="NAME",ValueFromPipeLine = $true)]
        [string]$DiskName
    )
    Begin {
        # eenter($MyInvocation.MyCommand)
    }

    Process {
        switch ($PSCmdlet.ParameterSetName) {
            'INDEX' {
            }
            'NAME' {
                $DiskIndex = (Get-Disk | Where-Object { $_.UniqueId -eq $DiskName }).Number
            }
        }

        $disk = Get-Disk -Number $DiskIndex
        $obj = [PSCustomObject]@{
            name = $disk.UniquId
            device = $disk.Path
            index = $disk.Number
            serial = $disk.SerialNumber
            uuid = $disk.Guid
            manufacturer = $disk.Manufacturer
            model = $disk.Model
            PhysicalSectorSize = $disk.PhysicalSectorSize
            default = $disk.IsBoot
            size = $disk.size
            PartitionStyle = $disk.PartitionStyle
            DriveType = $disk.ProvisioningType
        }

        return $obj
    }

    End {
        # eleave($MyInvocation.MyCommand)
    }
}