Pour monter un lecteur réseau avec powershell en tilisant un crédentiel.
$pwdFilePath = "C:\pwdMount.txt"
$serveurFichier = "\\serveur\partage"
$lettreLecteur = "N"
$labelLecteur = "monPartage"
if (!(Test-Path -path $pwdFilePath)) {
read-host -AsSecurestring -prompt 'Mot de passe à chiffrer'|ConvertFrom-SecureString | out-file $pwdFilePath
}
$username = 'login'
$password = Get-Content $pwdFilePath | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $password
# Montage du lecteur réseau
If((Test-Path -Path "$lettreLecteur:\") -eq $False)
{
Get-PSDrive|Out-Null
New-PSDrive -Name "$lettreLecteur" -Root "$serveurFichier" -Persist -PSProvider "FileSystem" -Credential $cred -Scope Global |Out-Null
(New-Object -ComObject Shell.Application).NameSpace("$lettreLecteur:\").Self.Name = '$labelLecteur'
}
If((Test-Path -Path "$lettreLecteur:\") -eq $False)
{
#Set-content -Path C:\error.txt -Value "Erreur de montage du partage"
Write-Output "Erreur de montage du partage"
exit
}