Last active
November 14, 2015 15:32
-
-
Save tiagoduarte/2d178594789ede0ce1e9 to your computer and use it in GitHub Desktop.
Example that can, a) download a secure document and b) convert an excel to csv using local client application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #summary | |
| #this script downloads an Excel document, saves it locally, and converts it to CSV | |
| #by tiago duarte @ 15-sep-2015 | |
| #http://about.me/tiagoduarte | |
| #set parameters for calling externally | |
| param( | |
| $xlsUrl = "http://mywebsite.com/mydocument.xlsx", | |
| $tempName = "downloaded-spreadsheet", | |
| $tempPath = "c:\temp\$tempName.xlsx" | |
| ) | |
| #http://stackoverflow.com/questions/508565/how-to-make-an-authenticated-web-request-in-powershell | |
| function DownloadFile($source, $destination) | |
| { | |
| if(test-path $destination) | |
| { | |
| write-host ("Removing previously downloaded file..." + $nl) | |
| rm $destination | |
| } | |
| write-host ("Downloading $source..." + $nl) | |
| $wc = New-Object System.Net.WebClient | |
| $wc.Credentials = $credentials | |
| $wc.DownloadFile($source, $destination) | |
| $wc.Dispose() | |
| } | |
| #http://stackoverflow.com/questions/27293481/convert-multiple-xls-to-csv-using-powershell | |
| function ConvertExcel-ToCSV($source) | |
| { | |
| $excelVisible = $false | |
| $excelCompatCheck = $false | |
| $excelSaveChanges = $false | |
| $file = gci $source | |
| $newname = $file.FullName -replace '\.xlsx$', '.csv' | |
| if(test-path $newname) | |
| { | |
| write-host ("Removing previous csv..." + $nl) | |
| rm $newname | |
| } | |
| write-host ("Converting $source to CSV..." + $nl) | |
| $ExcelWB = new-object -comobject excel.application -Property @{Visible = $excelVisible} | |
| $Workbook = $ExcelWB.Workbooks.Open($file.FullName) | |
| $Workbook.CheckCompatibility = $excelCompatCheck | |
| $Workbook.SaveAs($newname, 6) | |
| $Workbook.Close($excelSaveChanges) | |
| $ExcelWB.Quit() | |
| #this line is crucial in order to effectively close the Excel process started by the command line | |
| [Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWB) | Out-Null | |
| $Workbook = $null | |
| $ExcelWB = $null | |
| if(test-path $source) | |
| { | |
| rm $source | |
| write-host ("Removing source file..." + $nl) | |
| } | |
| } | |
| try | |
| { | |
| clear-host | |
| $global:nl = [Environment]::NewLine | |
| $user = read-host "Username" | |
| $pass = read-host -assecurestring "Password" | |
| $domain = read-host "Domain" | |
| $global:credentials = new-object System.Net.NetworkCredential($user, $pass, $domain) | |
| #$global:credentials = new-object System.Net.NetworkCredential("testuser", "pass", "domain") | |
| write-host ("Script execution started!" + $nl) | |
| DownloadFile $xlsUrl $tempPath | |
| ConvertExcel-ToCSV $tempPath | |
| #Get-Process | Where-Object {$_.Path -like "*Excel.exe*"} | Stop-Process | |
| } | |
| catch | |
| { | |
| write-host ($nl) | |
| write-warning "Program Crashed!" | |
| write-host ($nl) | |
| $_.Exception | |
| write-host ($nl) | |
| write-host ("ScriptName: " + ($_.InvocationInfo.ScriptName.Substring($_.InvocationInfo.ScriptName.lastIndexOf("\")+1)) + $nl) | |
| write-host ("Line: " + $_.InvocationInfo.ScriptLineNumber + $nl)# + " - Column: " + $_.InvocationInfo.OffsetInLine + $nl) | |
| write-host ("Column: " + $_.InvocationInfo.OffsetInLine + $nl) | |
| write-host ("Type: " + $_.CategoryInfo.Reason + $nl) | |
| } | |
| finally | |
| { | |
| write-host ("Script execution ended!" + $nl) | |
| } |
Author
tiagoduarte
commented
Nov 14, 2015

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment