Powershell script to log into NAS and map all accessible network shares as local drives in Explorer

Status
Not open for further replies.

KatzWo

Member
I apologize if this is the wrong place to ask. I'm building a new image. Everything works perfectly except for mapping the NAS and its accessible - to the user - network shares as local drives in Windows Explorer.

This is a non-generic script that works:

Code:
cmdkey /add:pcname /user:network\username /pass:password
New-PSDrive -Name "E" -Root "\\vm-beta\ShareOne" -Persist -PSProvider "FileSystem"

1st line of code: logs the user (with his NAS account) into the NAS
2st line: maps the given network share - that he can access with write/read rights - as a local drive in File Explorer

The problem with this script is that I need to generalize the script in that it should ask the user to log into the NAS with their credentials and after that 'detect' which network shares his NAS account has access to and then map all of those as local drives in File Explorer

To solve this issue, so far I've re-written the script to this to no avail:

Code:
# Prompt user for NAS credentials and save in Credential Manager
$credential = Get-Credential -Message "Enter NAS Login Credentials"
cmdkey /add:NAS /user:$($credential.Username) /pass:$($credential.Password)

# Get all network shares on the NAS
$networkShares = Get-SmbShare -CimSession NAS

# Map all network shares that the given user has access to as drives in Windows File Explorer
foreach ($share in $networkShares) {
    $access = Get-SmbShareAccess -CimSession NAS -Name $share.Name -Credential $credential
    foreach ($user in $access.AccountName) {
        if ($user -eq $($credential.UserName)) {
            New-PSDrive -Name $share.Name -Root $share.Path -Persist -PSProvider "FileSystem" -Credential $credential
            break
        }
    }
}

I'm at my wits end. Nothing seems to work. Is this even possible?

Once again I apologize if this isn't the right place to ask. I just want help.
 
Once again I apologize if this isn't the right place to ask.
Correct, this is not the right place to ask, its for NTLite and related subjects, not a scripting forum and seeing as you are also asking for help over at 10forums
Captvvure.JPG
i am closing this thread.
 
Status
Not open for further replies.
Back
Top