If you have GitHub Copilot, you may or may not know about this little tip, but wanted to let you know just in case. It has saved me so much time and it can be applied to a lot of scenarios.
Here is the prompt, “Can you add the export to markdown capability from #file:Test-365ACCompanyName.ps1 to #file:Test-365ACStreetAddress.ps1”
And what this does is it adds the functionality of exporting to markdown to the new file the same I did in CompanyName, but adapts it to fit StreetAddress. Further more, it adds Help for it and creates the parameter for it(with ValidatePattern).
From this:
<#
.SYNOPSIS
Tests whether users have a street address and generates a report.
.DESCRIPTION
The Test-365ACStreetAddress function tests whether users have a street address and generates a report.
It takes a list of users as input and checks if each user has a street address.
The function generates a report that includes the user's display name and whether they have a street address.
The report can be exported to an Excel file or an HTML file.
.PARAMETER Users
Specifies the list of users to test. If not provided, it retrieves all users from the Microsoft Graph API.
.PARAMETER TenantID
The ID of the tenant to connect to. Required if using app-only authentication.
.PARAMETER ClientID
The ID of the client to use for app-only authentication. Required if using app-only authentication.
.PARAMETER CertificateThumbprint
The thumbprint of the certificate to use for app-only authentication. Required if using app-only authentication.
.PARAMETER AccessToken
The access token to use for authentication. Required if using app-only authentication.
.PARAMETER InteractiveLogin
Specifies whether to use interactive login. If this switch is present, interactive login will be used. Otherwise, app-only authentication will be used.
.PARAMETER OutputExcelFilePath
Specifies the file path to export the report as an Excel file. The file must have a .xlsx extension.
.PARAMETER OutputHtmlFilePath
Specifies the file path to export the report as an HTML file. The file must have a .html extension.
.PARAMETER TestedProperty
Specifies the name of the property being tested. Default value is 'Has Street Address'.
.EXAMPLE
Test-365ACStreetAddress -Users $users -OutputExcelFilePath "C:\\Reports\\StreetAddressReport.xlsx"
This example tests the specified list of users for street addresses and exports the report to an Excel file.
.EXAMPLE
Test-365ACStreetAddress -OutputExcelFilePath "C:\\Reports\\StreetAddressReport.xlsx"
This example retrieves all users from the Microsoft Graph API, tests them for street addresses, and exports the report to an Excel file.
#>
Function Test-365ACStreetAddress {
[CmdletBinding()]
param
(
[Parameter(ValueFromPipeline = $true)]
[array]$Users = (Get-MgUser -All -Property DisplayName, StreetAddress | Select-Object DisplayName, StreetAddress),
[Parameter(Mandatory = $false)]
[string]$TenantID,
[Parameter(Mandatory = $false)]
[string]$ClientID,
[Parameter(Mandatory = $false)]
[string]$CertificateThumbprint,
[Parameter(Mandatory = $false)]
[string]$AccessToken,
[Parameter(Mandatory = $false)]
[switch]$InteractiveLogin,
[ValidatePattern('\\.xlsx$')]
[string]$OutputExcelFilePath,
[ValidatePattern('\\.html$')]
[string]$OutputHtmlFilePath,
[string]$TestedProperty = 'Has Street Address'
)
BEGIN {
if ($InteractiveLogin) {
Write-PSFMessage "Using interactive login..." -Level Host
Connect-MgGraph -Scopes "User.Read.All", "AuditLog.read.All" -NoWelcome
}
else {
Write-PSFMessage "Using app-only authentication..." -Level Host
Connect-MgGraph -ClientId $ClientID -TenantId $TenantID -CertificateThumbprint $CertificateThumbprint -Scopes "User.Read.All", "AuditLog.Read.All"
}
$results = @()
}
PROCESS {
foreach ($user in $Users) {
$hasStreetAddress = [bool]($user.StreetAddress)
$result = [PSCustomObject]@{
'User Display Name' = $user.DisplayName
$TestedProperty = $hasStreetAddress
}
$results += $result
}
}
END {
$totalTests = $results.Count
$passedTests = ($results | Where-Object { $_.$TestedProperty }).Count
$failedTests = $totalTests - $passedTests
if ($OutputExcelFilePath) {
Export-365ACResultToExcel -Results $results -OutputExcelFilePath $OutputExcelFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty
}
elseif ($OutputHtmlFilePath) {
Export-365ACResultToHtml -Results $results -OutputHtmlFilePath $OutputHtmlFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty
}
else {
Write-Output $results
}
}
}
To this:
<#
.SYNOPSIS
Tests whether users have a street address and generates a report.
.DESCRIPTION
The Test-365ACStreetAddress function tests whether users have a street address and generates a report.
It takes a list of users as input and checks if each user has a street address.
The function generates a report that includes the user's display name and whether they have a street address.
The report can be exported to an Excel file, an HTML file, or a Markdown file.
.PARAMETER Users
Specifies the list of users to test. If not provided, it retrieves all users from the Microsoft Graph API.
.PARAMETER TenantID
The ID of the tenant to connect to. Required if using app-only authentication.
.PARAMETER ClientID
The ID of the client to use for app-only authentication. Required if using app-only authentication.
.PARAMETER CertificateThumbprint
The thumbprint of the certificate to use for app-only authentication. Required if using app-only authentication.
.PARAMETER AccessToken
The access token to use for authentication. Required if using app-only authentication.
.PARAMETER InteractiveLogin
Specifies whether to use interactive login. If this switch is present, interactive login will be used. Otherwise, app-only authentication will be used.
.PARAMETER OutputExcelFilePath
Specifies the file path to export the report as an Excel file. The file must have a .xlsx extension.
.PARAMETER OutputHtmlFilePath
Specifies the file path to export the report as an HTML file. The file must have a .html extension.
.PARAMETER OutputMarkdownFilePath
Specifies the file path to export the report as a Markdown file. The file must have a .md extension.
.PARAMETER TestedProperty
Specifies the name of the property being tested. Default value is 'Has Street Address'.
.EXAMPLE
Test-365ACStreetAddress -Users $users -OutputExcelFilePath "C:\\Reports\\StreetAddressReport.xlsx"
This example tests the specified list of users for street addresses and exports the report to an Excel file.
.EXAMPLE
Test-365ACStreetAddress -OutputExcelFilePath "C:\\Reports\\StreetAddressReport.xlsx"
This example retrieves all users from the Microsoft Graph API, tests them for street addresses, and exports the report to an Excel file.
#>
Function Test-365ACStreetAddress {
[CmdletBinding()]
param
(
[Parameter(ValueFromPipeline = $true)]
[array]$Users = (Get-MgUser -All -Property DisplayName, StreetAddress | Select-Object DisplayName, StreetAddress),
[Parameter(Mandatory = $false)]
[string]$TenantID,
[Parameter(Mandatory = $false)]
[string]$ClientID,
[Parameter(Mandatory = $false)]
[string]$CertificateThumbprint,
[Parameter(Mandatory = $false)]
[string]$AccessToken,
[Parameter(Mandatory = $false)]
[switch]$InteractiveLogin,
[ValidatePattern('\\.xlsx$')]
[string]$OutputExcelFilePath,
[ValidatePattern('\\.html$')]
[string]$OutputHtmlFilePath,
[ValidatePattern('\\.md$')]
[string]$OutputMarkdownFilePath,
[string]$TestedProperty = 'Has Street Address'
)
BEGIN {
if ($InteractiveLogin) {
Write-PSFMessage "Using interactive login..." -Level Host
Connect-MgGraph -Scopes "User.Read.All", "AuditLog.read.All" -NoWelcome
}
else {
Write-PSFMessage "Using app-only authentication..." -Level Host
Connect-MgGraph -ClientId $ClientID -TenantId $TenantID -CertificateThumbprint $CertificateThumbprint -Scopes "User.Read.All", "AuditLog.Read.All"
}
$results = @()
}
PROCESS {
foreach ($user in $Users) {
$hasStreetAddress = [bool]($user.StreetAddress)
$result = [PSCustomObject]@{
'User Display Name' = $user.DisplayName
$TestedProperty = $hasStreetAddress
}
$results += $result
}
}
END {
$totalTests = $results.Count
$passedTests = ($results | Where-Object { $_.$TestedProperty }).Count
$failedTests = $totalTests - $passedTests
if ($OutputExcelFilePath) {
Export-365ACResultToExcel -Results $results -OutputExcelFilePath $OutputExcelFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty
Write-PSFMessage "Excel report saved to $OutputExcelFilePath" -Level Host
}
elseif ($OutputHtmlFilePath) {
Export-365ACResultToHtml -Results $results -OutputHtmlFilePath $OutputHtmlFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty
Write-PSFMessage "HTML report saved to $OutputHtmlFilePath" -Level Host
}
elseif ($OutputMarkdownFilePath) {
Export-365ACResultToMarkdown -Results $results -OutputMarkdownFilePath $OutputMarkdownFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty
Write-PSFMessage "Markdown report saved to $OutputMarkdownFilePath" -Level Host
}
else {
Write-PSFMessage -Level Output -Message ($results | Out-String)
}
}
}
And here is the source Test-365ACCompanyName.ps1 code
<#
.SYNOPSIS
Tests if users have a company name property and generates test results.
.DESCRIPTION
The Test-365ACCompanyName function tests if users have a company name property and generates test results. It takes an array of users as input and checks if each user has a company name property. The test results are stored in an array of custom objects, which include the user's display name and the result of the test.
.PARAMETER Users
Specifies the array of users to test. Each user should have a DisplayName and CompanyName property.
.PARAMETER TenantID
The ID of the tenant to connect to. Required if using app-only authentication.
.PARAMETER ClientID
The ID of the client to use for app-only authentication. Required if using app-only authentication.
.PARAMETER CertificateThumbprint
The thumbprint of the certificate to use for app-only authentication. Required if using app-only authentication.
.PARAMETER AccessToken
The access token to use for authentication. Required if using app-only authentication.
.PARAMETER InteractiveLogin
Specifies whether to use interactive login. If this switch is present, interactive login will be used. Otherwise, app-only authentication will be used.
.PARAMETER OutputExcelFilePath
Specifies the path to the output Excel file. If provided, the test results will be exported to an Excel file.
.PARAMETER OutputHtmlFilePath
Specifies the path to the output HTML file. If provided, the test results will be exported to an HTML file.
.PARAMETER OutputMarkdownFilePath
Specifies the path to the output Markdown file. If provided, the test results will be exported to a Markdown file.
.PARAMETER TestedProperty
Specifies the name of the tested property. Default value is 'Has Company Name'.
.INPUTS
An array of users with DisplayName and CompanyName properties.
.OUTPUTS
If OutputExcelFilePath or OutputHtmlFilePath is not provided, the function outputs an array of custom objects with the user's display name and the result of the test.
.EXAMPLE
$users = Get-MgUser -All -Property DisplayName, CompanyName | Select-Object DisplayName, CompanyName
Test-365ACCompanyName -Users $users -OutputExcelFilePath 'C:\\TestResults.xlsx'
This example tests if the users have a company name property and exports the test results to an Excel file.
#>
Function Test-365ACCompanyName {
[CmdletBinding()]
param
(
[Parameter(ValueFromPipeline = $true)]
[array]$Users = (Get-MgUser -All -Property DisplayName, CompanyName | Select-Object DisplayName, CompanyName),
[Parameter(Mandatory = $false)]
[string]$TenantID,
[Parameter(Mandatory = $false)]
[string]$ClientID,
[Parameter(Mandatory = $false)]
[string]$CertificateThumbprint,
[Parameter(Mandatory = $false)]
[string]$AccessToken,
[Parameter(Mandatory = $false)]
[switch]$InteractiveLogin,
[ValidatePattern('\\.xlsx$')]
[string]$OutputExcelFilePath,
[ValidatePattern('\\.html$')]
[string]$OutputHtmlFilePath,
[ValidatePattern('\\.md$')]
[string]$OutputMarkdownFilePath,
[string]$TestedProperty = 'Has Company Name'
)
BEGIN {
if ($InteractiveLogin) {
Write-PSFMessage "Using interactive login..." -Level Host
Connect-MgGraph -Scopes "User.Read.All", "AuditLog.read.All" -NoWelcome
}
else {
Write-PSFMessage "Using app-only authentication..." -Level Host
Connect-MgGraph -ClientId $ClientID -TenantId $TenantID -CertificateThumbprint $CertificateThumbprint -Scopes "User.Read.All", "AuditLog.Read.All"
}
$results = @()
}
PROCESS {
foreach ($user in $Users) {
$hasCompanyName = [bool]($user.CompanyName)
$result = [PSCustomObject]@{
'User Display Name' = $user.DisplayName
$TestedProperty = $hasCompanyName
}
$results += $result
}
}
END {
$totalTests = $results.Count
$passedTests = ($results | Where-Object { $_.$TestedProperty }).Count
$failedTests = $totalTests - $passedTests
if ($OutputExcelFilePath) {
Export-365ACResultToExcel -Results $results -OutputExcelFilePath $OutputExcelFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty
Write-PSFMessage "Excel report saved to $OutputExcelFilePath" -Level Host
}
elseif ($OutputHtmlFilePath) {
Export-365ACResultToHtml -Results $results -OutputHtmlFilePath $OutputHtmlFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty
Write-PSFMessage "HTML report saved to $OutputHtmlFilePath" -Level Host
}
elseif ($OutputMarkdownFilePath) {
Export-365ACResultToMarkdown -Results $results -OutputMarkdownFilePath $OutputMarkdownFilePath -TotalTests $totalTests -PassedTests $passedTests -FailedTests $failedTests -TestedProperty $TestedProperty
Write-PSFMessage "Markdown report saved to $OutputMarkdownFilePath" -Level Host
}
else {
Write-PSFMessage -Level Output -Message ($results | Out-String)
}
}
}
And yes, it did write out the whole function again, I didn’t manipulate it at all, so after reviewing you can literally just copy it in, and not have to worry about forgetting a “(” or “{”. I know it’s not a huge lift, but you can also do “Can you also do this for #file:Test-365ACZipcode.ps1” and it will.
I’ve used this prompt syntax for other functions as well with great results. Have you used anything like this before?
You can see more uses of it at GitHub – 365AutomatedCheck
Leave a Reply