Lorsqu’on commence à gérer un Tenant Office 365, quelque soit le module à gérer, PowerShell est le seul outil à maîtriser absolument.
En revanche, il devient très rapidement pénible de taper le mot de passe du compte Office 365 à chaque exécution du script, et surtout impossible de scheduler un script avec ce concept. On ne va pas pour autant stocker son mot de passe en clair dans un fichier texte de la machine.
La solution est donc d’utiliser un script de génération de fichier avec le password crypté qui peut ensuite être utilisé dans son script
Voici donc un petit exemple de script:
[string]$username = "YourTenantAccount@yourtenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\FOLDERTOSTOREPWD\ExportedPWD-$($username).txt"Write-Host " >> Account used:", $username -Foreground "Green"
Read-Host -Prompt "Please enter password:" -AsSecureString | ConvertFrom-SecureString | Out-File $PwdTXTPath
Write-Host " ---------------------------------------------- "
Write-Host " >> Your SecuredString file is stored in", $PwdTXTPath
Write-Host " =>> You can use it in your script" -Foreground "Red"
Write-Host " ---------------------------------------------- "
Ce fichier qui sera stocké avec le path exact: “C:\FOLDERTOSTOREPWD\ExportedPWD-YourTenantAccount@yourtenant.onmicrosoft.com.txt” pourra être utilisé dans votre script de connexion comme suit:
[string]$username = "YourTenantAccount@yourtenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\FOLDERTOSTOREPWD\ExportedPWD-$($username).txt"$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList ($username , $secureStringPwd)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName,$creds.Password)
$ctx.RequestTimeout = 1000000 # milliseconds
$spoweb = $ctx.Web
$ctx.Load($spoweb)
$ctx.ExecuteQuery()Write-Host
Write-Host $ctx.Url -BackgroundColor White -ForegroundColor DarkGreen
Il ne vous reste plus qu’à mettre à jour ce fichier à chaque changement de mot de passe.
Liens utiles:
- https://blog.kloud.com.au/2016/04/21/using-saved-credentials-securely-in-powershell-scripts/
- http://www.adminarsenal.com/admin-arsenal-blog/secure-password-with-powershell-encrypting-credentials-part-1/
- http://www.virtualtothecore.com/en/encrypt-passwords-in-powershell-scripts/
- https://blog.kloud.com.au/2016/04/21/using-saved-credentials-securely-in-powershell-scripts/
Romelard Fabrice [MBA Risk Management]
Commentaires
Enregistrer un commentaire