Src/Public/New-AsBuiltConfig.ps1
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
function New-AsBuiltConfig { <# .SYNOPSIS Creates As Built Report configuration files. .DESCRIPTION New-AsBuiltConfig starts a menu-driven procedure in the powershell console and asks the user a series of questions Answers to these questions are optionally saved in a JSON configuration file which can then be referenced using the -AsBuiltConfigFilePath parameter using New-AsBuiltReport, to save having to answer these questions again and also to allow the automation of New-AsBuiltReport. New-AsBuiltConfig will automatically be called by New-AsBuiltReport if the -AsBuiltConfigFilePath parameter is not specified If a user wants to generate a new As Built Report configuration without running a new report, this cmdlet is exported in the AsBuiltReport powershell module and can be called as a standalone cmdlet. .LINK https://github.com/AsBuiltReport/AsBuiltReport.Core .LINK https://www.asbuiltreport.com/user-guide/new-asbuiltconfig/ #> [CmdletBinding()] param() #Run section to prompt user for information about the As Built Report to be exported to JSON format (if saved) $global:Config = @{ } $DirectorySeparatorChar = [System.IO.Path]::DirectorySeparatorChar #region Report configuration Clear-Host Write-Host '---------------------------------------------' -ForegroundColor Cyan Write-Host ' < As Built Report Information > ' -ForegroundColor Cyan Write-Host '---------------------------------------------' -ForegroundColor Cyan $ReportAuthor = Read-Host -Prompt "Enter the name of the Author for this As Built Report [$([System.Environment]::Username)]" if (($ReportAuthor -like $null) -or ($ReportAuthor -eq "")) { $ReportAuthor = $([System.Environment]::Username) } $Config.Report = @{ 'Author' = $ReportAuthor } #endregion Report configuration #region Company configuration Clear-Host Write-Host '---------------------------------------------' -ForegroundColor Cyan Write-Host ' < Company Information > ' -ForegroundColor Cyan Write-Host '---------------------------------------------' -ForegroundColor Cyan $CompanyInfo = Read-Host -Prompt "Would you like to enter Company information for the As Built Report? (y/n)" while ("y", "n" -notcontains $CompanyInfo) { $CompanyInfo = Read-Host -Prompt "Would you like to enter Company information for the As Built Report? (y/n)" } if ($CompanyInfo -eq 'y') { $CompanyFullName = Read-Host -Prompt "Enter the Full Company Name" $CompanyShortName = Read-Host -Prompt "Enter the Company Short Name" $CompanyContact = Read-Host -Prompt "Enter the Company Contact" $CompanyEmail = Read-Host -Prompt "Enter the Company Email Address" $CompanyPhone = Read-Host -Prompt "Enter the Company Phone" $CompanyAddress = Read-Host -Prompt "Enter the Company Address" } $Config.Company = @{ 'FullName' = $CompanyFullName 'ShortName' = $CompanyShortName 'Contact' = $CompanyContact 'Email' = $CompanyEmail 'Phone' = $CompanyPhone 'Address' = $CompanyAddress } #endregion Company configuration #region Email configuration Clear-Host Write-Host '---------------------------------------------' -ForegroundColor Cyan Write-Host ' < Email Configuration > ' -ForegroundColor Cyan Write-Host '---------------------------------------------' -ForegroundColor Cyan if (-not ($SendEmail)) { $ConfigureMailSettings = Read-Host -Prompt "Would you like to enter SMTP configuration? (y/n)" while ("y", "n" -notcontains $ConfigureMailSettings) { $ConfigureMailSettings = Read-Host -Prompt "Would you like to enter SMTP configuration? (y/n)" } } if (($SendEmail) -or ($ConfigureMailSettings -eq "y")) { $MailServer = Read-Host -Prompt "Enter the mail server FQDN / IP address" while (($MailServer -eq $null) -or ($MailServer -eq "")) { $MailServer = Read-Host -Prompt "Enter the mail server FQDN / IP Address" } if (($MailServer -eq 'smtp.office365.com') -or ($MailServer -eq 'smtp.gmail.com')) { $MailServerPort = Read-Host -Prompt "Enter the mail server port number [587]" if (($MailServerPort -eq $null) -or ($MailServerPort -eq "")) { $MailServerPort = '587' } } else { $MailServerPort = Read-Host -Prompt "Enter the mail server port number [25]" if (($MailServerPort -eq $null) -or ($MailServerPort -eq "")) { $MailServerPort = '25' } } $MailServerUseSSL = Read-Host -Prompt "Use SSL for mail server connection? (true/false)" while ("true", "false" -notcontains $MailServerUseSSL) { $MailServerUseSSL = Read-Host -Prompt "Use SSL for mail server connection? (true/false)" } $MailServerUseSSL = Switch ($MailServerUseSSL) { "true" { $true } "false" { $false } } $MailCredentials = Read-Host -Prompt "Require mail server authentication? (true/false)" while ("true", "false" -notcontains $MailCredentials) { $MailCredentials = Read-Host -Prompt "Require mail server authentication? (true/false)" } $MailCredentials = Switch ($MailCredentials) { "true" { $true } "false" { $false } } $MailFrom = Read-Host -Prompt "Enter the mail sender address" while (($MailFrom -eq $null) -or ($MailFrom -eq "")) { $MailFrom = Read-Host -Prompt "Enter the mail sender address" } $MailRecipients = @() do { $MailTo = Read-Host -Prompt "Enter the mail server recipient address" $MailRecipients += $MailTo $AnotherRecipient = @() while ("y", "n" -notcontains $AnotherRecipient) { $AnotherRecipient = Read-Host -Prompt "Do you want to enter another recipient? (y/n)" } }until($AnotherRecipient -eq "n") $MailBody = Read-Host -Prompt "Enter the email message body content" if (($MailBody -eq $null) -or ($MailBody -eq "")) { $MailBody = "As Built Report attached" } } $Config.Email = @{ 'Server' = $MailServer 'Port' = $MailServerPort 'UseSSL' = $MailServerUseSSL 'Credentials' = $MailCredentials 'From' = $MailFrom 'To' = $MailRecipients 'Body' = $MailBody } #endregion Email Configuration #region Report Configuration Folder if ($Report -and (-not $ReportConfigFilePath)) { Clear-Host Write-Host '---------------------------------------------' -ForegroundColor Cyan Write-Host ' < Report Configuration > ' -ForegroundColor Cyan Write-Host '---------------------------------------------' -ForegroundColor Cyan $ReportConfigFolder = Read-Host -Prompt "Enter the full path of the folder to use for storing report configuration files and custom style scripts [$($Home + $DirectorySeparatorChar)AsBuiltReport]" if (($ReportConfigFolder -like $null) -or ($ReportConfigFolder -eq "")) { $ReportConfigFolder = $Home + $DirectorySeparatorChar + "AsBuiltReport" } #If the folder doesn't exist, create it if (-not (Test-Path -Path $ReportConfigFolder)) { Try { $Folder = New-Item -Path $ReportConfigFolder -ItemType Directory -Force } Catch { Write-Error $_ break } } #Add the path to the folder to the report configuration file $Config.UserFolder = @{ 'Path' = $ReportConfigFolder } # Test to see if the report configuration file exists. If it doesn't exist, generate the report configuration file. # If the report configuration file exists, prompt the user to overwrite the report configuration file. $ReportModule = Get-Module -Name "AsBuiltReport.$Report" -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1 $SourcePath = $($ReportModule.ModuleBase) + $DirectorySeparatorChar + $($ReportModule.Name) + ".json" $DestinationPath = $($ReportConfigFolder) + $DirectorySeparatorChar + $($ReportModule.Name) + ".json" if (-not (Get-ChildItem -Path $DestinationPath)) { Write-Verbose -Message "Copying '$($SourcePath)' to '$($DestinationPath)'." New-AsBuiltReportConfig -Report $Report -FolderPath $ReportConfigFolder } else { try { if (Test-Path -Path $DestinationPath) { $OverwriteReportConfig = Read-Host -Prompt "A report configuration file already exists in the specified folder for $($ReportModule.Name). Would you like to overwrite it? (y/n)" while ("y", "n" -notcontains $OverwriteReportConfig) { $OverwriteReportConfig = Read-Host -Prompt "A report configuration file already exists in the specified folder for $($ReportModule.Name). Would you like to overwrite it? (y/n)" } if ($OverwriteReportConfig -eq 'y') { Try { Write-Verbose -Message "Copying '$($SourcePath)' to '$($DestinationPath)'. Overwriting existing file." New-AsBuiltReportConfig -Report $Report -FolderPath $ReportConfigFolder -Force } Catch { Write-Error $_ Break } } } } catch { Write-Error $_ } } } #endregion Report Configuration Folder #region Save configuration Clear-Host Write-Host '----------------------------------------------' -ForegroundColor Cyan Write-Host ' < As Built Report Configuration > ' -ForegroundColor Cyan Write-Host '----------------------------------------------' -ForegroundColor Cyan $SaveAsBuiltConfig = Read-Host -Prompt "Would you like to save the As Built Report configuration file? (y/n)" while ("y", "n" -notcontains $SaveAsBuiltConfig) { $SaveAsBuiltConfig = Read-Host -Prompt "Would you like to save the As Built Report configuration file? (y/n)" } if ($SaveAsBuiltConfig -eq 'y') { $AsBuiltName = Read-Host -Prompt "Enter a name for the As Built Report configuration file [AsBuiltReport]" if (($AsBuiltName -like $null) -or ($AsBuiltName -eq "")) { $AsBuiltName = "AsBuiltReport" } if ($Config.UserFolder.Path) { $AsBuiltExportPath = Read-Host -Prompt "Enter the path to save the As Built Report configuration file [$($Config.UserFolder.Path)]" if (($AsBuiltExportPath -like $null) -or ($AsBuiltExportPath -eq "")) { $AsBuiltExportPath = $Config.UserFolder.Path } } elseif ($ReportConfigFilePath) { $ReportConfigFolderPath = Split-Path -Path $ReportConfigFilePath $AsBuiltExportPath = Read-Host -Prompt "Enter the path to save the As Built Report configuration file [$ReportConfigFolderPath]" if (($AsBuiltExportPath -like $null) -or ($AsBuiltExportPath -eq "")) { $AsBuiltExportPath = $ReportConfigFolderPath } } else { $AsBuiltExportPath = Read-Host -Prompt "Enter the path to save the As Built Report configuration file [$($Home + $DirectorySeparatorChar)AsBuiltReport]" if (($AsBuiltExportPath -like $null) -or ($AsBuiltExportPath -eq "")) { $AsBuiltExportPath = $Home + $DirectorySeparatorChar + "AsBuiltReport" } } if (-not (Test-Path -Path $AsBuiltExportPath)) { Write-Verbose -Message "Creating As Built Report configuration folder '$AsBuiltExportPath'." Try { $Folder = New-Item -Path $AsBuiltExportPath -ItemType Directory -Force } Catch { Write-Error $_ break } } $Config.UserFolder = @{ 'Path' = $AsBuiltExportPath } Write-Verbose -Message "Saving As Built Report configuration file '$($AsBuiltName).json' to path '$AsBuiltExportPath'." $AsBuiltConfigPath = Join-Path -Path $AsBuiltExportPath -ChildPath "$AsBuiltName.json" $Config | ConvertTo-Json | Out-File $AsBuiltConfigPath } else { Write-Verbose -Message "As Built Report configuration file not saved." } #endregion Save configuration # Print output to screen so that it can be captured to $Global:AsBuiltConfig variable in New-AsBuiltReport $Config # Verbose Output Write-Verbose -Message "Config.Report.Author = $ReportAuthor" Write-Verbose -Message "Config.UserFolder.Path = $ReportConfigFolder" foreach ($x in $Config.Company.Keys) { Write-Verbose -Message "Config.Company.$x = $($Config.Company[$x])" } foreach ($x in $Config.Email.Keys) { Write-Verbose -Message "Config.Email.$x = $($Config.Email[$x])" } }#End New-AsBuiltConfig Function |