Search code examples
powershellparameterssyntax

Parameter Set Path keeps giving me an error


I have the following parameters at the start of my script:

param(
    [Parameter(Mandatory = $false, HelpMessage = "Path to the Edumate export CSV file")]
    [ValidateScript({ Test-Path $_ -PathType Leaf })]
    [string]$EdumateExport = '..\Create and Edit Teams\1. Edumate Export\Edumate_20230112.csv',

    [Parameter(Mandatory = $false, HelpMessage = "Path to the existing teams CSV file")]
    [ValidateScript({ Test-Path $_ -PathType Leaf })]
    [string]$CurrentTeams = '..\Teams\Create and Edit Teams\2. Creation Records\Existing Teams.csv',

    [Parameter(Mandatory = $false, HelpMessage = "Path to the output location for the updated teams CSV file")]
    [ValidateScript({ Test-Path (Split-Path $_) -PathType Container })]
    [string]$OutputLocation = '..\Teams\Create and Edit Teams\2. Creation Records\Existing Teams.csv'
)

In the first two paths ($EdumateExport and $CurrentTeams), I am getting an error: "The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property."

I am not sure how to fix this. I am especially confused because the third path (for $OutputLocation) is the same as the second, but I do not get an error on that path.

I have tried with absolute paths, but the error is the same. Any assistance would be greatly appreciated.


Edit: the full code (as requested by @mklement0) can be found here:

# Check if already connected
$UserEmail = "J.T@Company.edu.au"
try {
    (Get-CsOnlineUser -Identity $UserEmail).AccountEnabled
    Write-Host "Already logged in to Microsoft Teams."
}
catch {
    Connect-MicrosoftTeams
}

# Set parameters and import files
param(
    [Parameter(Mandatory = $false, HelpMessage = "Path to the Edumate export CSV file")]
    [ValidateScript({ Test-Path $_ -PathType Leaf })]
    [string]$EdumateExport = '..\Create and Edit Teams\1. Edumate Export\Edumate_20230112.csv',

    [Parameter(Mandatory = $false, HelpMessage = "Path to the existing teams CSV file")]
    [ValidateScript({ Test-Path $_ -PathType Leaf })]
    [string]$CurrentTeams = '..\Teams\Create and Edit Teams\2. Creation Records\Existing Teams.csv',

    [Parameter(Mandatory = $false, HelpMessage = "Path to the output location for the updated teams CSV file")]
    [ValidateScript({ Test-Path (Split-Path $_) -PathType Container })]
    [string]$OutputLocation = '..\Teams\Create and Edit Teams\2. Creation Records\Existing Teams.csv'
)

$Teams = Import-Csv -Path $EdumateExport -ErrorAction Stop
$ExistingTeams = Import-Csv -Path $CurrentTeams -ErrorAction Stop

# Duplicate and rename $CurrentTeams for backup
$LastWriteDate = (Get-Item $CurrentTeams).LastWriteTime.ToString("yyyyMMdd")
$NewFileName = "Existing_$LastWriteDate.csv"
$NewFilePath = Join-Path (Split-Path $CurrentTeams) $NewFileName
Copy-Item -Path $CurrentTeams -Destination $NewFilePath

# Create a new variable to store the Team data with an additional column for the Team Id
$TeamsWithIds = $ExistingTeams

