From a42c3bf5efd841de24e80ecac8f02110e028c8e1 Mon Sep 17 00:00:00 2001 From: Kawawete Date: Wed, 13 Mar 2024 17:41:33 +0100 Subject: [PATCH] Tried to integrate Threading, as by default, a lot of functionnality will hang the GUI --- Sharepoint/gui/spmover/.vscode/launch.json | 11 ++ Sharepoint/gui/spmover/sputils.ps1 | 197 ++++++++++++++------- 2 files changed, 143 insertions(+), 65 deletions(-) create mode 100644 Sharepoint/gui/spmover/.vscode/launch.json diff --git a/Sharepoint/gui/spmover/.vscode/launch.json b/Sharepoint/gui/spmover/.vscode/launch.json new file mode 100644 index 0000000..44590ca --- /dev/null +++ b/Sharepoint/gui/spmover/.vscode/launch.json @@ -0,0 +1,11 @@ +{ + "configurations": [ + { + "name": "PowerShell: Launch Script", + "type": "PowerShell", + "request": "launch", + "script": "${workspaceFolder}\\sputils.ps1", + "args": [] + } + ] +} \ No newline at end of file diff --git a/Sharepoint/gui/spmover/sputils.ps1 b/Sharepoint/gui/spmover/sputils.ps1 index f0f1755..a998f9b 100644 --- a/Sharepoint/gui/spmover/sputils.ps1 +++ b/Sharepoint/gui/spmover/sputils.ps1 @@ -12,13 +12,28 @@ # Source : C:\Users\SebastienQUEROL\source\repos\sharepointmover-gui\Form1.Designer.cs ################################################################################ -function Get-ScriptDirectory -{ #Return the directory name of this script - $Invocation = (Get-Variable MyInvocation -Scope 1).Value - Split-Path $Invocation.MyCommand.Path -} +###############Thread Setup -$ScriptPath = Get-ScriptDirectory +$Global:syncHash = [hashtable]::Synchronized(@{}) +$newRunspace =[runspacefactory]::CreateRunspace() +$newRunspace.ApartmentState ="STA" +$newRunspace.ThreadOptions ="ReuseThread" +$newRunspace.Open() +$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) + +########################### + +##Aucune foutre idée de ce qu'est cense faire ce morceau de code, ca a été généré par le createur de forms +# function Get-ScriptDirectory +# { #Return the directory name of this script +# $Invocation = (Get-Variable MyInvocation -Scope 1).Value +# Split-Path $Invocation.MyCommand.Path +# } +# $ScriptPath = Get-ScriptDirectory +### + +#### Premier runspace, contien le programme dans sa totalité +$MainWindowGUI = [PowerShell]::Create().AddScript({ # Chargement des assemblies externes Add-Type -AssemblyName System.Windows.Forms @@ -209,7 +224,7 @@ $BconnectSP.UseVisualStyleBackColor = $true # $URLTenant.Location = New-Object System.Drawing.Point(22, 55) $URLTenant.Name = "URLTenant" -$URLTenant.Text = "https://perlinpimpim-admin.sharepoint.com" +$URLTenant.Text = "https://guycarlier.sharepoint.com" $URLTenant.Size = New-Object System.Drawing.Size(325, 20) $URLTenant.TabIndex = 1 # @@ -364,7 +379,7 @@ $tabPage5.UseVisualStyleBackColor = $true $LresultsMove.HideSelection = $false $LresultsMove.Location = New-Object System.Drawing.Point(8, 173) $LresultsMove.Name = "LresultsMove" -$LresultsMove.FullRowSelect = true +#$LresultsMove.FullRowSelect = true $LresultsMove.Size = New-Object System.Drawing.Size(776, 190) $LresultsMove.TabIndex = 7 $LresultsMove.AutoSize = $true @@ -385,76 +400,123 @@ $FMain.Text = "SharePoint Utils GUI" #Onglet Move ##Lister les sites dans les comboBoxes $BlistSites.Add_Click({ - #On vide la variable pnpConnection pour ne pas avoir de faux positifs - $pnpConnection=$Null - #On stocke la derniere connection PnP - $pnpConnection = Get-PnPConnection | Out-string - #A ameliorer, mais pour l'instant, ca devrait passer - if(-not ([string]::IsNullOrEmpty($pnpConnection))){ - $CBdestSite.Items.Clear() - $CBsourceSite.Items.Clear() - $sitesList = $null - $sitesList = Get-PnPTenantSite | Where -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1", 'EHS#1','POINTPUBLISHINGTOPIC#0') | Select url - $LresultsMove.Items.Clear() - foreach($siteUrl in $sitesList){ - $siteUrl -match "@{Url=(?.*)}" - $siteUrlClean = $matches["content"] - $CBsourceSite.Items.Add([string]$siteUrlClean) - $CBdestSite.Items.Add([string]$siteUrlClean) - } - } - elseif(([string]::IsNullOrEmpty($pnpConnection))){ - $LresultsMove.Items.Clear() - $LresultsMove.Items.Add("Connectez-vous au tenant d'abord") + $newRunspace =[runspacefactory]::CreateRunspace() + $newRunspace.ApartmentState ="STA" + $newRunspace.ThreadOptions ="ReuseThread" + $newRunspace.Open() + $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) + + $BlistSitesRunSpace = [PowerShell]::Create().AddScript({ + + #On vide la variable pnpConnection pour ne pas avoir de faux positifs + $pnpConnection=$Null + #On stocke la derniere connection PnP + $pnpConnection = Get-PnPConnection | Out-string + #A ameliorer, mais pour l'instant, ca devrait passer + if(-not ([string]::IsNullOrEmpty($pnpConnection))){ + $CBdestSite.Items.Clear() + $CBsourceSite.Items.Clear() + $sitesList = $null + $sitesList = Get-PnPTenantSite | Where-Object -Property Template -NotIn ("SRCHCEN#0", "REDIRECTSITE#0", "SPSMSITEHOST#0", "APPCATALOG#0", "POINTPUBLISHINGHUB#0", "EDISC#0", "STS#-1", 'EHS#1','POINTPUBLISHINGTOPIC#0') | Select-Object url + $LresultsMove.Items.Clear() + foreach($siteUrl in $sitesList){ + $siteUrl -match "@{Url=(?.*)}" + $siteUrlClean = $matches["content"] + $CBsourceSite.Items.Add([string]$siteUrlClean) + $CBdestSite.Items.Add([string]$siteUrlClean) + } } - else{ - $LresultsMove.Items.Clear() - $LresultsMove.Items.Add("Connectez-vous au tenant d'abord") - } + elseif(([string]::IsNullOrEmpty($pnpConnection))){ + $LresultsMove.Items.Clear() + $LresultsMove.Items.Add("Connectez-vous au tenant d'abord") + } + else{ + $LresultsMove.Items.Clear() + $LresultsMove.Items.Add("Connectez-vous au tenant d'abord") + } + }) + $BlistSitesRunSpace.PowerShell.Runspace = $newRunspace + $BlistSitesRunSpace.Thread = $BlistSitesRunSpace.PowerShell.BeginInvoke() }) ##Bouger les dossiers -$Bgo.Add_Click({ - $sourceSite = $CBsourceSite.Text - $destSite = $CBdestSite.Text +# $copyIt = { +# param ($sourceSite,$destSite) - $jobMovePending = Copy-PnPFile -SourceUrl $sourceSite -TargetUrl $destSite -Force -OverwriteIfAlreadyExists - $jobStatus = Receive-PnPCopyMoveJobStatus -Job $jobMovePending - Write-Host $jobStatus - +# $sourceSite = $sourceSite -Replace '[A-Za-z]+://([A-Za-z]+(\.[A-Za-z]+)+)','' +# $destSite = $destSite -Replace '[A-Za-z]+://([A-Za-z]+(\.[A-Za-z]+)+)','' + +# $jobCopyPending = Copy-PnPFile -SourceUrl "$sourceSite" -TargetUrl "$destSite" -Force -OverwriteIfAlreadyExists +# $jobStatus = Receive-PnPCopyMoveJobStatus -Job $jobCopyPending -Wait +# Write-Output $jobStatus + +# } + +$Bgo.Add_Click({ + + $newRunspace =[runspacefactory]::CreateRunspace() + $newRunspace.ApartmentState ="STA" + $newRunspace.ThreadOptions ="ReuseThread" + $newRunspace.Open() + $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) + + $copySPRunspace.PowerShell = [PowerShell]::Create().AddScript({ + $destSite = $CBdestSite.Text + $sourceSite = $CBsourceSite.Text + $sourceSite = $sourceSite -Replace '[A-Za-z]+://([A-Za-z]+(\.[A-Za-z]+)+)','' + $destSite = $destSite -Replace '[A-Za-z]+://([A-Za-z]+(\.[A-Za-z]+)+)','' + + $jobCopyPending = Copy-PnPFile -SourceUrl "$sourceSite" -TargetUrl "$destSite" -Force -OverwriteIfAlreadyExists + $jobStatus = Receive-PnPCopyMoveJobStatus -Job $jobCopyPending -Wait + Write-Output $jobStatus + }) + $copySPRunspace.PowerShell.Runspace = $newRunspace + $copySPRunspace.Thread = $copySPRunspace.PowerShell.BeginInvoke() }) + + #Onglet Parametres ##Connexion Sharepoint +$pnpConnection=$null +$pnpConnection = Get-PnPConnection | Out-string +$tenantURL = $URLTenant.Text + $BconnectSP.Add_Click({ - - $pnpConnection=$null - $pnpConnection = Get-PnPConnection | Out-string - $tenantURL = $URLTenant.Text - $connResults = Connect-PnPOnline -Url $tenantURL -Interactive 2>&1 | Out-String + Disconnect-PnPOnline + $newRunspace =[runspacefactory]::CreateRunspace() + $newRunspace.ApartmentState ="STA" + $newRunspace.ThreadOptions ="ReuseThread" + $newRunspace.Open() + $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) - Switch ($ERROR) - { - {$ERROR[0] -match "Host not reachable" }{ - $label4.Text = "Injoignable" - $label4.ForeColor = [System.Drawing.Color]::MediumVioletRed - Break} - {$ERROR[0] -match "User canceled authentication."}{ - $label4.Text = "Annulé" - $label4.ForeColor = [System.Drawing.Color]::MediumVioletRed - Break} - {-not ([string]::IsNullOrEmpty($pnpConnection))}{ - $label4.Text = "Connecté" - $label4.ForeColor = [System.Drawing.Color]::Green - } - Default { - $label4.Text = "Connecté" - $label4.ForeColor = [System.Drawing.Color]::Green + $connectToSPRunspace.PowerShell = [PowerShell]::Create().AddScript({ + Connect-PnPOnline -Url $tenantURL -Interactive | Out-String + + Switch ($ERROR) + { + {$ERROR[0] -match "Host not reachable" }{ + $label4.Text = "Injoignable" + $label4.ForeColor = [System.Drawing.Color]::MediumVioletRed + Break} + {$ERROR[0] -match "User canceled authentication."}{ + $label4.Text = "Annulé" + $label4.ForeColor = [System.Drawing.Color]::MediumVioletRed + Break} + {-not ([string]::IsNullOrEmpty($pnpConnection))}{ + $label4.Text = "Connecté" + $label4.ForeColor = [System.Drawing.Color]::Green + } + Default { + $label4.Text = "Connecté" + $label4.ForeColor = [System.Drawing.Color]::Green + } } - } - -}) + }) + $connectToSPRunspace.PowerShell.Runspace = $newRunspace + $connectToSPRunspace.Thread = $connectToSPRunspace.PowerShell.BeginInvoke() + }) function OnFormClosing_FMain{ @@ -478,3 +540,8 @@ $FMain.Add_Shown({$FMain.Activate()}) $ModalResult=$FMain.ShowDialog() # Libération de la Form $FMain.Dispose() + +}) + +$MainWindowGUI.Runspace = $newRunspace +$data = $MainWindowGUI.BeginInvoke() \ No newline at end of file