65 lines
2.0 KiB
PowerShell
65 lines
2.0 KiB
PowerShell
Function Update-AliasesInPSD1 {
|
|
<#
|
|
.SYNOPSIS
|
|
Concatenates all the aliases from Get-Aliases for insertion into the PSD1 file referenced.
|
|
|
|
.EXAMPLE
|
|
Update-AliasesInPSD1 .\Alkami.PowerShell.IIS\Public .\Alkami.PowerShell.IIS\Alkami.PowerShell.IIS.psd1
|
|
|
|
.PARAMETER FolderPath
|
|
The name of the folder to examine all files under.
|
|
|
|
.PARAMETER Psd1FilePath
|
|
The file to be rewritten. (This is called out in case more than one .psd1 file exists in a folder.)
|
|
#>
|
|
[CmdletBinding()]
|
|
Param (
|
|
[String]$FolderPath,
|
|
[String]$Psd1FilePath
|
|
)
|
|
process {
|
|
$aliasNames = @(Get-Aliases $FolderPath) | Sort-Object
|
|
|
|
if ($aliasNames.Count -eq 0) {
|
|
return
|
|
}
|
|
|
|
$toBeJoinedAliases = @()
|
|
|
|
foreach($name in $aliasNames) {
|
|
$toBeJoinedAliases += "'$name'"
|
|
}
|
|
|
|
$joinedAliases = $toBeJoinedAliases -Join ','
|
|
|
|
$contents = (Get-Content $Psd1FilePath)
|
|
$newContents = @()
|
|
|
|
$foundAliasesLine = $false
|
|
foreach($line in $contents) {
|
|
if ($line.Trim().StartsWith("AliasesToExport")) {
|
|
$splits = $line -split '='
|
|
$line = $splits[0].TrimEnd() + " = $joinedAliases"
|
|
$foundAliasesLine = $true
|
|
}
|
|
$newContents += $line
|
|
}
|
|
|
|
if (!$foundAliasesLine) {
|
|
$newContents = @()
|
|
## We couldn't find an existing aliases line in that PSD1, so we need to add one.
|
|
## We are gonna add it after FunctionsToExport
|
|
foreach($line in $contents) {
|
|
$newContents += $line
|
|
if ($line.Trim().StartsWith("FunctionsToExport")) {
|
|
$splits = $line -split '='
|
|
$newLine = $splits[0].TrimEnd().Replace('FunctionsToExport','AliasesToExport') + " = $joinedAliases"
|
|
|
|
$newContents += $newLine
|
|
}
|
|
}
|
|
}
|
|
|
|
Set-Content -Path $Psd1FilePath -Value $newContents
|
|
}
|
|
} |