Skip to content

Commit

Permalink
Merge pull request #9 from LarryWisherMan/feature/ModuleUpdates
Browse files Browse the repository at this point in the history
Feature/module updates
  • Loading branch information
LarryWisherMan authored Oct 1, 2024
2 parents 71a8f9b + 9586c56 commit 4cedd0b
Show file tree
Hide file tree
Showing 88 changed files with 6,895 additions and 2,751 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"powershell.codeFormatting.whitespaceBeforeOpenParen": true,
"powershell.codeFormatting.whitespaceAroundOperator": true,
"powershell.codeFormatting.whitespaceAfterSeparator": true,
"powershell.codeFormatting.ignoreOneLineBlock": false,
"powershell.codeFormatting.ignoreOneLineBlock": true,
"powershell.codeFormatting.pipelineIndentationStyle": "IncreaseIndentationAfterEveryPipeline",
"powershell.codeFormatting.preset": "Custom",
"powershell.codeFormatting.alignPropertyValuePairs": true,
Expand Down
75 changes: 71 additions & 4 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@
"label": "test",
"type": "shell",
"command": "&${cwd}/build.ps1",
"args": ["-AutoRestore","-Tasks","test"],
"args": [
"-AutoRestore",
"-Tasks",
"test"
],
"presentation": {
"echo": true,
"reveal": "always",
Expand Down Expand Up @@ -121,7 +125,6 @@
}
]
},

{
"label": "CreateTestScript",
"type": "shell",
Expand All @@ -144,8 +147,72 @@
"panel": "shared"
},
"problemMatcher": []
},
{
"label": "Combine PowerShell Functions from Folder into One File",
"type": "shell",
"command": "powershell",
"args": [
"-ExecutionPolicy",
"Bypass",
"-File",
"${workspaceFolder}/_WorkSpaceTasks/Combine-PowerShellFunctions.ps1",
"-FolderPath",
"${input:folderPath}", // Prompt for folder path
"-ExportTo",
"${input:destinationFile}" // Prompt for the single export file
],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Separate PowerShell Functions into Multiple Files",
"type": "shell",
"command": "powershell",
"args": [
"-ExecutionPolicy",
"Bypass",
"-File",
"${workspaceFolder}/_WorkSpaceTasks/Separate-PowerShellFunctions.ps1", // Correct path to the script
"-FunctionFile",
"${file}", // The selected file in VSCode
"-ExportTo",
"${input:destinationFolder}" // Prompt for the export folder
],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
],
"inputs": [
{
"id": "functionFiles",
"type": "promptString",
"description": "Enter the paths to the PowerShell files (comma-separated) to combine functions",
"default": "${workspaceFolder}/path_to_first_file.ps1,${workspaceFolder}/path_to_second_file.ps1"
},
{
"id": "destinationFile",
"type": "promptString",
"description": "Enter the destination file path for combining all functions",
"default": "${workspaceFolder}/combined_functions.ps1"
},
{
"id": "destinationFolder",
"type": "promptString",
"description": "Enter the destination folder path for exporting individual function files",
"default": "${workspaceFolder}/exported_functions"
},
{
"id": "folderPath",
"type": "promptString",
"description": "Enter the folder path containing the PowerShell files",
"default": "${workspaceFolder}/ps_functions"
}


]
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Removed bug from `Process-RegistryProfiles` regarding populating the `FolderName`
variable.
- Refactored model to create a UserProfile object to better handel different removal
scenarios.

### Added

Expand Down
2 changes: 1 addition & 1 deletion RequiredModules.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#'WinRegOps' = '0.3.0'

