Tried to integrate Threading, as by default, a lot of functionnality will hang the GUI

This commit is contained in:
Kawawete 2024-03-13 17:41:33 +01:00
parent a66fb59b6e
commit a42c3bf5ef
2 changed files with 143 additions and 65 deletions

View File

@ -0,0 +1,11 @@
{
"configurations": [
{
"name": "PowerShell: Launch Script",
"type": "PowerShell",
"request": "launch",
"script": "${workspaceFolder}\\sputils.ps1",
"args": []
}
]
}

View File

@ -12,13 +12,28 @@
# Source : C:\Users\SebastienQUEROL\source\repos\sharepointmover-gui\Form1.Designer.cs # Source : C:\Users\SebastienQUEROL\source\repos\sharepointmover-gui\Form1.Designer.cs
################################################################################ ################################################################################
function Get-ScriptDirectory ###############Thread Setup
{ #Return the directory name of this script
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$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 # Chargement des assemblies externes
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Windows.Forms
@ -209,7 +224,7 @@ $BconnectSP.UseVisualStyleBackColor = $true
# #
$URLTenant.Location = New-Object System.Drawing.Point(22, 55) $URLTenant.Location = New-Object System.Drawing.Point(22, 55)
$URLTenant.Name = "URLTenant" $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.Size = New-Object System.Drawing.Size(325, 20)
$URLTenant.TabIndex = 1 $URLTenant.TabIndex = 1
# #
@ -364,7 +379,7 @@ $tabPage5.UseVisualStyleBackColor = $true
$LresultsMove.HideSelection = $false $LresultsMove.HideSelection = $false
$LresultsMove.Location = New-Object System.Drawing.Point(8, 173) $LresultsMove.Location = New-Object System.Drawing.Point(8, 173)
$LresultsMove.Name = "LresultsMove" $LresultsMove.Name = "LresultsMove"
$LresultsMove.FullRowSelect = true #$LresultsMove.FullRowSelect = true
$LresultsMove.Size = New-Object System.Drawing.Size(776, 190) $LresultsMove.Size = New-Object System.Drawing.Size(776, 190)
$LresultsMove.TabIndex = 7 $LresultsMove.TabIndex = 7
$LresultsMove.AutoSize = $true $LresultsMove.AutoSize = $true
@ -385,6 +400,14 @@ $FMain.Text = "SharePoint Utils GUI"
#Onglet Move #Onglet Move
##Lister les sites dans les comboBoxes ##Lister les sites dans les comboBoxes
$BlistSites.Add_Click({ $BlistSites.Add_Click({
$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 #On vide la variable pnpConnection pour ne pas avoir de faux positifs
$pnpConnection=$Null $pnpConnection=$Null
#On stocke la derniere connection PnP #On stocke la derniere connection PnP
@ -394,7 +417,7 @@ $BlistSites.Add_Click({
$CBdestSite.Items.Clear() $CBdestSite.Items.Clear()
$CBsourceSite.Items.Clear() $CBsourceSite.Items.Clear()
$sitesList = $null $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 $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() $LresultsMove.Items.Clear()
foreach($siteUrl in $sitesList){ foreach($siteUrl in $sitesList){
$siteUrl -match "@{Url=(?<content>.*)}" $siteUrl -match "@{Url=(?<content>.*)}"
@ -411,28 +434,65 @@ $BlistSites.Add_Click({
$LresultsMove.Items.Clear() $LresultsMove.Items.Clear()
$LresultsMove.Items.Add("Connectez-vous au tenant d'abord") $LresultsMove.Items.Add("Connectez-vous au tenant d'abord")
} }
})
$BlistSitesRunSpace.PowerShell.Runspace = $newRunspace
$BlistSitesRunSpace.Thread = $BlistSitesRunSpace.PowerShell.BeginInvoke()
}) })
##Bouger les dossiers ##Bouger les dossiers
# $copyIt = {
# param ($sourceSite,$destSite)
# $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({ $Bgo.Add_Click({
$sourceSite = $CBsourceSite.Text
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState ="STA"
$newRunspace.ThreadOptions ="ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$copySPRunspace.PowerShell = [PowerShell]::Create().AddScript({
$destSite = $CBdestSite.Text $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]+)+)',''
$jobMovePending = Copy-PnPFile -SourceUrl $sourceSite -TargetUrl $destSite -Force -OverwriteIfAlreadyExists $jobCopyPending = Copy-PnPFile -SourceUrl "$sourceSite" -TargetUrl "$destSite" -Force -OverwriteIfAlreadyExists
$jobStatus = Receive-PnPCopyMoveJobStatus -Job $jobMovePending $jobStatus = Receive-PnPCopyMoveJobStatus -Job $jobCopyPending -Wait
Write-Host $jobStatus Write-Output $jobStatus
})
$copySPRunspace.PowerShell.Runspace = $newRunspace
$copySPRunspace.Thread = $copySPRunspace.PowerShell.BeginInvoke()
}) })
#Onglet Parametres #Onglet Parametres
##Connexion Sharepoint ##Connexion Sharepoint
$BconnectSP.Add_Click({ $pnpConnection=$null
$pnpConnection = Get-PnPConnection | Out-string
$tenantURL = $URLTenant.Text
$pnpConnection=$null $BconnectSP.Add_Click({
$pnpConnection = Get-PnPConnection | Out-string Disconnect-PnPOnline
$tenantURL = $URLTenant.Text $newRunspace =[runspacefactory]::CreateRunspace()
$connResults = Connect-PnPOnline -Url $tenantURL -Interactive 2>&1 | Out-String $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
Switch ($ERROR) Switch ($ERROR)
{ {
@ -453,8 +513,10 @@ $BconnectSP.Add_Click({
$label4.ForeColor = [System.Drawing.Color]::Green $label4.ForeColor = [System.Drawing.Color]::Green
} }
} }
})
}) $connectToSPRunspace.PowerShell.Runspace = $newRunspace
$connectToSPRunspace.Thread = $connectToSPRunspace.PowerShell.BeginInvoke()
})
function OnFormClosing_FMain{ function OnFormClosing_FMain{
@ -478,3 +540,8 @@ $FMain.Add_Shown({$FMain.Activate()})
$ModalResult=$FMain.ShowDialog() $ModalResult=$FMain.ShowDialog()
# Libération de la Form # Libération de la Form
$FMain.Dispose() $FMain.Dispose()
})
$MainWindowGUI.Runspace = $newRunspace
$data = $MainWindowGUI.BeginInvoke()