NN.MrfkCommands.psm1

#Region './Public/Get-MrfkAdmCreds.ps1' 0
function Get-MrfkAdmCreds {
    param (
        [string]$admCredsPath = "$env:USERPROFILE\.creds\MRFK\adm_creds.xml"
    )

    if (!(Test-Path $admCredsPath)) {
        New-MrfkAdmCreds
    }
    
    Import-Clixml $admCredsPath
}
#EndRegion './Public/Get-MrfkAdmCreds.ps1' 12
#Region './Public/Get-MrfkComputerInfo.ps1' 0
function Get-MrfkComputerInfo {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory,ParameterSetName="Get computer by hostname")]$Hostname,
        [Parameter(Mandatory,ParameterSetName="Get computers by username")]$Username,
        $MECMNameSpace = "root/SMS/site_PS1",
        $MECMHost = "sccm-ps.intern.mrfylke.no",
        $DC = "dc01.intern.mrfylke.no"
    )

    begin {
        try {
            $null = Get-ADUser -Filter "Name -eq 0"
        }
        catch [System.Management.Automation.CommandNotFoundException] {
            Write-Error -ErrorAction Stop -Message @"
Please install RSAT before running this function. You can install RSAT by following this guide:
https://github.com/NorskNoobing/NN.MrfkCommands#prerequisites
"@

        }
    }

    process {
        $ComputerExportArr = New-Object -TypeName System.Collections.ArrayList

        $splat = @{
            "Credential" = Get-MrfkAdmCreds
            "ComputerName" = $MECMHost
            "ErrorAction" = "Stop"
        }
        $CimSession = New-CimSession @splat

        switch ($PsCmdlet.ParameterSetName) {
            "Get computer by hostname" {
                [array]$HostnameArr = $Hostname
            }
            "Get computers by username" {
                $splat = @{
                    "Query" = "Select * from SMS_R_System where LastLogonUserName = `"$Username`""
                    "Namespace" = $MECMNameSpace
                    "CimSession" = $CimSession
                }
                [array]$HostnameArr = (Get-CimInstance @splat).Name
            }
        }

        $HostnameArr.ForEach({
            $splat = @{
                "Query" = "Select * from SMS_R_System where name = `"$_`""
                "Namespace" = $MECMNameSpace
                "CimSession" = $CimSession
            }
            $MecmComputer = Get-CimInstance @splat
            
            $splat = @{
                "Query" = @"
select distinct SMS_G_System_PROCESSOR.*
from SMS_R_System
inner join SMS_G_System_PROCESSOR
on SMS_G_System_PROCESSOR.ResourceID = SMS_R_System.ResourceId
where ResourceId = $($MecmComputer.ResourceId)
"@

                "Namespace" = $MECMNameSpace
                "CimSession" = $CimSession
            }
            $CPUInfo = Get-CimInstance @splat
            
            $splat = @{
                "Query" = @"
Select * from SMS_G_System_Computer_System_Product
where ResourceId = $($MecmComputer.ResourceId)
"@

                "Namespace" = $MECMNameSpace
                "CimSession" = $CimSession
            }
            $ModelInfo = Get-CimInstance @splat
            
            $ADComputer = Get-ADComputer $_
            
            $splat = @{
                "Filter" = {objectclass -eq "msFVE-RecoveryInformation"}
                "SearchBase" = $ADComputer.DistinguishedName
                "Properties" = "msFVE-RecoveryPassword"
                "Credential" = Get-MrfkAdmCreds
                "Server" = $DC
            }
            $BitlockerRecoveryKeys = (Get-ADObject @splat)."msFVE-RecoveryPassword"
            
            if ($MecmComputer.agentname) {
                $HeartbeatIndex = $MecmComputer.agentname.IndexOf("Heartbeat Discovery")
                $LastHeartbeat = $MecmComputer.agenttime[$HeartbeatIndex]
            }
            
            $null = $ComputerExportArr.Add(
                [PSCustomObject]@{
                    "Hostname" = $_
                    "LastLoggedOnUser" = $MecmComputer.LastLogonUserName
                    "MACAddresses" = $MecmComputer.MACAddresses
                    "Model" = $ModelInfo.Name
                    "CPUName" = $CPUInfo.Name
                    "SN" = $ModelInfo.IdentifyingNumber
                    "LastHeartbeat" = $LastHeartbeat
                    "BitlockerRecoveryKeys" = $BitlockerRecoveryKeys
                }
            )
        })
        $ComputerExportArr
    }
}
#EndRegion './Public/Get-MrfkComputerInfo.ps1' 110
#Region './Public/Get-MrfkUserInfo.ps1' 0
function Get-MrfkUserInfo {
    [CmdletBinding()]
    param (
        [Parameter(ParameterSetName="username")][string]$Username,
        [Parameter(ParameterSetName="displayname")][string]$DisplayName,
        [Parameter(ParameterSetName="mobilephone")][string]$MobilePhone,
        [switch]$IncludeComputerInfo,
        [switch]$ExpandComputerInfo
    )

    begin {
        try {
            $null = Get-ADUser -Filter "Name -eq 0"
        }
        catch [System.Management.Automation.CommandNotFoundException] {
            Write-Error -ErrorAction Stop -Message @"
Please install RSAT before running this function. You can install RSAT by following this guide:
https://github.com/NorskNoobing/NN.MrfkCommands#prerequisites
"@

        }
    }

    process {
        switch ($PsCmdlet.ParameterSetName) {
            "username" {
                $filter = "SamAccountName -like `"$Username`""
            }
            "displayname" {
                $filter = "DisplayName -like `"$DisplayName`""
            }
            "mobilephone" {
                $filter = "MobilePhone -like `"$MobilePhone`""
            }
        }

        #Get userinfo of the ADusers
        $ADUser = Get-ADUser -filter $filter -Properties MobilePhone,DisplayName | Select-Object @(
            "DisplayName","Name","SamAccountName","MobilePhone",
            "UserPrincipalName","Enabled","DistinguishedName"
        )

        #Pick an ADUser if we get multiple hits on the search query
        if ($ADUser -is [array]) {
            $splat = @{
                "Title" = "Found multiple hits on the input. Please select an user."
                "OutputMode" = "Single"
            }
            $ADUser = $ADUser | Out-GridView @splat
        }

        if (!$ADUser) {
            Write-Error -ErrorAction "Stop" -Message "Please select a user before continuing."
        }

        if ($IncludeComputerInfo) {
            $ComputerExportArr = Get-MrfkComputerInfo -Username $ADUser.SamAccountName
            if (!$ExpandComputerInfo) {
                $ADUser | Add-Member -MemberType NoteProperty -Name "Computers" -Value $ComputerExportArr
            }
        }

        #Post output
        $ADUser
        if ($ExpandComputerInfo) {
            $ComputerExportArr
        }
    }
}
#EndRegion './Public/Get-MrfkUserInfo.ps1' 69
#Region './Public/New-MrfkAdmCreds.ps1' 0
function New-MrfkAdmCreds {
    param (
        [string]$admCredsPath = "$env:USERPROFILE\.creds\MRFK\adm_creds.xml"
    )

    #Create parent folders for the file
    $admCredsDir = $admCredsPath.Substring(0, $admCredsPath.lastIndexOf('\'))
    if (!(Test-Path $admCredsDir)) {
        $null = New-Item -ItemType Directory $admCredsDir
    }
    
    #Create adm_creds file
    Get-Credential -Message "Enter your mrfk admin credentials" | Export-Clixml $admCredsPath
}
#EndRegion './Public/New-MrfkAdmCreds.ps1' 15