WisherTools.Helpers.psm1

#Region './Public/Get-BackupFilePath.ps1' -1

<#
.SYNOPSIS
Generates a unique backup file path with a timestamp.

.DESCRIPTION
This function generates a unique backup file path by appending a timestamp to a user-defined or default file prefix. It is typically used to ensure that each backup file created has a unique name.

.PARAMETER BackupDirectory
The directory where the backup file will be saved. Defaults to "C:\LHStuff\UserProfileTools\RegProfBackup".

.PARAMETER Prefix
The prefix for the backup file name. Defaults to "ProfileListBackup".

.EXAMPLE
Get-BackupFilePath -BackupDirectory "C:\Backups" -Prefix "RegistryBackup"

Generates a backup file path in "C:\Backups" with the prefix "RegistryBackup".

.OUTPUTS
System.String

.NOTES
#>


function Get-BackupFilePath {
    param (
        [string]$BackupDirectory = "C:\LHStuff\UserProfileTools\RegProfBackup",
        [string]$Prefix = "ProfileListBackup"
    )

    # Generate a unique backup file name with timestamp
    $dateTimeStamp = (Get-Date -Format "yyyyMMdd_HHmmss")
    $backupFileName = "$Prefix`_$dateTimeStamp.reg"
    $backupPath = Join-Path -Path $BackupDirectory -ChildPath $backupFileName

    return $backupPath
}
#EndRegion './Public/Get-BackupFilePath.ps1' 38
#Region './Public/Get-DirectoryPath.ps1' -1

<#
.SYNOPSIS
Converts a base path to either a local or UNC path format.

.DESCRIPTION
This function converts a base path to a local path if the target is on the local computer, or to a UNC format if the target is on a remote computer.

.PARAMETER BasePath
The base directory path that needs to be converted.

.PARAMETER ComputerName
The name of the computer where the directory is located.

.PARAMETER IsLocal
Boolean value indicating if the target is local. If true, the function returns the local path format; if false, it returns a UNC path.

.EXAMPLE
Get-DirectoryPath -BasePath "C:\Files" -ComputerName "RemotePC" -IsLocal $false

Converts the local path "C:\Files" to a UNC path for the remote computer.

.OUTPUTS
System.String

.NOTES
#>


function Get-DirectoryPath {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$BasePath,

        [Parameter(Mandatory = $true)]
        [string]$ComputerName,

        [Parameter(Mandatory = $true)]
        [bool]$IsLocal  # Determines whether to return local or UNC format
    )

    if ($IsLocal) {
        # Convert UNC to local format
        $localPath = $BasePath -replace '(?:.+)\\([a-zA-Z])\$\\', '$1:\'
        return $localPath
    }
    else {
        # Convert local path to UNC format
        $uncPath = $BasePath -replace '^([a-zA-Z]):\\', "\\$ComputerName\`$1`$\"
        return $uncPath
    }
}
#EndRegion './Public/Get-DirectoryPath.ps1' 52
#Region './Public/Get-FunctionScriptBlock.ps1' -1

<#
.SYNOPSIS
Retrieves the script block of a specified PowerShell function.

.DESCRIPTION
This function dynamically retrieves the script block definition of a specified PowerShell function. It can be used to examine the contents of an existing function.

.PARAMETER FunctionName
The name of the function whose script block is to be retrieved.

.EXAMPLE
Get-FunctionScriptBlock -FunctionName 'Get-Process'

Retrieves the script block of the 'Get-Process' function.

.OUTPUTS
System.String

.NOTES
#>


function Get-FunctionScriptBlock {
    param (
        [string]$FunctionName
    )

    try {
        # Get the current definition (body) of the specified function dynamically
        $functionBody = (Get-Command -Name $FunctionName).Definition

        if (-not $functionBody) {
            throw "Function '$FunctionName' does not exist."
        }

        # Create the full function definition as a script block
        $fullFunctionScript = @"
function $FunctionName {
    $functionBody
}
"@


        return $fullFunctionScript
    } catch {
        Write-Error "Failed to retrieve the function '$FunctionName'. Error: $_"
        return $null
    }
}
#EndRegion './Public/Get-FunctionScriptBlock.ps1' 48
#Region './Public/Test-ComputerPing.ps1' -1

<#
.SYNOPSIS
Pings a computer to check if it is online.

.DESCRIPTION
This function sends a ping request to the specified computer and returns a boolean value indicating whether the computer is reachable. A configurable timeout can be specified for the ping.

.PARAMETER ComputerName
The name of the computer to be pinged.

.PARAMETER Timeout
The timeout value for the ping request in milliseconds. The default value is 2000 ms (2 seconds).

.EXAMPLE
Test-ComputerPing -ComputerName "RemotePC" -Timeout 3000

Pings the computer "RemotePC" with a timeout of 3 seconds to check if it's online.

.OUTPUTS
System.Boolean

.NOTES
#>


function Test-ComputerPing {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$ComputerName,

        [Parameter(Mandatory = $false)]
        [int]$Timeout = 2000  # Default timeout is 4000 ms (4 seconds)
    )

    try {
        # Build the filter query to include the address and timeout
        $query = "Address='$ComputerName' AND Timeout=$Timeout"

        # Use Get-CimInstance to ping the computer with a timeout
        $pingResult = Get-CimInstance -ClassName Win32_PingStatus `
            -Filter $query -ErrorAction Stop

        # Check if the ping status is 0 (successful ping)
        if ($pingResult.StatusCode -eq 0) {
            Write-Verbose "Computer '$ComputerName' is online."
            return $true
        } else {
            Write-Verbose "Computer '$ComputerName' is offline or unreachable (StatusCode: $($pingResult.StatusCode))."
            return $false
        }
    } catch {
        Write-Verbose "Failed to ping computer '$ComputerName'. Error: $_"
        return $false
    }
}
#EndRegion './Public/Test-ComputerPing.ps1' 56
#Region './Public/Test-DirectoryExists.ps1' -1

<#
.SYNOPSIS
Ensures that a specified directory exists, and creates it if necessary.

.DESCRIPTION
This function checks whether a directory exists. If the directory does not exist, it creates the directory at the specified path.

.PARAMETER Directory
The path of the directory to check or create.

.EXAMPLE
Test-DirectoryExists -Directory "C:\Backups"

Checks if the directory "C:\Backups" exists. If not, the function creates it.

.OUTPUTS
None

.NOTES
#>


function Test-DirectoryExists {
    param (
        [string]$Directory
    )

    if (-not (Test-Path $Directory)) {
        New-Item -Path $Directory -ItemType Directory -Force | Out-Null
    }
}
#EndRegion './Public/Test-DirectoryExists.ps1' 31