I'm working on a project for work and I need some help. I'm able to pass one argument, multiple arguments, and all arguments, when the arguments are in order(CapA,CapB,etc.) but my function is useless whenever I try to pass the arguments out of order. Ex.(CapB,CapA) I know how to do it in C , but I'm not sure how to do it in PowerShell. Please see the following code:
function Get-FieldOps
{
<#
.SYNOPSIS
Generates list of technicians for each Field Service Team.
.Description
Generates list of technicians for each Field Service Team.
It is able to generate a list of technicians for one team name, multiple team names, or all team names.
.Parameter Team
Team has to be a string variable or list of string variables.
If value passed to team doesn't match a Field Service Team then it will error out.
.Example
Get-FieldOps -Team CapF
.Example
Get-FieldOps CapF
.NOTES
Created on: 12/22/2021
Created by:
FileName:
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String[]]$Team
)
$OTS_EUC_Field_Ops = @('CapA','CapB', 'CapC','CapD', 'CapE', 'CapF', 'Delivery', 'Network', 'NorthA', 'NorthB', 'NorthC', 'Projects', 'SouthA', 'SouthB', 'SouthC', 'SouthD')
$OTS_Ivanti_Groups = @('_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2A','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2B','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2C','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2D','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2E', '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2F','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-Delivery','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-Network', '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-North-A','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-North-B','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-North-C','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-Projects-1361711097','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-South-A','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-South-B','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-South-C','_DOA-OTS-Ivanti-Team-OTS-EUC-Field-South-D')
for($index=0;$index -lt $OTS_EUC_Field_Ops.Length;$index )
{
if($Team[$index] -like $OTS_EUC_Field_Ops[$index])
{
Write-Host "************************************************"
$OTS_EUC_Field_Ops[$index]
Get-ADGroupMember $OTS_Ivanti_Groups[$index] |Format-Table -Property name,SamAccountName,distinguishedName -AutoSize
}
if($Team -like "ALL")
{
Write-Host "************************************************"
$OTS_EUC_Field_Ops[$index]
Get-ADGroupMember $OTS_Ivanti_Groups[$index] |Format-Table -Property name,SamAccountName,distinguishedName -AutoSize
}
}
}
CodePudding user response:
Use a hashtable (an unordered associative array) instead of 3 arrays - this way you don't need to worry about alignment between the user input and the existing mapping between team names and group names:
# Define mapping
$FieldTeamGroupMapping = @{
'CapA' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2A'
'CapB' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2B'
'CapC' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2C'
'CapD' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2D'
'CapE' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2E'
'CapF' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-2F'
'Delivery' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-Delivery'
'Network' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-Network'
'NorthA' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-North-A'
'NorthB' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-North-B'
'NorthC' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-North-C'
'Projects' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-Projects-1361711097'
'SouthA' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-South-A'
'SouthB' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-South-B'
'SouthC' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-South-C'
'SouthD' = '_DOA-OTS-Ivanti-Team-OTS-EUC-Field-South-D'
}
if($Team -contains 'ALL'){
# If the user specified `ALL`, resolve all team names
$Team = $FieldTeamGroupMapping.Keys
}
# Loop over list of team names and do operations
foreach($teamName in $Team){
if(-not $FieldTeamGroupMapping.ContainsKey($teamName)){
Write-Error "Team name '$teamName' is not recognized"
}
else {
Write-Host "************************************************"
$teamName
Get-ADGroupMember $FieldTeamGroupMapping[$teamName] |Format-Table -Property name,SamAccountName,distinguishedName -AutoSize
}
}
