MAK
MAK

Reputation: 125

Unexpected vs missing parenthesis problem

Despite spending ages on trying to debug this error message, I cannot resolve it and am hoping another pair of eyes can spot where the error is. When running the PowerShell script via Windows PowerShell, I am presented with this error message:

enter image description here

It is complaining about an "unexpected token '}'" on the last line below where there is a closing parenthesis, but as far as I can tell, this shouldn't be an error as it is required to close the opening parenthesis on the first line:

foreach ($auditlogitem in $aggregateResults) {
    $datum = New-Object –TypeName PSObject
    $d=convertfrom-json $auditlogitem.AuditData
    if ($datum -AND $d ) {
        $datum | Add-Member -MemberType NoteProperty -Name Id -Value $d.Id
        $datum | Add-Member –MemberType NoteProperty –Name CreationDateTime –Value $auditlogitem.CreationDate
        $datum | Add-Member –MemberType NoteProperty –Name CreationTimeUTC –Value $d.CreationTime
        $datum | Add-Member –MemberType NoteProperty –Name RecordType –Value $d.RecordType
        $datum | Add-Member –MemberType NoteProperty –Name Operation –Value $d.Operation
        $datum | Add-Member –MemberType NoteProperty –Name OrganizationId –Value $d.OrganizationId
        $datum | Add-Member –MemberType NoteProperty –Name UserType –Value $d.UserType
        $datum | Add-Member –MemberType NoteProperty –Name UserKey –Value $d.UserKey
        $datum | Add-Member –MemberType NoteProperty –Name Workload –Value $d.Workload
        $datum | Add-Member –MemberType NoteProperty –Name UserId –Value $d.UserId
        $datum | Add-Member –MemberType NoteProperty –Name ClientIP –Value $d.ClientIP
        $datum | Add-Member –MemberType NoteProperty –Name UserAgent –Value $d.UserAgent
        $datum | Add-Member –MemberType NoteProperty –Name Activity –Value $d.Activity
        $datum | Add-Member –MemberType NoteProperty –Name ItemName –Value $d.ItemName
        $datum | Add-Member –MemberType NoteProperty –Name WorkSpaceName –Value $d.WorkSpaceName
        $datum | Add-Member –MemberType NoteProperty –Name DashboardName –Value $d.DashboardName
        $datum | Add-Member –MemberType NoteProperty –Name DatasetName –Value $d.DatasetName
        $datum | Add-Member –MemberType NoteProperty –Name ReportName –Value $d.ReportName
        $datum | Add-Member –MemberType NoteProperty –Name WorkspaceId –Value $d.WorkspaceId
        $datum | Add-Member –MemberType NoteProperty –Name ObjectId –Value $d.ObjectId
        $datum | Add-Member –MemberType NoteProperty –Name DashboardId –Value $d.DashboardId
        $datum | Add-Member –MemberType NoteProperty –Name DatasetId –Value $d.DatasetId
        $datum | Add-Member –MemberType NoteProperty –Name ReportId –Value $d.ReportId
        $datum | Add-Member –MemberType NoteProperty –Name OrgAppPermission –Value $d.OrgAppPermission
        #option to include the below JSON column however for large amounts of data it may be difficult for PBI to parse
        #$datum | Add-Member –MemberType NoteProperty –Name Datasets –Value (ConvertTo-Json $d.Datasets)
        
        #below is a simple PowerShell statement to grab one of the entries and place in the DatasetName if any exist
        foreach ($dataset in $d.datasets) {
            $datum.DatasetName = $dataset.DatasetName
            $datum.DatasetId = $dataset.DatasetId
        }
        $data+=$datum
    }
}

If I remove the closing/last parenthesis, it complains of a missing parenthesis but if I don't remove it, it complains of an unexpected parenthesis!

If I remove this entire code block from the program, then it executes fine so I'm fairly confident that the problem is within the above block of code.

Full program:

#Audit Logs 

Set-ExecutionPolicy RemoteSigned

#Better for scheduled jobs
$User = "[email protected]"
$PWord = ConvertTo-SecureString -String "e2asxsxsx4DWR" -AsPlainText -Force
$UserCredential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord

#This will prompt the user for credential (optional)
#$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

$startDate=(get-date).AddDays(-5)
$endDate=(get-date)
$scriptStart=(get-date)

$sessionName = (get-date -Format 'u')+'pbiauditlog'
# Reset user audit accumulator
$aggregateResults = @()
$i = 0 # Loop counter
Do { 
    $currentResults = Search-UnifiedAuditLog -StartDate $startDate -EndDate $enddate -SessionId $sessionName -SessionCommand ReturnLargeSet -ResultSize 1000 -RecordType PowerBIAudit
    if ($currentResults.Count -gt 0) {
        Write-Host ("  Finished {3} search #{1}, {2} records: {0} min" -f [math]::Round((New-TimeSpan -Start $scriptStart).TotalMinutes,4), $i, $currentResults.Count, $user.UserPrincipalName )
        # Accumulate the data
        $aggregateResults += $currentResults
        # No need to do another query if the # recs returned <1k - should save around 5-10 sec per user
        if ($currentResults.Count -lt 1000) {
            $currentResults = @()
        } else {
            $i++
        }
    }
} Until ($currentResults.Count -eq 0) # --- End of Session Search Loop --- #

