Dave Platt
Dave Platt

Reputation: 3

Powershell script only works if breakpoint is present

I have a powershell script which reads file and folder info and adds data to an array.

The script runs fine if there is a breakpoint present. The breakpoint can be anywhere in the script and as soon as it is reached and I continue the execution the code finishes without error.

Even if I set the breakpoint to Disabled with Get-PSBreakpoint | Disable-PSBreakpoint the line before the breakpoint the code executes without error.

When the breakpoint is removed the code errors with "Method invocation failed because [System.ManagementAutomation.PSObject] doesn't contain method named op_Addition when adding to the array.

The error occurs here

$report += New-Object -TypeName PSObject -Property $props

and here

$filerec += New-Object -TypeName PSObject -Property $propsfile

This is the function where the error occurs

    function ScanDrive([string]$scanpath)
{
    #Scan the drive
    $files = Get-ChildItem -Path $scanpath -Force -Recurse
    $filecount = $files.Count
    $Counter = 0

    #Set up the result array
    $folderRes = @()
    $filesRes = @()

    ForEach ($file in $files)
    {
        $lblInfo.Text = $file.FullName
        $Form.Refresh()
        $Counter++
        [Int]$Percentage = ($Counter/$filecount)*100
        $pbScan.Value = $Percentage
        Write-Debug "Help"
        if ($file.Attributes -eq 'Directory')
        {
            write-host 'This is a folder' + $file.FullName -ForegroundColor DarkGreen
            try
            {

                $acl = Get-Acl -path $file.PSPath
                Write-Host 'ACL' + $acl.Group.ToString()
                $access = $acl.access
                foreach($ar in $access)
                {
                    $props = [ordered]@{
                        'ID' = ''
                        'FolderName' = $file.Name;
                        'ADGroupUser' = $ar.IdentityReference;
                        'Permissions' = $ar.FileSystemRights.ToString();
                        'ControlType' = $ar.AccessControlType;
                        'IsInherited' = $ar.IsInherited
                        }
                    $report += New-Object -TypeName PSObject -Property $props 

                    $folderRes += $report
                }
                Write-Host "Folder record count : " + $folderRes.Count.ToString()
            }
            catch
            {
                Write-Host '******************************' -ForegroundColor Yellow
                Write-Host 'Error ' + $_.Exception.Message  -ForegroundColor Red
                Write-Host '******************************' -ForegroundColor Yellow
            }
        }
        else
        {
            write-host 'This is a file' + write-host $file.FullName -ForegroundColor DarkGreen

            $iname = $file.Name
            $iparent = $file.PSParentPath
            $isize = $file.Length
            $itype = $file.Extension
            $iowner = get-acl $file.FullName
            $owner = $iowner.Owner
            $icreated = $file.CreationTime
            $ilasmod = $file.LastWriteTime
            $idirectory = $file.Directory
            $ireadonly = $file.IsReadOnly
            $ilastacc = $file.LastAccessTime
            $ifilename = $file.FullName
            $usergroup = $iowner.Group
            $iatts = $file.Attributes

            $propsfile = [ordered]@{
                        'ID' = ''
                        'Name' = $iname;
                        'Parent' = $iparent;
                        'Size' = $isize;
                        'Type' = $itype;
                        'Owner' = $owner;
                        'CreatedDate' = $icreated;
                        'LastModDate' = $ilasmod;
                        'Directory' = $idirectory;
                        'IsReadOnly' = $ireadonly;
                        'LastAccessedDate' = $ilastacc;
                        'FileName' = $ifilename;
                        'UserGroup' = $usergroup;
                        'Attributes' = $iatts;
                        'LoggedDate' = Get-Date
                        }

                    $filerec += New-Object -TypeName PSObject -Property $propsfile 

                    $filesRes += $filerec
                }
    }

    Write-Host "File record count : " + $filesRes.Count.ToString()

    Return $filesRes,$folderRes

}

Upvotes: 0

Views: 178

Answers (1)

Robert Dyjas
Robert Dyjas

Reputation: 5227

You don't need to add to $filerec and $report, you can just overwrite them (change += to =):

$filerec = New-Object -TypeName PSObject -Property $propsfile
$report = New-Object -TypeName PSObject -Property $props

Current code tries to create an array of objects and then add it to another array which would create duplicates in $folderRes and $filesRes.

Upvotes: 1

Related Questions