-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_To_Pulse.ps1
More file actions
305 lines (274 loc) · 13.5 KB
/
upload_To_Pulse.ps1
File metadata and controls
305 lines (274 loc) · 13.5 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<# upload_To_Pulse.ps1 (v2)
- Runs Python generator
- Waits for the expected monthly Excel to appear and be unlocked
- Uploads to /work-essentials/apps/pulse-intranet/Documents
- Updates /work-essentials/apps/pulse-intranet/Pages/Pulse-page-analytics.aspx
- Logs to Optional\upload_To_Pulse.log
#>
[CmdletBinding()]
param(
[string]$SiteUrl = "https://pulse/work-essentials/apps/pulse-intranet",
[string]$LibraryServerRelUrl = "/work-essentials/apps/pulse-intranet/Documents",
[string]$PageServerRelUrl = "/work-essentials/apps/pulse-intranet/Pages/Pulse-page-analytics.aspx",
[string]$PythonScript = "generate_Report.py",
[int]$MonthsToUpdate = 3,
[switch]$PublishAfterUpload,
[switch]$Overwrite
)
$ErrorActionPreference = 'Stop'
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$optionalDir = Join-Path $here "Optional"; New-Item -ItemType Directory -Force -Path $optionalDir | Out-Null
if (-not $PSBoundParameters.ContainsKey('PublishAfterUpload')) { $PublishAfterUpload = $true }
if (-not $PSBoundParameters.ContainsKey('Overwrite')) { $Overwrite = $true }
# ---- log
$logPath = Join-Path $optionalDir "upload_To_Pulse.log"
Start-Transcript -Path $logPath -Append | Out-Null
Write-Host "=== Pulse GA4 Upload Runner ===" -ForegroundColor Cyan
Write-Host "Working dir: $here"
# ---- previous full month & expected filename
$today = Get-Date
$firstThisMonth = Get-Date -Year $today.Year -Month $today.Month -Day 1
$lastPrev = $firstThisMonth.AddDays(-1)
$firstPrev = Get-Date -Year $lastPrev.Year -Month $lastPrev.Month -Day 1
$dateTag = "{0:yyyyMMdd}-{1:yyyyMMdd}" -f $firstPrev,$lastPrev
$expectedName = "Pulse-all-pages-report-$dateTag.xlsx"
$expectedRoot = Join-Path $here $expectedName
$expectedOptional = Join-Path $optionalDir $expectedName
# ---- helper: wait for file and ensure not locked
function Wait-FileReady {
param([string]$Path,[int]$TimeoutSec=180)
$sw=[Diagnostics.Stopwatch]::StartNew()
while($sw.Elapsed.TotalSeconds -lt $TimeoutSec){
if(Test-Path -LiteralPath $Path){
try{
$fs=[System.IO.File]::Open($Path,[System.IO.FileMode]::Open,[System.IO.FileAccess]::ReadWrite,[System.IO.FileShare]::None)
$fs.Close()
return $true
}catch{ Start-Sleep -Seconds 2 }
}else{ Start-Sleep -Seconds 2 }
}
return $false
}
# ---- 1) run python generator
try{
Push-Location $here
Write-Host "Running: py -u $PythonScript" -ForegroundColor Yellow
& py -u $PythonScript
} finally { Pop-Location }
# ---- 2) pick a file to upload (prefer expected name, check root then Optional)
$localFile = $null
if (Wait-FileReady -Path $expectedRoot -TimeoutSec 180) {
$localFile = $expectedRoot
Write-Host "Found expected report in ROOT: $localFile" -ForegroundColor Green
} elseif (Wait-FileReady -Path $expectedOptional -TimeoutSec 180) {
$localFile = $expectedOptional
Write-Host "Found expected report in OPTIONAL: $localFile" -ForegroundColor Green
} else {
# Fallback: newest unlocked file
$cand = Get-ChildItem -Path $here -Filter "Pulse-all-pages-report-*.xlsx" -File |
Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($cand -and (Wait-FileReady -Path $cand.FullName -TimeoutSec 60)) {
$localFile = $cand.FullName
Write-Host "Using newest unlocked file: $localFile" -ForegroundColor Yellow
} else {
throw "Could not find an unlocked 'Pulse-all-pages-report-*.xlsx'. Close Excel and re-run."
}
}
$targetName = $expectedName # always upload with canonical name
# ---- 3) CSOM setup
$csomPath = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI"
Add-Type -Path (Join-Path $csomPath "Microsoft.SharePoint.Client.dll")
Add-Type -Path (Join-Path $csomPath "Microsoft.SharePoint.Client.Runtime.dll")
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$ctx.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
function Ensure-FolderPath {
param([Microsoft.SharePoint.Client.ClientContext]$Ctx,[string]$ServerRelativeUrl)
$web=$Ctx.Web; $Ctx.Load($web); $Ctx.ExecuteQuery()
$parts=$ServerRelativeUrl.Trim('/').Split('/'); if($parts.Length -lt 2){throw "Bad ServerRelativeUrl: $ServerRelativeUrl"}
$cur='/' + ($parts[0..1] -join '/')
for($i=2;$i -lt $parts.Length;$i++){
$cur="$cur/$($parts[$i])"
try{ $f=$web.GetFolderByServerRelativeUrl($cur); $Ctx.Load($f); $Ctx.ExecuteQuery() }catch{
$parentUrl=$cur.Substring(0,$cur.LastIndexOf('/')); $p=$web.GetFolderByServerRelativeUrl($parentUrl)
$Ctx.Load($p); $Ctx.ExecuteQuery(); $nf=$p.Folders.Add($cur); $Ctx.Load($nf); $Ctx.ExecuteQuery()
}
}
return $web.GetFolderByServerRelativeUrl($ServerRelativeUrl.TrimEnd('/'))
}
# validate lib
$lists=$ctx.Web.Lists; $ctx.Load($lists); $ctx.ExecuteQuery()
$docLibs=$lists | Where-Object{ $_.BaseTemplate -eq 101 -and -not $_.Hidden }
foreach($l in $docLibs){ $ctx.Load($l.RootFolder) }; $ctx.ExecuteQuery()
$roots=$docLibs | ForEach-Object{ $_.RootFolder.ServerRelativeUrl.TrimEnd('/') }
if(-not ($roots | Where-Object{ $LibraryServerRelUrl.TrimEnd('/').StartsWith($_,'OrdinalIgnoreCase') })){
throw "Document library not found for: $LibraryServerRelUrl. Use one of:`n$($roots -join "`n")"
}
try{
$folder=$ctx.Web.GetFolderByServerRelativeUrl($LibraryServerRelUrl.TrimEnd('/'))
$ctx.Load($folder); $ctx.ExecuteQuery()
}catch{
Write-Host "Creating library/folder path: $LibraryServerRelUrl" -ForegroundColor DarkYellow
$folder=Ensure-FolderPath -Ctx $ctx -ServerRelativeUrl $LibraryServerRelUrl
}
# ---- 4) upload (after an extra quick unlock wait)
if (-not (Wait-FileReady -Path $localFile -TimeoutSec 60)) {
throw "Local file is still locked: $localFile"
}
$targetUrl = ($folder.ServerRelativeUrl.TrimEnd('/') + '/' + $targetName)
$ctx.Load($folder.Files); $ctx.ExecuteQuery()
$existing = $folder.Files | Where-Object { $_.ServerRelativeUrl -ieq $targetUrl }
if ($existing -and -not $Overwrite) {
Write-Host "File exists and -Overwrite not specified: $targetUrl" -ForegroundColor Yellow
} else {
$bytes=[System.IO.File]::ReadAllBytes($localFile)
$ms = New-Object System.IO.MemoryStream(,$bytes)
$fi = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fi.ContentStream=$ms; $fi.Url=$targetName; $fi.Overwrite=$true
$uploaded=$folder.Files.Add($fi); $ctx.Load($uploaded); $ctx.ExecuteQuery()
$ms.Dispose()
Write-Host "Uploaded: $targetUrl" -ForegroundColor Green
}
try{
$uploaded.CheckIn("Automated upload",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn); $ctx.ExecuteQuery()
if($PublishAfterUpload){ $uploaded.Publish("Automated publish"); $ctx.ExecuteQuery() }
}catch{ Write-Host "Check-in/Publish skipped: $($_.Exception.Message)" -ForegroundColor DarkGray }
# ---- 5) page update (last N full months)
Add-Type -AssemblyName System.Web | Out-Null
function Get-PageHtmlOrNull {
param([Microsoft.SharePoint.Client.ClientContext]$Ctx,[string]$ServerRelUrl)
$file=$Ctx.Web.GetFileByServerRelativeUrl($ServerRelUrl); $item=$file.ListItemAllFields
$Ctx.Load($item); $Ctx.ExecuteQuery()
$html=$null; $field=$null
if($item.FieldValues.ContainsKey("PublishingPageContent") -and $item["PublishingPageContent"]){ $html=[string]$item["PublishingPageContent"]; $field="PublishingPageContent" }
elseif($item.FieldValues.ContainsKey("WikiField") -and $item["WikiField"]){ $html=[string]$item["WikiField"]; $field="WikiField" }
return @($html,$field,$item,$file)
}
function Set-PageHtml { param($Ctx,$Item,[string]$FieldName,[string]$Html)
$Item[$FieldName]=$Html; $Item.Update(); $Ctx.ExecuteQuery()
try{ $Item.File.CheckIn("Auto update monthly links",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn); $Ctx.ExecuteQuery(); $Item.File.Publish("Auto publish"); $Ctx.ExecuteQuery() }catch{}
}
function MonthLabel([datetime]$d){ return ("{0} {1}" -f (Get-Culture).DateTimeFormat.AbbreviatedMonthNames[$d.Month-1].TrimEnd('.'), $d.Year) }
# Build link objects for last N full months IF files exist in the library
$now=Get-Date; $cursor=$now; $links=@()
for($i=0;$i -lt [Math]::Max(1,$MonthsToUpdate);$i++){
$firstThis=Get-Date -Year $cursor.Year -Month $cursor.Month -Day 1
$lastPrev=$firstThis.AddDays(-1)
$firstPrev=Get-Date -Year $lastPrev.Year -Month $lastPrev.Month -Day 1
$lbl=MonthLabel $firstPrev
$nm="Pulse-all-pages-report-{0:yyyyMMdd}-{1:yyyyMMdd}.xlsx" -f $firstPrev,$lastPrev
$href=$LibraryServerRelUrl.TrimEnd('/') + '/' + $nm
$ok=$true; try{ $f=$ctx.Web.GetFileByServerRelativeUrl($href); $ctx.Load($f); $ctx.ExecuteQuery() }catch{ $ok=$false }
if($ok){ $links += [pscustomobject]@{Label=$lbl;Href=$href;Year=$firstPrev.Year} }
$cursor=(Get-Date -Year $firstPrev.Year -Month $firstPrev.Month -Day 1).AddDays(-1)
}
function Insert-Links-IntoHtml {
param([string]$Html,[object[]]$Links)
if([string]::IsNullOrWhiteSpace($Html)){ $Html = "" }
foreach($lnk in $Links){
$label=[regex]::Escape($lnk.Label); $safeHref=[System.Web.HttpUtility]::HtmlAttributeEncode($lnk.Href)
$pat="<a\b[^>]*>(\s*$label\s*)</a>"
if([regex]::IsMatch($Html,$pat,'IgnoreCase')){
$Html=[regex]::Replace($Html,$pat,"<a href=""$safeHref"">$($lnk.Label)</a>",1,'IgnoreCase')
Write-Host "Updated link: $($lnk.Label)" -ForegroundColor Green
} else {
$yearToken=">$($lnk.Year)<"
$idxYear=$Html.IndexOf($yearToken,[StringComparison]::OrdinalIgnoreCase)
if($idxYear -ge 0){
$idxUl=$Html.IndexOf("<ul",$idxYear,[StringComparison]::OrdinalIgnoreCase)
if($idxUl -ge 0){
$idxClose=$Html.IndexOf(">",$idxUl)
if($idxClose -ge 0){
$ins="`n<li><a href=""$safeHref"">$($lnk.Label)</a></li>`n"
$Html=$Html.Insert($idxClose+1,$ins)
Write-Host "Inserted under $($lnk.Year): $($lnk.Label)" -ForegroundColor Green
continue
}
}
}
$Html="<p><a href=""$safeHref"">$($lnk.Label)</a></p>`n$Html"
Write-Host "Prepended: $($lnk.Label)" -ForegroundColor Green
}
}
return $Html
}
# Fallback: update first Content Editor–style web part (property "Content")
function Update-First-ContentEditor-WebPart {
param($Ctx,$File,[object[]]$Links)
$scope=[Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared
$wpm=$File.GetLimitedWebPartManager($scope)
$Ctx.Load($wpm.WebParts); $Ctx.ExecuteQuery()
foreach($def in $wpm.WebParts){ $Ctx.Load($def); $Ctx.Load($def.WebPart) }
$Ctx.ExecuteQuery()
if($wpm.WebParts.Count -eq 0){
Write-Host "No web parts found on page." -ForegroundColor DarkYellow
return $false
}
$preferred = @('Pulse','Analytics','Reports','Links')
$cands=@()
foreach($def in $wpm.WebParts){
$wp=$def.WebPart
$t=$wp.Title
Write-Host ("WebPart: Title='{0}' Type='{1}'" -f $t,$wp.GetType().FullName) -ForegroundColor Gray
$props=$wp.Properties
$hasContent=$false
try{ $hasContent = $props -and $props.FieldValues.ContainsKey("Content") }catch{ $hasContent=$false }
if($hasContent){
$score=100; foreach($tok in $preferred){ if($t -and ($t -match $tok)){ $score-=50 } }
$cands += [pscustomobject]@{Def=$def;WP=$wp;Score=$score}
}
}
if($cands.Count -eq 0){
Write-Host "No CEWP-like web part with editable 'Content' found." -ForegroundColor DarkYellow
return $false
}
$chosen = $cands | Sort-Object Score | Select-Object -First 1
$wp = $chosen.WP; $def = $chosen.Def
$old=""; try{ $old=[string]$wp.Properties["Content"] }catch{}
$new = Insert-Links-IntoHtml -Html $old -Links $Links
if($new -eq $old){ Write-Host ("No changes needed in '" + $wp.Title + "'.") -ForegroundColor DarkGray; return $true }
$wp.Properties["Content"]=$new
$def.SaveWebPartChanges(); $Ctx.ExecuteQuery()
Write-Host ("Updated web part '" + $wp.Title + "' content.") -ForegroundColor Green
return $true
}
if($links.Count -gt 0){
$ph = Get-PageHtmlOrNull -Ctx $ctx -ServerRelUrl $PageServerRelUrl
$html=$ph[0]; $field=$ph[1]; $item=$ph[2]; $file=$ph[3]
$updated = $false
if($field){
$newHtml = Insert-Links-IntoHtml -Html $html -Links $links
if($newHtml -ne $html){ Set-PageHtml -Ctx $ctx -Item $item -FieldName $field -Html $newHtml }
$updated = $true
Write-Host "Page updated (field: $field)." -ForegroundColor Green
} else {
Write-Host "Page has no Publishing/Wiki field; attempting Web Part update..." -ForegroundColor Yellow
$updated = Update-First-ContentEditor-WebPart -Ctx $ctx -File $file -Links $links
}
if(-not $updated){
Write-Host "Could not update page content automatically. See log for web part list." -ForegroundColor DarkYellow
}
} else {
Write-Host "No page updates (no matching monthly files in library)." -ForegroundColor DarkGray
}
# ---- end / logging tidy ----
try { Stop-Transcript | Out-Null } catch {}
Write-Host ("Done. Log: " + (Join-Path $optionalDir "upload_To_Pulse.log")) -ForegroundColor Cyan
# Stop-Transcript | Out-Null
# ---- logging
$logPath = Join-Path $optionalDir "upload_To_Pulse.log"
$transcriptStarted = $false
try {
Start-Transcript -Path $logPath -Append -Force | Out-Null
$transcriptStarted = $true
} catch {
Write-Host "Transcript not started (host limitation): $($_.Exception.Message)" -ForegroundColor DarkGray
}
try {
# >>> all your existing script body goes here <<<
}
finally {
if ($transcriptStarted) {
try { Stop-Transcript | Out-Null } catch {}
}
}
Write-Host "Done. Log: $logPath" -ForegroundColor Cyan