'WinRegOps' = @{
Version = '0.4.0-preview0003'
Version = ' 0.4.0-preview0004'
Parameters = @{
AllowPrerelease = $true
Repository = "PSGallery"
Expand Down
48 changes: 48 additions & 0 deletions _WorkSpaceTasks/Combine-PowerShellFunctions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
param(
[string]$FolderPath, # Accept a folder path
[string]$ExportTo # Export to a single destination file
)

# Ensure the export folder exists if exporting to a directory
$exportFolderPath = Split-Path $ExportTo
if (-not (Test-Path -Path $exportFolderPath))
{
New-Item -Path $exportFolderPath -ItemType Directory
}

# If the export file doesn't exist, create an empty file
if (-not (Test-Path -Path $ExportTo))
{
New-Item -Path $ExportTo -ItemType File
}

$combinedContent = ""

# Get all .ps1 files in the folder
$FunctionFiles = Get-ChildItem -Path $FolderPath -Filter *.ps1

foreach ($FunctionFile in $FunctionFiles)
{
# Read the contents of the function file
$functionsContent = Get-Content -Path $FunctionFile.FullName -Raw

# Use a regular expression to match each function in the file
$functionPattern = 'function\s+([^\s]+)\s*{[^{}]*((?>[^{}]+|(?<open>{)|(?<-open>}))*(?(open)(?!)))\s*}'
$matches = [regex]::Matches($functionsContent, $functionPattern)

foreach ($match in $matches)
{
# Capture the function name and its entire content
$functionName = $match.Groups[1].Value
$functionBody = $match.Value

# Append each function to the combined content
$combinedContent += "`n`n# Function: $functionName from file $($FunctionFile.FullName)`n"
$combinedContent += $functionBody
}
}

# Export all functions to the single export file
Set-Content -Path $ExportTo -Value $combinedContent -Force # Force overwrites existing file

Write-Host "Exported all functions from provided folder to $ExportTo"
39 changes: 39 additions & 0 deletions _WorkSpaceTasks/Export-Functon.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
param(
[string]$FunctionFile,
[string]$ExportTo
)

# Ensure the export folder exists if exporting to a directory
if (-not (Test-Path -Path $ExportTo))
{
$exportFolderPath = Split-Path $ExportTo
if (-not (Test-Path -Path $exportFolderPath))
{
New-Item -Path $exportFolderPath -ItemType Directory
}
}

# Read the contents of the function file
$functionsContent = Get-Content -Path $FunctionFile -Raw

# Use a regular expression to match each function in the file
$functionPattern = 'function\s+([^\s]+)\s*{[^{}]*((?>[^{}]+|(?<open>{)|(?<-open>}))*(?(open)(?!)))\s*}'
$matches = [regex]::Matches($functionsContent, $functionPattern)

$combinedContent = ""

foreach ($match in $matches)
{
# Capture the function name and its entire content
$functionName = $match.Groups[1].Value
$functionBody = $match.Value

# Append each function to the combined content
$combinedContent += "`n`n# Function: $functionName`n"
$combinedContent += $functionBody
}

# Export all functions to the single export file
Set-Content -Path $ExportTo -Value $combinedContent

Write-Host "Exported all functions to $ExportTo"
39 changes: 39 additions & 0 deletions _WorkSpaceTasks/Separate-PowerShellFunctions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
param(
[string]$FunctionFile, # The input file containing multiple functions
[string]$ExportTo # The export folder for individual function files
)

# Ensure the export folder exists
if (-not (Test-Path -Path $ExportTo))
{
New-Item -Path $ExportTo -ItemType Directory -Force
}

# Read the contents of the function file
$functionsContent = Get-Content -Path $FunctionFile -Raw

# Use a regular expression to match each function in the file
$functionPattern = 'function\s+([^\s]+)\s*{[^{}]*((?>[^{}]+|(?<open>{)|(?<-open>}))*(?(open)(?!)))\s*}'
$matches = [regex]::Matches($functionsContent, $functionPattern)

foreach ($match in $matches)
{
# Capture the function name and its entire content
$functionName = $match.Groups[1].Value
$functionBody = $match.Value

# Define the export file path for each function
$exportFilePath = Join-Path -Path $ExportTo -ChildPath "$functionName.ps1"

# Ensure the directory exists for the file path (especially if nested directories are involved)
$exportFolder = Split-Path -Path $exportFilePath -Parent
if (-not (Test-Path -Path $exportFolder))
{
New-Item -Path $exportFolder -ItemType Directory -Force
}

# Export the function to its own file
Set-Content -Path $exportFilePath -Value $functionBody

Write-Host "Exported $functionName to $exportFilePath"
}
3 changes: 3 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
BuiltModuleSubdirectory: module
CopyPaths:
- en-US
- Data
- Formats
- Types
# - DSCResources
# - Modules
Encoding: UTF8
Expand Down
124 changes: 124 additions & 0 deletions source/Classes/RegUserProfile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
class RegUserProfile {
[string]$SID
[string]$ProfilePath
[bool]$IsOrphaned
[string]$OrphanReason
[string]$ComputerName
[bool]$IsSpecial
[bool]$IsLoaded
[string]$UserName
[string]$Domain
[datetime]$LastLogonDate
[long]$ProfileSize
[string]$ProfileType
[datetime]$CreatedDate
[string]$ProfileStatus
[bool]$IsTemporary
[bool]$IsCorrupted
[string[]]$SecurityGroups
[string]$HomeDirectory
[bool]$IsEncrypted
[string]$ProfileState
[string]$LastUsedApp
[bool]$HasBackup
[bool]$IsRoaming
[datetime]$LastModifiedDate
[bool]$IsAdminProfile

# New properties
[bool]$HasUserFolder # Indicates if the user's folder exists
[datetime]$Created # The DateTime when the object was instantiated

# Constructor
RegUserProfile(
[string]$SID,
[string]$ProfilePath,
[bool]$IsOrphaned,
[string]$OrphanReason = $null,
[string]$ComputerName,
[bool]$IsSpecial,
[bool]$IsLoaded,
[string]$UserName,
[string]$Domain,
[datetime]$LastLogonDate,
[long]$ProfileSize,
[string]$ProfileType,
[datetime]$CreatedDate,
[string]$ProfileStatus,
[bool]$IsTemporary,
[bool]$IsCorrupted,
[string[]]$SecurityGroups,
[string]$HomeDirectory,
[bool]$IsEncrypted,
[string]$ProfileState,
[string]$LastUsedApp,
[bool]$HasBackup,
[bool]$IsRoaming,
[datetime]$LastModifiedDate,
[bool]$IsAdminProfile,
[bool]$HasUserFolder # New property
) {
# Initialize all properties
$this.SID = $SID
$this.ProfilePath = $ProfilePath
$this.IsOrphaned = $IsOrphaned
$this.OrphanReason = $OrphanReason
$this.ComputerName = $ComputerName
$this.IsSpecial = $IsSpecial
$this.IsLoaded = $IsLoaded
$this.UserName = $UserName
$this.Domain = $Domain
$this.LastLogonDate = $LastLogonDate
$this.ProfileSize = $ProfileSize
$this.ProfileType = $ProfileType
$this.CreatedDate = $CreatedDate
$this.ProfileStatus = $ProfileStatus
$this.IsTemporary = $IsTemporary
$this.IsCorrupted = $IsCorrupted
$this.SecurityGroups = $SecurityGroups
$this.HomeDirectory = $HomeDirectory
$this.IsEncrypted = $IsEncrypted
$this.ProfileState = $ProfileState
$this.LastUsedApp = $LastUsedApp
$this.HasBackup = $HasBackup
$this.IsRoaming = $IsRoaming
$this.LastModifiedDate = $LastModifiedDate
$this.IsAdminProfile = $IsAdminProfile
$this.HasUserFolder = $HasUserFolder
$this.Created = [DateTime]::Now # Automatically set when object is created
}

# JSON Serialization Example
[string] ToJson() {
$properties = @{
SID = $this.SID
ProfilePath = $this.ProfilePath
IsOrphaned = $this.IsOrphaned
OrphanReason = $this.OrphanReason
ComputerName = $this.ComputerName
IsSpecial = $this.IsSpecial
IsLoaded = $this.IsLoaded
UserName = $this.UserName
Domain = $this.Domain
LastLogonDate = $this.LastLogonDate
ProfileSize = $this.ProfileSize
ProfileType = $this.ProfileType
CreatedDate = $this.CreatedDate
ProfileStatus = $this.ProfileStatus
IsTemporary = $this.IsTemporary
IsCorrupted = $this.IsCorrupted
SecurityGroups = $this.SecurityGroups
HomeDirectory = $this.HomeDirectory
IsEncrypted = $this.IsEncrypted
ProfileState = $this.ProfileState
LastUsedApp = $this.LastUsedApp
HasBackup = $this.HasBackup
IsRoaming = $this.IsRoaming
LastModifiedDate = $this.LastModifiedDate
IsAdminProfile = $this.IsAdminProfile
HasUserFolder = $this.HasUserFolder
Created = $this.Created # Include the new Created DateTime property
}
return $properties | ConvertTo-Json
}
}
Loading

0 comments on commit 4cedd0b

Please sign in to comment.