List sites now works; Connection label now fully works; The URL field auto-populates with the last know valid PnPOnline session.

This commit is contained in:
Kawawete 2024-03-12 17:05:25 +01:00
parent 993825bea5
commit f120caa0e2
1 changed files with 53 additions and 20 deletions

View File

@ -315,8 +315,19 @@ $label4.Location = New-Object System.Drawing.Point(380, 32)
$label4.Name = "label4"
$label4.Size = New-Object System.Drawing.Size(66, 13)
$label4.TabIndex = 3
$label4.Text = "Déconnecté"
$label4.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
##On modifie le texte du label en fonction de l'état initial de la connection a PNPOnline
$pnpConnection = Get-PnPConnection
if(-not ([string]::IsNullOrEmpty($pnpConnection))){
$pnpConnection -match "@{Url=(?<content>.*)}"
$pnpConnectionUrl = $matches["content"]
$label4.Text = "Connecté"
$label4.ForeColor = [System.Drawing.Color]::Green
$URLTenant.Text = $pnpConnectionUrl
}else{
$label4.Text = "Déconnecté"
$label4.ForeColor = [System.Drawing.Color]::MediumVioletRed
}
#
# tabPage3
#
@ -371,46 +382,64 @@ $FMain.Text = "SharePoint Utils GUI"
#Onglet Move
##Lister les sites dans les comboBoxes
$BlistSites.Add_Click({
#On vide la variable lastError pour ne pas avoir de faux positifs
$lastError=''
#On stocke la derniere erreur
$lastError=$ERROR[0]
#On vide la variable pnpConnection pour ne pas avoir de faux positifs
$pnpConnection=$Null
#On stocke la derniere connection PnP
$pnpConnection = Get-PnPConnection
#A ameliorer, mais pour l'instant, ca devrait passer
if([string]::IsNullOrEmpty($lastError)){
$sitesList=@()
$sitesList = Get-SPOSite | Select url
$CBsourceSite.Items.AddRange($sitesList)
$CBdestSite.Items.AddRange($sitesList)
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=(?<content>.*)}"
$siteUrlClean = $matches["content"]
$CBsourceSite.Items.Add([string]$siteUrlClean)
$CBdestSite.Items.Add([string]$siteUrlClean)
}
}
elseif(-not ([string]::IsNullOrEmpty($lastError))){
$sitesListError=@('Circulez','Ya rien a voir')
$CBsourceSite.Items.AddRange($sitesListError)
$CBdestSite.Items.AddRange($sitesListError)}
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")
}
})
##Bouger les dossiers
$Bgo.Add_Click({
$sourceSite = $CBsourceSite.Text
$LresultsMove.Items.Add($sourceSite)
})
#Onglet Parametres
##Connexion Sharepoint
$BconnectSP.Add_Click({
$pnpConnection=$null
$pnpConnection = Get-PnPConnection
$tenantURL = $URLTenant.Text
Connect-PnPOnline -Url $tenantURL -Interactive 2>&1 | Out-String
$connResults = Connect-PnPOnline -Url $tenantURL -Interactive 2>&1 | 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 {
Default {
$label4.Text = "Connecté"
$label4.ForeColor = [System.Drawing.Color]::Green
}
@ -430,7 +459,11 @@ function OnFormClosing_FMain{
($_).Cancel= $False
}
$FMain.Add_FormClosing( { OnFormClosing_FMain} )
$FMain.Add_FormClosing({
#Disconnect-PnPOnline
Write-Host "Goodbye, come again soon :*"
OnFormClosing_FMain
})
$FMain.Add_Shown({$FMain.Activate()})
$ModalResult=$FMain.ShowDialog()