Hello there,
I’m trying to retrieve credential from an xml file (store encrypted credential) to map a drive when windows start.
But the “# Mapping P Drive” seems not working i tried with different cmdlet and other variable but i haven’t find the right one.
# Create script for mapping P Drive
$mapDriveScript = @"
# Reading XML File for getting the credential
\$credential = Import-Clixml -Path "$xmlFilePath"
# Mapping P Drive
New-PSDrive -Name "P" -PSProvider "FileSystem" -Root "\\serveur\partage" -Credential \$credential -Persist -scope Global
"@
Here the full script for the context
# Define Variables
$username = "domain\username"
$password = ConvertTo-SecureString "password" -AsPlainText -Force
# Define the folder path
$secureFolderPath = "C:\NetworkSharedFolder"
$xmlFilePath = "$secureFolderPath\credentials.xml"
"$secureFolderPath\MapProductionDrive.ps1"
# folder creation
if (-not (Test-Path -Path $secureFolderPath)) {
New-Item -Path $secureFolderPath -ItemType Directory
}
# Hide the folder
$folder = Get-Item -Path $secureFolderPath
$folder.Attributes = 'Hidden'
# XML File generation
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
$credential | Export-Clixml -Path "$secureFolderPath\credentials.xml"
# Define security for the XML file
$acl = Get-Acl -Path "$secureFolderPath\credentials.xml"
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Users", "Read", "Allow")
$acl.SetAccessRule($rule)
Set-Acl -Path "$secureFolderPath\credentials.xml" -AclObject $acl
# Create script for mapping P Drive
$mapDriveScript = @"
# Reading XML File for getting the credential
\$credential = Import-Clixml -Path "$xmlFilePath"
# Mapping P Drive
New-PSDrive -Name "P" -PSProvider "FileSystem" -Root "\\serveur\partage" -Credential \$credential -Persist -scope Global
"@
# saving file configuration in the .ps1
$mapDriveScript | Out-File -FilePath $ps1FilePath
# Define permission for user for the .ps1
$acl = Get-Acl -Path $ps1FilePath
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Users", "ReadAndExecute", "Allow")
$acl.SetAccessRule($rule)
Set-Acl -Path $ps1FilePath -AclObject $acl
# Windows Task Scheduler creation
$taskName = "MapProductionDrive"
$taskDescription = "Map network drive at startup using stored credentials"
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File `"$ps1FilePath`""
$trigger = New-ScheduledTaskTrigger -AtLogOn
$principal = New-ScheduledTaskPrincipal -UserId "Users" -LogonType Group
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
Register-ScheduledTask -TaskName $taskName -Description $taskDescription -Action $action -Trigger $trigger -Principal $principal -Settings $settings
The remaining script is functional, but when it try to map drive (depending how i edit the script) sometime nothing happen, sometime i have a windows 95 windows which ask me credential.
Can you help me guy please ? Thank you
I’m not a devs and i scripted this to set up a share in a production environnement.