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.Name = "label4"
$label4.Size = New-Object System.Drawing.Size(66, 13) $label4.Size = New-Object System.Drawing.Size(66, 13)
$label4.TabIndex = 3 $label4.TabIndex = 3
$label4.Text = "Déconnecté"
$label4.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter $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 # tabPage3
# #
@ -371,44 +382,62 @@ $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({
#On vide la variable lastError pour ne pas avoir de faux positifs #On vide la variable pnpConnection pour ne pas avoir de faux positifs
$lastError='' $pnpConnection=$Null
#On stocke la derniere erreur #On stocke la derniere connection PnP
$lastError=$ERROR[0] $pnpConnection = Get-PnPConnection
#A ameliorer, mais pour l'instant, ca devrait passer #A ameliorer, mais pour l'instant, ca devrait passer
if([string]::IsNullOrEmpty($lastError)){ if(-not ([string]::IsNullOrEmpty($pnpConnection))){
$sitesList=@() $CBdestSite.Items.Clear()
$sitesList = Get-SPOSite | Select url $CBsourceSite.Items.Clear()
$CBsourceSite.Items.AddRange($sitesList) $sitesList = $null
$CBdestSite.Items.AddRange($sitesList) $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(([string]::IsNullOrEmpty($pnpConnection))){
$LresultsMove.Items.Clear()
$LresultsMove.Items.Add("Connectez-vous au tenant d'abord")
} }
elseif(-not ([string]::IsNullOrEmpty($lastError))){
$sitesListError=@('Circulez','Ya rien a voir')
$CBsourceSite.Items.AddRange($sitesListError)
$CBdestSite.Items.AddRange($sitesListError)}
else{ else{
$LresultsMove.Items.Clear()
$LresultsMove.Items.Add("Connectez-vous au tenant d'abord") $LresultsMove.Items.Add("Connectez-vous au tenant d'abord")
} }
}) })
##Bouger les dossiers
$Bgo.Add_Click({
$sourceSite = $CBsourceSite.Text
$LresultsMove.Items.Add($sourceSite)
})
#Onglet Parametres #Onglet Parametres
##Connexion Sharepoint ##Connexion Sharepoint
$BconnectSP.Add_Click({ $BconnectSP.Add_Click({
$pnpConnection=$null
$pnpConnection = Get-PnPConnection
$tenantURL = $URLTenant.Text $tenantURL = $URLTenant.Text
Connect-PnPOnline -Url $tenantURL -Interactive 2>&1 | Out-String $connResults = Connect-PnPOnline -Url $tenantURL -Interactive 2>&1 | Out-String
Switch ($ERROR) Switch ($ERROR)
{ {
{$ERROR[0] -match "Host not reachable" }{ {$ERROR[0] -match "Host not reachable" }{
$label4.Text = "Injoignable" $label4.Text = "Injoignable"
$label4.ForeColor = [System.Drawing.Color]::MediumVioletRed $label4.ForeColor = [System.Drawing.Color]::MediumVioletRed
} Break}
{$ERROR[0] -match "User canceled authentication."}{ {$ERROR[0] -match "User canceled authentication."}{
$label4.Text = "Annulé" $label4.Text = "Annulé"
$label4.ForeColor = [System.Drawing.Color]::MediumVioletRed $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.Text = "Connecté"
@ -430,7 +459,11 @@ function OnFormClosing_FMain{
($_).Cancel= $False ($_).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()}) $FMain.Add_Shown({$FMain.Activate()})
$ModalResult=$FMain.ShowDialog() $ModalResult=$FMain.ShowDialog()