From 5614dcb11cac5fe5d70bd4eb744014a172241888 Mon Sep 17 00:00:00 2001 From: Kawawete Date: Wed, 13 Mar 2024 23:11:49 +0100 Subject: [PATCH 1/2] Chqnged threading stuff, back to jobs --- Sharepoint/gui/spmover/sputils.ps1 | 52 +++++++----------------------- 1 file changed, 12 insertions(+), 40 deletions(-) diff --git a/Sharepoint/gui/spmover/sputils.ps1 b/Sharepoint/gui/spmover/sputils.ps1 index a998f9b..a1d4fb2 100644 --- a/Sharepoint/gui/spmover/sputils.ps1 +++ b/Sharepoint/gui/spmover/sputils.ps1 @@ -14,12 +14,6 @@ ###############Thread Setup -$Global:syncHash = [hashtable]::Synchronized(@{}) -$newRunspace =[runspacefactory]::CreateRunspace() -$newRunspace.ApartmentState ="STA" -$newRunspace.ThreadOptions ="ReuseThread" -$newRunspace.Open() -$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) ########################### @@ -32,9 +26,6 @@ $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) # $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 Add-Type -AssemblyName System.Drawing @@ -400,13 +391,9 @@ $FMain.Text = "SharePoint Utils GUI" #Onglet Move ##Lister les sites dans les comboBoxes $BlistSites.Add_Click({ - $newRunspace =[runspacefactory]::CreateRunspace() - $newRunspace.ApartmentState ="STA" - $newRunspace.ThreadOptions ="ReuseThread" - $newRunspace.Open() - $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) - $BlistSitesRunSpace = [PowerShell]::Create().AddScript({ + $jobListSP = Start-Job -ScriptBlock { + # Votre fonction ici #On vide la variable pnpConnection pour ne pas avoir de faux positifs $pnpConnection=$Null @@ -434,10 +421,15 @@ $BlistSites.Add_Click({ $LresultsMove.Items.Clear() $LresultsMove.Items.Add("Connectez-vous au tenant d'abord") } - }) - $BlistSitesRunSpace.PowerShell.Runspace = $newRunspace - $BlistSitesRunSpace.Thread = $BlistSitesRunSpace.PowerShell.BeginInvoke() + } + if ($jobListSP.State -eq "Completed") { + $resultListSP = Receive-Job -Job $jobListSP + $button.Invoke({ + $button.Text = "Terminé" + $textBox.Text = $resultListSP + }) + } }) ##Bouger les dossiers @@ -455,11 +447,6 @@ $BlistSites.Add_Click({ $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 @@ -471,8 +458,6 @@ $Bgo.Add_Click({ $jobStatus = Receive-PnPCopyMoveJobStatus -Job $jobCopyPending -Wait Write-Output $jobStatus }) - $copySPRunspace.PowerShell.Runspace = $newRunspace - $copySPRunspace.Thread = $copySPRunspace.PowerShell.BeginInvoke() }) @@ -485,14 +470,8 @@ $tenantURL = $URLTenant.Text $BconnectSP.Add_Click({ Disconnect-PnPOnline - $newRunspace =[runspacefactory]::CreateRunspace() - $newRunspace.ApartmentState ="STA" - $newRunspace.ThreadOptions ="ReuseThread" - $newRunspace.Open() - $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash) - $connectToSPRunspace.PowerShell = [PowerShell]::Create().AddScript({ - Connect-PnPOnline -Url $tenantURL -Interactive | Out-String + Connect-PnPOnline -Url $tenantURL -Interactive | Out-String Switch ($ERROR) { @@ -513,9 +492,7 @@ $BconnectSP.Add_Click({ $label4.ForeColor = [System.Drawing.Color]::Green } } - }) - $connectToSPRunspace.PowerShell.Runspace = $newRunspace - $connectToSPRunspace.Thread = $connectToSPRunspace.PowerShell.BeginInvoke() + }) @@ -540,8 +517,3 @@ $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 From 66bd96e180570782eaed296fc2fd576b05c1878e Mon Sep 17 00:00:00 2001 From: Kawawete Date: Wed, 13 Mar 2024 23:24:47 +0100 Subject: [PATCH 2/2] Stuff --- Sharepoint/gui/spmover/sputils.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/Sharepoint/gui/spmover/sputils.ps1 b/Sharepoint/gui/spmover/sputils.ps1 index a1d4fb2..d8970f5 100644 --- a/Sharepoint/gui/spmover/sputils.ps1 +++ b/Sharepoint/gui/spmover/sputils.ps1 @@ -393,7 +393,6 @@ $FMain.Text = "SharePoint Utils GUI" $BlistSites.Add_Click({ $jobListSP = Start-Job -ScriptBlock { - # Votre fonction ici #On vide la variable pnpConnection pour ne pas avoir de faux positifs $pnpConnection=$Null