-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonVariablesGenerator.ps1
More file actions
50 lines (40 loc) · 1.97 KB
/
CommonVariablesGenerator.ps1
File metadata and controls
50 lines (40 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# This script also generates a list of $variables from other files, different output than the other script - CommonVariables.ps1
# Define the folder path where your .ps1 files are located
$scriptFolder = "C:\Users\...\PowerShell"
# Define the output file path for the variables
$outputPs1 = "C:\Users\...\PowerShellCommonProfile.ps1"
# Get all .ps1 files in the folder
$ps1Files = Get-ChildItem -Path $scriptFolder -Filter *.ps1
# Initialize an empty array to hold new variable information
$newVariables = @()
# Loop through each .ps1 file
foreach ($file in $ps1Files) {
# Read the contents of the script
$scriptContent = Get-Content -Path $file.FullName
# Use a regular expression to find all variable definitions (lines that start with $)
$variables = $scriptContent | Select-String -Pattern "\$[a-zA-Z_][a-zA-Z0-9_]*" | ForEach-Object {
# Clean up the variable name to remove any unwanted characters like `=` or `,`
$_.Matches.Value -replace "[^a-zA-Z0-9_$]", ""
}
# Create a custom object for each variable to store the filename and variable name
foreach ($var in $variables) {
$newVariables += [pscustomobject]@{
ScriptFile = $file.Name
Variable = $var
}
}
}
# Check if the CommonVariables.ps1 file already exists
if (-not (Test-Path $outputPs1)) {
# Create the file and add a header comment
Add-Content -Path $outputPs1 -Value "# This file contains common variables extracted from other scripts"
}
# Loop through the new variables and append them to the CommonVariables.ps1 file
foreach ($newVar in $newVariables) {
# Check if the variable already exists in the CommonVariables.ps1 file
$existingContent = Get-Content -Path $outputPs1
if (-not ($existingContent -match [regex]::Escape($newVar.Variable))) {
# Append the new variable to the file
Add-Content -Path $outputPs1 -Value "`$($newVar.Variable) = `"$($newVar.Variable)_value`" # from $($newVar.ScriptFile)"
}
}