# Iterate through each row of the CSV
foreach ($Team in $Teams) {
    # Find existing Team by Email
    $ExistingTeam = $ExistingTeams | Where-Object { $_.EMAIL -eq $Team.EMAIL }

    # Check if the team already exists in #$ExistingTeam
    if ($ExistingTeam) {
        # Assign the existing team's TeamId to the current team
        $TeamId = $ExistingTeam.TeamId

        # Check if the first owner needs to be updated
        if ($ExistingTeam.OWNER1 -ne $Team.OWNER1) {
            # Check if the first owner is currently an owner of the team
            if ( $null -ne (Get-TeamUser -GroupId $TeamId -Role "Owner" | Where-Object { $_.User -eq $ExistingTeam.OWNER1 } )) {
                # Remove the first owner from the team
                Remove-TeamUser -GroupId $TeamId -User $ExistingTeam.OWNER1
            }
            # Check if the new first owner is not an empty string
            if ($Team.OWNER1 -ne "") {
                # Add the new first owner to the team
                Add-TeamUser -GroupId $TeamId -User $Team.OWNER1 -Role "Owner"
            }
        }

        # Check if the second owner needs to be updated
        if ($ExistingTeam.OWNER2 -ne $Team.OWNER2) {
            # Check if the second owner is currently an owner of the team
            if ( $null -ne (Get-TeamUser -GroupId $TeamId -Role "Owner" | Where-Object { $_.User -eq $ExistingTeam.OWNER2 } )) {
                # Remove the second owner from the team
                Remove-TeamUser -GroupId $TeamId -User $ExistingTeam.OWNER2
            }
            # Check if the new second owner is not an empty string
            if ($Team.OWNER2 -ne "") {
                # Add the new second owner to the team
                Add-TeamUser -GroupId $TeamId -User $Team.OWNER2 -Role "Owner"
            }
        }

        # Check if the second owner needs to be updated
        if ($ExistingTeam.OWNER3 -ne $Team.OWNER3) {
            # Check if the second owner is currently an owner of the team
            if ( $null -ne (Get-TeamUser -GroupId $TeamId -Role "Owner" | Where-Object { $_.User -eq $ExistingTeam.OWNER3 } )) {
                # Remove the second owner from the team
                Remove-TeamUser -GroupId $TeamId -User $ExistingTeam.OWNER3
            }
            # Check if the new second owner is not an empty string
            if ($Team.OWNER3 -ne "") {
                # Add the new second owner to the team
                Add-TeamUser -GroupId $TeamId -User $Team.OWNER3 -Role "Owner"
            }
        }

        Write-Host "Existing Teams checked:" $Team.NAME

    }

    else {
        # Create the Team. Add additional team owners if present.
        $TeamId = (New-Team -DisplayName $Team.NAME -Description $Team.DESCRIPTION -MailNickName $Team.EMAIL -Owner $Team.OWNER1 -Template EDU_Class ).GroupId

        if ($null -ne $Team.OWNER2 -and $Team.OWNER2 -ne "") {
            Add-TeamUser -GroupId $TeamId -User $Team.OWNER2 -Role "Owner"
        }

        if ($null -ne $Team.OWNER3 -and $Team.OWNER3 -ne "") {
            Add-TeamUser -GroupId $TeamId -User $Team.OWNER3 -Role "Owner"
        }

        # Creating a new custom object with properties from the current team and the generated TeamId, and adding it to the collection of teams with ids
        $TeamWithId = [PSCustomObject][Ordered]@{'NAME' = $Team.NAME; 'DESCRIPTION' = $Team.DESCRIPTION; 'EMAIL' = $Team.EMAIL; 'OWNER1' = $Team.OWNER1; 'OWNER2' = $Team.OWNER2; 'OWNER3' = $Team.OWNER3; 'RULE' = $Team.RULE; 'TeamId' = $TeamId }
        $TeamsWithIds += $TeamwithId

        Write-Host "New Team created:" $Team.NAME

    }
}

# Export the new variable to a csv file
$TeamsWithIds | Export-Csv $OutputLocation -NoTypeInformation
Write-Host "Teams created and information exported."

