-
Notifications
You must be signed in to change notification settings - Fork 1
/
RefreshExcelFiles.ps1
111 lines (84 loc) · 2.74 KB
/
RefreshExcelFiles.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#VARIABLES
$files = @(
#List of file paths go here:
#"C:\reports\salesHistory.xlsm",
#"C:\reports\teamProductivity.xlsm"
)
$Date = (Get-Date -Format 'M-d-yyyy')
$errorFile = "C:\Temp\RefreshExcelError_" + $Date + ".txt" #Where you want an error file to be generated.
$isError = $false
#Function to test filelock. Found on http://stackoverflow.com/questions/24992681/powershell-check-if-a-file-is-locked
function Test-FileLock {
param (
[parameter(Mandatory=$true)][string]$Path
)
$oFile = New-Object System.IO.FileInfo $Path
if ((Test-Path -Path $Path) -eq $false) {
return $false
}
try {
$oStream = $oFile.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
if ($oStream) {
$oStream.Close()
}
$false
} catch {
# file is locked by a process.
return $true
}
}
#Loop through all files, attempt to grab lock and refresh.
foreach ($file in $files){
#Ensure file exists
IF (!(Test-Path $file)) {
Write-Host $File" not found. `n" -foregroundcolor Red
$errMsg = "FILE NOT FOUND: " + $file
Add-Content $errorFile $errMsg
$isError = $true;
CONTINUE;
}
Write-Host $file -foregroundcolor Green
Write-Host "Checking for lock in file..." -nonewline
#Check if file is locked
IF (Test-FileLock $file){
#File is locked.
#Check if there is an error file yet.
IF (!(Test-Path $errorFile)){
#Error file doesn't exist, create one
New-Item $errorFile -type file
}
#Add entry to log file
$errMsg = "FILE LOCKED: " + $file
Add-Content $errorFile $errMsg
Write-Host "file locked." -foregroundcolor Magenta
Write-Host "Error added to"+$errorFile -foregroundcolor Magenta
$isError = $true;
} ELSE {
#File is NOT locked.
Write-Host "file available."
$excelObj = New-Object -ComObject Excel.Application
$excelObj.Visible = $false
#Open the workbook
$workBook = $excelObj.Workbooks.Open($file)
#Refresh all data in workbook.
Write-Host "Starting refresh..." -nonewline
$workBook.RefreshAll()
Write-Host "done."
Write-Host "Saving file..." -nonewline
$workBook.Save()
Write-Host "done."
#Close workbook.
$workBook.Close()
$excelObj.Quit()
#We must decrement the CLR reference count (to prevent the process from continuing to run in the background, which causes memory and lock problems).
#https://technet.microsoft.com/en-us/library/ff730962.aspx
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excelObj)
Remove-Variable excelObj
}
Write-Host "`n"
}
Write-Host "`n"
#If an anticipated error found above, open the error file.
IF ($isError){
Invoke-Item $errorFile
}