Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

PowerShell Exlaining this Script, for Understanding

$
0
0
function Import-OldXLS {
    [CmdletBinding()] #bind the parameters of the function 
    param (
	#define parameters as mandatory, specify a position
        [Parameter(Mandatory=$true,Position=0)] 
        [System.String]
        $FileName,

        [Parameter(Mandatory=$false,Position=1)]
        [ValidateNotNull()]
        [Microsoft.Office.Interop.Excel.ApplicationClass]
        $ExcelInstance = $null
    )

    begin {
        $_closeExcel = $false
        if ($ExcelInstance -eq $null) {
            $_closeExcel = $true

            try {
                $ExcelInstance = New-Object -ComObject "Excel.Application"
            } catch {
                Write-Error -ErrorRecord $_
            }
        }
    }

    process {
        $results = @{}
        
        try {
            $wb = $ExcelInstance.Workbooks.Open($FileName, 2, $true)
            $sheet = $wb.ActiveSheet
        } catch {
            Write-Error -ErrorRecord $_
            return
        }
        


        for ($i = 2; $i -le $sheet.Range("A2").CurrentRegion.Rows.Count; $i++) {
            try {
                $id = $sheet.Range("A$i").Value2
                $info1 = $sheet.Range("E$i").Value2
                $info2 = $sheet.Range("G$i").Value2
            } catch {
                Write-Error -ErrorRecord $_
                return
            }

            $results[$id] = New-Object -TypeName System.Management.Automation.PSObject |
            Add-Member -MemberType NoteProperty -Name "Info1" -Value $info1 -PassThru |
            Add-Member -MemberType NoteProperty -Name "Info2" -Value $info2 -PassThru
        }

        $wb.Close()

        $results
    }

    end {
        if ($_closeExcel) {
            $ExcelInstance.DisplayAlerts = $false
            $ExcelInstance.Quit()
            [System.Runtime.Interopservices.Marshal]::FinalReleaseComObject($ExcelInstance) | Out-Null
            [GC]::Collect()
        }

    }
}

function Update-NewXLS {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true,Position=0)]
        [System.String]
        $FileName,

        [Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true)]
        [System.Collections.Hashtable]
        $OldData,

        [Parameter(Mandatory=$false,Position=2)]
        [ValidateNotNull()]
        [Microsoft.Office.Interop.Excel.ApplicationClass]
        $ExcelInstance = $null,

        [Switch]
        $AutoSave
    )
    
    begin {
        $_closeExcel = $false
        if ($ExcelInstance -eq $null) {
            $_closeExcel = $true

            try {
                $ExcelInstance = New-Object -ComObject "Excel.Application"
            } catch {
                throw
            }
        }
    }

    process {
        try {
            $wb = $ExcelInstance.Workbooks.Open($FileName, 2, $false)
            $sheet = $wb.ActiveSheet
        } catch {
            Write-Error -ErrorRecord $_
            return
        }

        for ($i = 2; $i -le $sheet.Range("A2").CurrentRegion.Rows.Count; $i++) {
            try {
                $id = $sheet.Range("A$i").Value2
            } catch {
                Write-Error -ErrorRecord $_
                return
            }

            if ($OldData.Contains($id)) {
                $sheet.Range("E$i").Value2 = $OldData[$id].Info1
                $sheet.Range("G$i").Value2 = $OldData[$id].Info2
            } else {
                $sheet.Range("A$($i):G$i").Interior.Color = [System.Drawing.ColorTranslator]::ToOle([System.Drawing.Color]::Red)
            }
        }

        $ExcelInstance.DisplayAlerts = $false

        if ($AutoSave) {
            try {
                $wb.Save()
            } catch {
                $AutoSave = $false
                Write-Error -ErrorRecord $_
            }
        }

        $ExcelInstance.DisplayAlerts = $true
    }

    end {
        if ($_closeExcel -and $AutoSave) {
            $ExcelInstance.DisplayAlerts = $false
            $ExcelInstance.Quit()
            [System.Runtime.Interopservices.Marshal]::FinalReleaseComObject($ExcelInstance) | Out-Null
            [GC]::Collect()
        } else {
            $ExcelInstance.Visible = $true
        }

    }
}

$source = "O:\PowerShell Practice\OLD.xls"
$destination = "O:\PowerShell Practice\NEW.xls"


$objXL = New-Object -ComObject "Excel.Application"
Import-OldXLS -FileName $source -ExcelInstance $objXL | Update-NewXLS -FileName $destination -ExcelInstance  $objXL


Viewing all articles
Browse latest Browse all 21975

Trending Articles