none
Windows 7 Powershell Error Method invocation failed because [System.IO.FileInfo]... RRS feed

  • Question

  • Hello,

    First off I am not a scripter. I have dabbled in it for years but can't script for beans. I have been working with our company scripter for a week and have a user backup script that works but not without errors. To be honest our scripter has spent a lot of time on it (too much, actually) so I don't hold anything against him, but since it works he is being better utilized. He said it ran without errors on his Windows 10 PC with a different version of PS, but I need it for Widnows 7 PC's. It will be ran to save our users backup info to be used when we roll them to Windows 10. Our users freak when they see the red text and try to stop it so I would like to get the error fixed with your help. I will post the errors then the script.

    This is for the Windows 7 desktop using the following powershell version:

    PSVersion                      5.1.1440

    PSEdition                      Desktop

    PSCompatibleVersions           {1.0, 2.

    BuildVersion                   10.0.144

    CLRVersion                     4.0.3031

    WSManStackVersion              3.0

    PSRemotingProtocolVersion      2.3

    Thank you for your assistance

    Errors:

    Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
    At U:\My Documents\Backup_Script\backup_script_2.ps1:126 char:9
    +         $FilesCount += Get-ChildItem $Backup -Recurse | Where-Object  ...
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
    Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
    At U:\My Documents\Backup_Script\backup_script_2.ps1:126 char:9
    +         $FilesCount += Get-ChildItem $Backup -Recurse | Where-Object  ...
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
    Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
    At U:\My Documents\Backup_Script\backup_script_2.ps1:126 char:9
    +         $FilesCount += Get-ChildItem $Backup -Recurse | Where-Object  ...
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
    Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
    At U:\My Documents\Backup_Script\backup_script_2.ps1:126 char:9
    +         $FilesCount += Get-ChildItem $Backup -Recurse | Where-Object  ...
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

    You cannot call a method on a null-valued expression.
    At U:\My Documents\Backup_Script\backup_script_2.ps1:128 char:9
    +         $SumMB+=$colItems.Sum.ToString()
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Script:

    #Get Logged in User
    $user1 = $env:USERNAME
    #Variables, Make changes here ONLY!
    $rptpath = "U:\MyBackup"
    $Destination="U:\MyBackup" #Copy the Files to this Location
    $Staging="C:\Users\Downloads\Staging"
    $ClearStaging=$false # When $true, Staging Dir will be cleared
    $Versions="3" #How many of the last Backups you want to keep
    #$BackupDirs=“C:\Test"
    $BackupDirs=“C:\Users\$user1\AppData\Roaming\Chrome\Data\Default\Bookmarks”,  "C:\Users\$user1\Favorites", "C:\Users\$user1\AppData\Roaming\Microsoft\Sticky Notes”, "C:\Users\$user1\Desktop", "C:\Users\$user1\Favorites\Favorites Bar"
    $ExcludeDirs="C:\Users\seimi\OneDrive \0-Temp\Dir1","C:\Users\seimi\OneDrive - Joe Schmo\0-Temp\Dir2" #This list of Directories will not be copied
    $LogName="Log.txt" #Log Name
    $LoggingLevel="3" #LoggingLevel only for Output in Powershell Window, 1=smart, 3=Heavy
    $Zip=$false #Zip the Backup Destination
    $Use7ZIP=$false #Make sure it is installed
    $RemoveBackupDestination=$false #Remove copied files after Zip, only if $Zip is true
    $UseStaging=$false #only if you use ZIP, than we copy file to Staging, zip it and copy the ZIP to destination, like Staging, and to save NetworkBandwith
    #STOP-no changes from here
    #Settings - do not change anything from here down------------------------------------------------------------------------
    $ExcludeString=""
    #[string[]]$excludedArray = $ExcludeDirs -split ","
    foreach ($Entry in $ExcludeDirs)
    {
        $Temp="^"+$Entry.Replace("\","\\")
        $ExcludeString+=$Temp+"|"
    }
    $ExcludeString=$ExcludeString.Substring(0,$ExcludeString.Length-1)
    #$ExcludeString
    [RegEx]$exclude = $ExcludeString
    if ($UseStaging -and $Zip)
    {
        #Logging "INFO" "Use Temp Backup Dir"
        $Backupdir=$Staging +"\Backup"#+ (Get-Date -format yyyy-MM-dd)+"-"+(Get-Random -Maximum 100000)+"\"
    }
    else
    {
        #Logging "INFO" "Use orig Backup Dir"
        $Backupdir=$Destination +"\Backup" #+ (Get-Date -format yyyy-MM-dd)+"-"+(Get-Random -Maximum 100000)+"\"
    }
    #$BackupdirTemp=$Temp +"\Backup"#+ (Get-Date -format yyyy-MM-dd)+"-"+(Get-Random -Maximum 100000)+"\"
    $Log=$Backupdir+$LogName
    $Log
    $Items=0
    $Count=0
    $ErrorCount=0
    $StartDate=Get-Date #-format dd.MM.yyyy-HH:mm:ss
    #FUNCTION
    #Logging
    Function Logging ($State, $Message) {
        $Datum=Get-Date -format dd.MM.yyyy-HH:mm:ss
        if (!(Test-Path -Path $Log)) {
            New-Item -Path $Log -ItemType File | Out-Null
        }
        $Text="$Datum - $State"+":"+" $Message"
        if ($LoggingLevel -eq "1" -and $Message -notmatch "was copied") {Write-Host $Text}
        elseif ($LoggingLevel -eq "3") {Write-Host $Text}
      
        add-Content -Path $Log -Value $Text
       
    }

    #Create Backupdir
    Function Create-Backupdir {
        New-Item -Path $Backupdir -ItemType Directory | Out-Null
        sleep -Seconds 5
        Logging "INFO" "Create Backupdir $Backupdir"
    }
    #Delete Backupdir
    Function Delete-Backupdir {
        $Folder=Get-ChildItem $Destination | where {$_.Attributes -eq "Directory"} | Sort-Object -Property CreationTime -Descending:$false | Select-Object -First 1
        Logging "INFO" "Remove Dir: $Folder"
       
        $Folder.FullName | Remove-Item -Recurse -Force
    }

    #Delete Zip
    Function Delete-Zip {
        $Zip=Get-ChildItem $Destination | where {$_.Attributes -eq "Archive" -and $_.Extension -eq ".zip"} |  Sort-Object -Property CreationTime -Descending:$false |  Select-Object -First 1
        Logging "INFO" "Remove Zip: $Zip"
       
        $Zip.FullName | Remove-Item -Recurse -Force
    }
    #Check if Backupdirs and Destination is available
    function Check-Dir {
        Logging "INFO" "Check if BackupDir and Destination exists"
        if (!(Test-Path $BackupDirs)) {
            return $false
            Logging "Error" "$BackupDirs does not exist"
        }
        if (!(Test-Path $Destination)) {
            return $false
            Logging "Error" "$Destination does not exist"
        }
    }
    #Save all the Files
    Function Make-Backup {
        Logging "INFO" "Started the Backup"
        $Files=@()
        $SumMB=0
        $SumItems=0
        $SumCount=0
        $colItems=0
        Logging "INFO" "Count all files and create the Top Level Directories"
        foreach ($Backup in $BackupDirs) {
            $colItems = (Get-ChildItem $Backup -recurse | Where-Object {$_.mode -notmatch "h"} | Measure-Object -property length -sum)
            $Items=0
            $FilesCount += Get-ChildItem $Backup -Recurse | Where-Object {$_.mode -notmatch "h"} 
            Copy-Item -Path $Backup -Destination $Backupdir -Force -ErrorAction SilentlyContinue
            $SumMB+=$colItems.Sum.ToString()
            $SumItems+=$colItems.Count
        }
        $TotalMB="{0:N2}" -f ($SumMB / 1MB) + " MB of Files"
        Logging "INFO" "There are $SumItems Files with  $TotalMB to copy"
        foreach ($Backup in $BackupDirs) {
            $Index=$Backup.LastIndexOf("\")
            $SplitBackup=$Backup.substring(0,$Index)
            $Files = Get-ChildItem $Backup -Recurse  | select * | Where-Object {$_.mode -notmatch "h" -and $_.fullname -notmatch $exclude} | select fullname #$_.mode -notmatch "h" -and
            foreach ($File in $Files) {
                $restpath = $file.fullname.replace($SplitBackup,"")
                try {
                    Copy-Item  $file.fullname $($Backupdir+$restpath) -Force -ErrorAction SilentlyContinue |Out-Null
                    Logging "INFO" "$file was copied"
                }
                catch {
                    $ErrorCount++
                    Logging "ERROR" "$file returned an error an was not copied"
                }
                $Items += (Get-item $file.fullname).Length
                $status = "Copy file {0} of {1} and copied {3} MB of {4} MB: {2}" -f $count,$SumItems,$file.Name,("{0:N2}" -f ($Items / 1MB)).ToString(),("{0:N2}" -f ($SumMB / 1MB)).ToString()
                $Index=[array]::IndexOf($BackupDirs,$Backup)+1
                $Text="Copy data Location {0} of {1}" -f $Index ,$BackupDirs.Count
                Write-Progress -Activity $Text $status -PercentComplete ($Items / $SumMB*100) 
                if ($File.Attributes -ne "Directory") {$count++}
            }
        }
        $SumCount+=$Count
        $SumTotalMB="{0:N2}" -f ($Items / 1MB) + " MB of Files"
        Logging "INFO" "----------------------"
        Logging "INFO" "Copied $SumCount files with $SumTotalMB"
        Logging "INFO" "$ErrorCount Files could not be copied"

        # Send e-mail with reports as attachments
        if ($SendEmail -eq $true) {
            $EmailSubject = "Backup Email $(get-date -format MM.yyyy)"
            $EmailBody = "Backup Script $(get-date -format MM.yyyy) (last Month).`nYours sincerely `Matthew - SYSTEM ADMINISTRATOR"
            Logging "INFO" "Sending e-mail to $EmailTo from $EmailFrom (SMTPServer = $EmailSMTP) "
            ### the attachment is $log
            Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body $EmailBody -SmtpServer $EmailSMTP -attachment $Log
        }
    }

    #create Backup Dir
    Create-Backupdir
    Logging "INFO" "----------------------"
    Logging "INFO" "Start the Script"
    #Check if Backupdir needs to be cleaned and create Backupdir
    $Count=(Get-ChildItem $Destination | where {$_.Attributes -eq "Directory"}).count
    Logging "INFO" "Check if there are more than $Versions Directories in the Backupdir"
    if ($count -gt $Versions)
    {
        Delete-Backupdir
    }

    $CountZip=(Get-ChildItem $Destination | where {$_.Attributes -eq "Archive" -and $_.Extension -eq ".zip"}).count
    Logging "INFO" "Check if there are more than $Versions Zip in the Backupdir"
    if ($CountZip -gt $Versions) {
        Delete-Zip
    }
    #Check if all Dir are existing and do the Backup
    $CheckDir=Check-Dir
    if ($CheckDir -eq $false) {
        Logging "ERROR" "One of the Directory are not available, Script has stopped"
    } else {
        Make-Backup
        $Enddate=Get-Date #-format dd.MM.yyyy-HH:mm:ss
        $span = $EndDate - $StartDate
        $Minutes=$span.Minutes
        $Seconds=$Span.Seconds
        Logging "INFO" "Backupduration $Minutes Minutes and $Seconds Seconds"
        Logging "INFO" "----------------------"
        Logging "INFO" "----------------------"
        if ($Zip)
        {
            Logging "INFO" "Compress the Backup Destination"
           
            if ($Use7ZIP)
            {
                Logging "INFO" "Use 7ZIP"
                if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {Logging "WARNING" "7Zip not found"}
                set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
                #sz a -t7z "$directory\$zipfile" "$directory\$name"   
                       
                if ($UseStaging -and $Zip)
                {
                    $Zip=$Staging+("\"+$Backupdir.Replace($Staging,'').Replace('\','')+".zip")
                    sz a -t7z $Zip $Backupdir
                   
                    Logging "INFO" "Move Zip to Destination"
                    Move-Item -Path $Zip -Destination $Destination
                    if ($ClearStaging)
                    {
                    Logging "INFO" "Clear Staging"
                    Get-ChildItem -Path $Staging -Recurse -Force | remove-item -Confirm:$false -Recurse
                    }
                }
                else
                {
                    sz a -t7z ($Destination+("\"+$Backupdir.Replace($Destination,'').Replace('\','')+".zip")) $Backupdir
                }
                   
            }
            else
            {
            Logging "INFO" "Use Powershell Compress-Archive"
            Compress-Archive -Path $Backupdir -DestinationPath ($Destination+("\"+$Backupdir.Replace($Destination,'').Replace('\','')+".zip")) -CompressionLevel Optimal -Force
            }

            If ($RemoveBackupDestination)
            {
                Logging "INFO" "Backupduration $Minutes Minutes and $Seconds Seconds"
                #Remove-Item -Path $BackupDir -Force -Recurse
                get-childitem -Path $BackupDir -recurse -Force  | remove-item -Confirm:$false -Recurse
                get-item -Path $BackupDir   | remove-item -Confirm:$false -Recurse
            }
        }
    }
    Write-Host "Press any key to close ..."
    #$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

    • Moved by Bill_Stewart Monday, July 29, 2019 8:26 PM This is not "troubleshoot my 300 line script for me" forum
    Monday, May 20, 2019 10:40 PM

All replies

  • First please understand that you must post code using the code posting tool provided.  Your code, as posted, is unreadable in most browsers.


    \_(ツ)_/

    Monday, May 20, 2019 10:44 PM
  • Second you must isolate the line as we do not waste our time counting line numbers.

    The error is explicit.  You have bad syntax on the line in question. 

    The version of PS you are running is the same version as runs on W10.  WMF 5.1.

    Also note that Microsoft has tools that do everything you are trying to do.  Please check with the MDT site to find out how to migrate a users account from one OS to another.  It takes very little effort and can be completely customized.  There hasn't been a need to write a script for this since Windows 98.


    \_(ツ)_/

    Monday, May 20, 2019 10:48 PM
  • Please read the following to gain a better understanding of the requirements for posting in a technical forum:

    How to ask questions in a technical forum


    \_(ツ)_/

    Monday, May 20, 2019 10:50 PM
  • Start here to learn how to migrate Windows accounts: https://docs.microsoft.com/en-us/sccm/mdt/


    \_(ツ)_/

    • Proposed as answer by LikeToCode Thursday, May 23, 2019 8:44 PM
    Monday, May 20, 2019 10:51 PM
  • Also note that your error message does not match the script posted.

    Start by actually looking at your error line.  Run in the debugger and analyze the line and its arguments.  That is something that we cannot do for you.


    \_(ツ)_/

    Monday, May 20, 2019 10:54 PM