$data=@()

foreach ($auditlogitem in $aggregateResults) {
    $datum = New-Object –TypeName PSObject
    $d=convertfrom-json $auditlogitem.AuditData
    if ($datum -AND $d ) {
        $datum | Add-Member -MemberType NoteProperty -Name Id -Value $d.Id
        $datum | Add-Member –MemberType NoteProperty –Name CreationDateTime –Value $auditlogitem.CreationDate
        $datum | Add-Member –MemberType NoteProperty –Name CreationTimeUTC –Value $d.CreationTime
        $datum | Add-Member –MemberType NoteProperty –Name RecordType –Value $d.RecordType
        $datum | Add-Member –MemberType NoteProperty –Name Operation –Value $d.Operation
        $datum | Add-Member –MemberType NoteProperty –Name OrganizationId –Value $d.OrganizationId
        $datum | Add-Member –MemberType NoteProperty –Name UserType –Value $d.UserType
        $datum | Add-Member –MemberType NoteProperty –Name UserKey –Value $d.UserKey
        $datum | Add-Member –MemberType NoteProperty –Name Workload –Value $d.Workload
        $datum | Add-Member –MemberType NoteProperty –Name UserId –Value $d.UserId
        $datum | Add-Member –MemberType NoteProperty –Name ClientIP –Value $d.ClientIP
        $datum | Add-Member –MemberType NoteProperty –Name UserAgent –Value $d.UserAgent
        $datum | Add-Member –MemberType NoteProperty –Name Activity –Value $d.Activity
        $datum | Add-Member –MemberType NoteProperty –Name ItemName –Value $d.ItemName
        $datum | Add-Member –MemberType NoteProperty –Name WorkSpaceName –Value $d.WorkSpaceName
        $datum | Add-Member –MemberType NoteProperty –Name DashboardName –Value $d.DashboardName
        $datum | Add-Member –MemberType NoteProperty –Name DatasetName –Value $d.DatasetName
        $datum | Add-Member –MemberType NoteProperty –Name ReportName –Value $d.ReportName
        $datum | Add-Member –MemberType NoteProperty –Name WorkspaceId –Value $d.WorkspaceId
        $datum | Add-Member –MemberType NoteProperty –Name ObjectId –Value $d.ObjectId
        $datum | Add-Member –MemberType NoteProperty –Name DashboardId –Value $d.DashboardId
        $datum | Add-Member –MemberType NoteProperty –Name DatasetId –Value $d.DatasetId
        $datum | Add-Member –MemberType NoteProperty –Name ReportId –Value $d.ReportId
        $datum | Add-Member –MemberType NoteProperty –Name OrgAppPermission –Value $d.OrgAppPermission
        #option to include the below JSON column however for large amounts of data it may be difficult for PBI to parse
        #$datum | Add-Member –MemberType NoteProperty –Name Datasets –Value (ConvertTo-Json $d.Datasets)
        
        #below is a simple PowerShell statement to grab one of the entries and place in the DatasetName if any exist
        foreach ($dataset in $d.datasets) {
            $datum.DatasetName = $dataset.DatasetName
            $datum.DatasetId = $dataset.DatasetId
        }
        $data+=$datum
    }
}

$datestring = $startDate.ToString("yyyyMMdd")
$fileName = ($datestring + ".csv")
Write-Host ("Writing to file {0}" -f $fileName) 

if ($data) {
    $data | Export-csv $fileName
}

#File transfer to Azure storage account 
Connect-AzAccount -Credential $UserCredential
Get-AzVM -ResourceGroupName "Audit" -status | Export-Csv "$Env:temp/AuditLogs.csv"
$Context = New-AzStorageContext -StorageAccountName "storageaccountname" -StorageAccountKey "FccC+Ev3m+kjnaslkdcjnlaksjcndlkasjdn5XErWQ1sgsmPxhjpn3M5ew=="
Set-AzStorageBlobContent -Force -Context $Context -Container "auditlogs" -File $filename -Blob $filename 

#Close PowerShell session
Remove-PSSession -Id $Session.Id

Upvotes: 0

Views: 201

Answers (1)

MAK
MAK

Reputation: 125

It's the hypen in -TypeName

It's an – (em-dash) in my code, but if I replace it with - (en-dash), it works fine!

Answer posted by mclayton: Unexpected vs missing parenthesis problem

Upvotes: 1

Related Questions