|
1 | | -<# |
2 | | -.SYNOPSIS |
3 | | - This function tests whether users have a company name associated with their account and exports the results to Excel, HTML, or the console. .DESCRIPTION |
4 | | - The Test-365ACCompanyName function checks whether users have a company name associated with their account. It takes an array of users, an optional Excel file path as input. It then iterates through each user, determines if they have a company name, and generates a result object for each user. The function returns the results as an array of objects or exports them to an Excel file or an HTML file. |
5 | | -
|
6 | | -.PARAMETER Users |
7 | | - Specifies an array of users to test. Each user should have a display name and a company name property. |
8 | | -
|
9 | | -.PARAMETER ExcelFilePath |
10 | | - Specifies the file path to export the results to an Excel file. If this parameter is provided, the Export-Excel module must be installed. |
11 | | -
|
12 | | -.PARAMETER HtmlFilePath |
13 | | - Specifies the file path to export the results to an HTML file. |
14 | | -
|
15 | | -.PARAMETER TestedProperty |
16 | | - Specifies the property that is being tested. Default is 'Has Company Name'. |
17 | | -
|
18 | | -.EXAMPLE |
19 | | - Test-365ACCompanyName -Users $users -ExcelFilePath "C:\Results.xlsx" |
20 | | - This example tests the company names of the users in the $users array and exports the results to an Excel file located at "C:\Results.xlsx". |
21 | | -
|
22 | | -.EXAMPLE |
23 | | - Test-365ACCompanyName -Users $users -HtmlFilePath "C:\Results.html" |
24 | | - This example tests the company names of the users in the $users array and exports the results to an HTML file located at "C:\Results.html". |
25 | | -
|
26 | | -.NOTES |
27 | | - - This function requires the ImportExcel module to export results to Excel. If the module is not installed, an error will be displayed. |
28 | | - - The Export-365ACResultToExcel and Export-365ACResultToHtml functions are assumed to be defined elsewhere in the script. |
29 | | -
|
30 | | -.LINK |
31 | | - https://github.com/DevClate/365AutomatedCheck |
32 | | -#> |
33 | 1 | Function Test-365ACCompanyName { |
34 | 2 | [CmdletBinding()] |
35 | 3 | param |
36 | 4 | ( |
37 | 5 | [Parameter(ValueFromPipeline = $true)] |
38 | | - [array]$Users = (get-mguser -all -Property DisplayName, CompanyName | Select-Object displayname, CompanyName), |
| 6 | + [array]$Users = (get-mguser -all -Property DisplayName, CompanyName | Select-Object DisplayName, CompanyName), |
| 7 | + |
| 8 | + [ValidatePattern('\.xlsx$')] |
| 9 | + [string]$ValidationExcelFilePath, |
39 | 10 |
|
40 | 11 | [ValidatePattern('\.xlsx$')] |
41 | | - [string]$ExcelFilePath, |
| 12 | + [string]$OutputExcelFilePath, |
42 | 13 |
|
43 | 14 | [ValidatePattern('\.html$')] |
44 | 15 | [string]$HtmlFilePath, |
45 | 16 |
|
46 | 17 | [string]$TestedProperty = 'Has Company Name' |
47 | 18 | ) |
48 | 19 | BEGIN { |
49 | | - if ($ExcelFilePath -and !(Get-Command Export-Excel -ErrorAction SilentlyContinue)) { |
50 | | - Write-Error "Export-Excel cmdlet not found. Please install the ImportExcel module." |
51 | | - return |
| 20 | + $validCompanyNames = @() |
| 21 | + if ($ValidationExcelFilePath) { |
| 22 | + if (!(Get-Command Import-Excel -ErrorAction SilentlyContinue)) { |
| 23 | + Write-Error "Import-Excel cmdlet not found. Please install the ImportExcel module." |
| 24 | + return@ |
| 25 | + } |
| 26 | + # Import the Excel file to get valid company names |
| 27 | + $validCompanyNames = Import-Excel -Path $ValidationExcelFilePath | Select-Object -ExpandProperty CompanyName -Unique |
52 | 28 | } |
53 | 29 | $results = @() |
54 | 30 | } |
55 | 31 | PROCESS { |
| 32 | + $columnName = $ValidationExcelFilePath ? 'Has Valid Company Name' : 'Has Company Name' |
56 | 33 | foreach ($user in $Users) { |
57 | 34 | $hasCompanyName = [bool]($user.CompanyName) |
| 35 | + if ($ValidationExcelFilePath) { |
| 36 | + $hasCompanyName = $user.CompanyName -in $validCompanyNames |
| 37 | + } |
58 | 38 | $result = [PSCustomObject]@{ |
59 | 39 | 'User Display Name' = $user.DisplayName |
60 | | - 'Has Company Name' = $hasCompanyName |
| 40 | + $columnName = $hasCompanyName |
61 | 41 | } |
62 | 42 | $results += $result |
63 | 43 | } |
64 | 44 | } |
65 | 45 | END { |
66 | 46 | $totalTests = $results.Count |
67 | | - $passedTests = ($results | Where-Object { $_.'Has Company Name' }).Count |
| 47 | + # Update passed and failed tests calculation based on the new logic |
| 48 | + $passedTests = ($results | Where-Object { $_.$columnName }).Count |
68 | 49 | $failedTests = $totalTests - $passedTests |
69 | | - if ($ExcelFilePath) { |
70 | | - Export-365ACResultToExcel -Results $results -ExcelFilePath $ExcelFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty |
| 50 | + if ($OutputExcelFilePath) { |
| 51 | + Export-365ACResultToExcel -Results $results -OutputExcelFilePath $OutputExcelFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $columnName |
71 | 52 | } |
72 | 53 | elseif ($HtmlFilePath) { |
73 | | - Export-365ACResultToHtml -Results $results -HtmlFilePath $HtmlFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty |
| 54 | + Export-365ACResultToHtml -Results $results -HtmlFilePath $HtmlFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $columnName |
74 | 55 | } |
75 | 56 | else { |
76 | 57 | Write-Output $results |
|
0 commit comments