Solution

  • The param() block needs to be one of the first elements in a script or block - only #Requires directives, using ... statements, comments and attribute decorators can precede it.

    Change your script as follows:

    # Set parameters and import files
    param(
        [Parameter(Mandatory = $false, HelpMessage = "Path to the Edumate export CSV file")]
        [ValidateScript({ Test-Path $_ -PathType Leaf })]
        [string]$EdumateExport = '..\Create and Edit Teams\1. Edumate Export\Edumate_20230112.csv',
    
        [Parameter(Mandatory = $false, HelpMessage = "Path to the existing teams CSV file")]
        [ValidateScript({ Test-Path $_ -PathType Leaf })]
        [string]$CurrentTeams = '..\Teams\Create and Edit Teams\2. Creation Records\Existing Teams.csv',
    
        [Parameter(Mandatory = $false, HelpMessage = "Path to the output location for the updated teams CSV file")]
        [ValidateScript({ Test-Path (Split-Path $_) -PathType Container })]
        [string]$OutputLocation = '..\Teams\Create and Edit Teams\2. Creation Records\Existing Teams.csv'
    )
    
    # Check if already connected
    $UserEmail = "J.T@Company.edu.au"
    try {
        (Get-CsOnlineUser -Identity $UserEmail).AccountEnabled
        Write-Host "Already logged in to Microsoft Teams."
    }
    catch {
        Connect-MicrosoftTeams
    }
    
    $Teams = Import-Csv -Path $EdumateExport -ErrorAction Stop
    $ExistingTeams = Import-Csv -Path $CurrentTeams -ErrorAction Stop
    
    # Duplicate and rename $CurrentTeams for backup
    $LastWriteDate = (Get-Item $CurrentTeams).LastWriteTime.ToString("yyyyMMdd")
    $NewFileName = "Existing_$LastWriteDate.csv"
    $NewFilePath = Join-Path (Split-Path $CurrentTeams) $NewFileName
    Copy-Item -Path $CurrentTeams -Destination $NewFilePath
    
    # Create a new variable to store the Team data with an additional column for the Team Id
    $TeamsWithIds = $ExistingTeams
    
    # Iterate through each row of the CSV
    foreach ($Team in $Teams) {
        # Find existing Team by Email
        $ExistingTeam = $ExistingTeams | Where-Object { $_.EMAIL -eq $Team.EMAIL }
    
        # Check if the team already exists in #$ExistingTeam
        if ($ExistingTeam) {
            # Assign the existing team's TeamId to the current team
            $TeamId = $ExistingTeam.TeamId
    
            # Check if the first owner needs to be updated
            if ($ExistingTeam.OWNER1 -ne $Team.OWNER1) {
                # Check if the first owner is currently an owner of the team
                if ( $null -ne (Get-TeamUser -GroupId $TeamId -Role "Owner" | Where-Object { $_.User -eq $ExistingTeam.OWNER1 } )) {
                    # Remove the first owner from the team
                    Remove-TeamUser -GroupId $TeamId -User $ExistingTeam.OWNER1
                }
                # Check if the new first owner is not an empty string
                if ($Team.OWNER1 -ne "") {
                    # Add the new first owner to the team
                    Add-TeamUser -GroupId $TeamId -User $Team.OWNER1 -Role "Owner"
                }
            }
    
            # Check if the second owner needs to be updated
            if ($ExistingTeam.OWNER2 -ne $Team.OWNER2) {
                # Check if the second owner is currently an owner of the team
                if ( $null -ne (Get-TeamUser -GroupId $TeamId -Role "Owner" | Where-Object { $_.User -eq $ExistingTeam.OWNER2 } )) {
                    # Remove the second owner from the team
                    Remove-TeamUser -GroupId $TeamId -User $ExistingTeam.OWNER2
                }
                # Check if the new second owner is not an empty string
                if ($Team.OWNER2 -ne "") {
                    # Add the new second owner to the team
                    Add-TeamUser -GroupId $TeamId -User $Team.OWNER2 -Role "Owner"
                }
            }
    
            # Check if the second owner needs to be updated
            if ($ExistingTeam.OWNER3 -ne $Team.OWNER3) {
                # Check if the second owner is currently an owner of the team
                if ( $null -ne (Get-TeamUser -GroupId $TeamId -Role "Owner" | Where-Object { $_.User -eq $ExistingTeam.OWNER3 } )) {
                    # Remove the second owner from the team
                    Remove-TeamUser -GroupId $TeamId -User $ExistingTeam.OWNER3
                }
                # Check if the new second owner is not an empty string
                if ($Team.OWNER3 -ne "") {
                    # Add the new second owner to the team
                    Add-TeamUser -GroupId $TeamId -User $Team.OWNER3 -Role "Owner"
                }
            }
    
            Write-Host "Existing Teams checked:" $Team.NAME
    
        }
    
        else {
            # Create the Team. Add additional team owners if present.
            $TeamId = (New-Team -DisplayName $Team.NAME -Description $Team.DESCRIPTION -MailNickName $Team.EMAIL -Owner $Team.OWNER1 -Template EDU_Class ).GroupId
    
            if ($null -ne $Team.OWNER2 -and $Team.OWNER2 -ne "") {
                Add-TeamUser -GroupId $TeamId -User $Team.OWNER2 -Role "Owner"
            }
    
            if ($null -ne $Team.OWNER3 -and $Team.OWNER3 -ne "") {
                Add-TeamUser -GroupId $TeamId -User $Team.OWNER3 -Role "Owner"
            }
    
            # Creating a new custom object with properties from the current team and the generated TeamId, and adding it to the collection of teams with ids
            $TeamWithId = [PSCustomObject][Ordered]@{'NAME' = $Team.NAME; 'DESCRIPTION' = $Team.DESCRIPTION; 'EMAIL' = $Team.EMAIL; 'OWNER1' = $Team.OWNER1; 'OWNER2' = $Team.OWNER2; 'OWNER3' = $Team.OWNER3; 'RULE' = $Team.RULE; 'TeamId' = $TeamId }
            $TeamsWithIds += $TeamwithId
    
            Write-Host "New Team created:" $Team.NAME
    
        }
    }
    
    # Export the new variable to a csv file
    $TeamsWithIds | Export-Csv $OutputLocation -NoTypeInformation
    Write-Host "Teams created and information exported."