0

I am developing a couple of PowerShell scripts to help speed up the process of migrating user data from an old workstation to a new one. Currently trying to make one to help with retrieving and then deploying mapped network drives and have hit a snagged with the deployment aspect. I am new to PowerShell and learning as I go along using the ISE to help spot some of the problem areas the script has. Here is a copy of what the script currently looks like and the error I am receiving when trying to run it on the machine.

# Import drive list.
$mappedDrives = Import-Csv C:\Users\########\Desktop\WIP_Scripts\MasterCopy\mappedDrives.csv
$mappedDrives | %{$_ -replace ":"}
foreach ($Name in $mappedDrives) {
    New-PSDrive -Name $Name.Name -PSProvider FileSystem -Root "ProviderName" -Persist -ErrorAction Continue 
}

Once I have it working Ill make the edits for where the import comes from. The errors I am currently receiving are:

New-PSDrive : Cannot process the drive name because the drive name contains one or more of 
the following characters that are not valid: ; ~ / \ . :
At C:\Users\#######\Desktop\WIP_Scripts\MasterCopy\ImportMappedDrives.ps1:8 char:5
+     New-PSDrive -Name $Name.Name -PSProvider FileSystem -Root "Provid ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-PSDrive], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.NewPSDriveCommand
 
New-PSDrive : Cannot process the drive name because the drive name contains one or more of 
the following characters that are not valid: ; ~ / \ . :
At C:\Users\#######\Desktop\WIP_Scripts\MasterCopy\ImportMappedDrives.ps1:8 char:5
+     New-PSDrive -Name $Name.Name -PSProvider FileSystem -Root "Provid ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-PSDrive], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.NewPSDriveCommand
 
New-PSDrive : Cannot process the drive name because the drive name contains one or more of 
the following characters that are not valid: ; ~ / \ . :
At C:\Users\#######\Desktop\WIP_Scripts\MasterCopy\ImportMappedDrives.ps1:8 char:5
+     New-PSDrive -Name $Name.Name -PSProvider FileSystem -Root "Provid ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-PSDrive], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.NewPSDriveCommand

For the script used to retrieve the drive information:

$mappedDrives = @()
$Name = Get-WmiObject -ClassName Win32_MappedLogicalDisk | Select Name, ProviderName
foreach ($Name in $Name) {
    if ($Name. ProviderName) {
        $mappedDrives += Select-Object Name, ProviderName -InputObject $Name
    }
}

$mappedDrives | Export-Csv mappedDrives.csv 

MappedDrives.csv Output

Also attached is what the mappeddrives.csv output looks like for the retrieval. I thought that the csv file may be causing the invalid character arguements since the Name found within the csv file includes the ":" character. Also I am a bit confused on whether or not it will be able to see the "ProviderName" within the csv file or if I need to declare it in order for the script to add it to its argument. Again I am extremely new to Powershell so lots of what I have written down is what I have found from this site, Microsoft, or other blogs/forums and trying to Frankenstein together a working script. Any feedback on how to improve or get this to work and/or why using another method would be better in this situation would be greatly appreciated.

###Revision 1###

Utilizing the new script provided by RetiredGeek

# Import drive list.

$CFSArgs = @{PropertyNames = "Name", "ProviderName"
             Delimiter = ','}
$MappedDrives = (Get-Content "G:\BEKDocs\Scripts\Test\mappedDrives.csv") | 
                 ConvertFrom-String @CFSArgs

for ($cnt = 1; $cnt -lt $MappedDrives.count; $cnt++) {
   $NPSDArgs = 
      @{Name        = $(($MappedDrives[$cnt].Name).Substring(0,1)) 
        PSProvider  = "FileSystem"
        Root        = "$($MappedDrives[$cnt].ProviderName)" 
        Persist     = $True
        ErrorAction = "Continue"
                }
        New-PSDrive @NPSDArgs
}

I am now receiving the following error:

New-PSDrive : When you use the Persist parameter, the root must be a file system location 
on a remote computer.
At C:\Users\######\Desktop\MasterCopy\Test2.ps1:16 char:9
+         New-PSDrive @NPSDArgs
+         ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (":PSDriveInfo) [New-PSDrive], NotSupported 
   Exception
    + FullyQualifiedErrorId : DriveRootNotNetworkPath,Microsoft.PowerShell.Commands.NewPSD 
   riveCommand

The two questions I have now are:

  1. Would it be more appropriate to use "Net use" instead of "New-PSDrive" for what I am trying to achieve(which is mapping a network drive to a computer using the cvs file created)?
  2. If the use of the New-PSDrive cmdlet is a non-issue how do I rectify the error the script is currently outputting?

Thanks again to RetiredGeek and Theo for your inputs.

1
  • Append switch -NoTypeInformation to the Export-Csv mappedDrives.csv. This will prevent the top line with field type description to be written that is causing the problem. Commented Jan 8, 2022 at 10:26

1 Answer 1

0

FT,

You need to evaluate your arguments in the New-PSDrive line. Using Substring there eliminates code and makes it more efficient. I had problems using Import-CSV so I switched to Get-Content and adjusted

# Import drive list.

$CFSArgs = @{PropertyNames = "Name", "ProviderName"
             Delimiter = ','}
$MappedDrives = (Get-Content "G:\BEKDocs\Scripts\Test\mappedDrives.csv") | 
                 ConvertFrom-String @CFSArgs

for ($cnt = 1; $cnt -lt $MappedDrives.count; $cnt++) {
   $NPSDArgs = 
      @{Name        = $(($MappedDrives[$cnt].Name).Substring(0,1)) 
        PSProvider  = "FileSystem"
        Root        = "$($MappedDrives[$cnt].ProviderName)" 
        Scope       = "Global"
        Persist     = $True
        ErrorAction = "Continue"
                }
        New-PSDrive @NPSDArgs -WhatIf
}

I used the -WhatIf parameter since I don't have your targets available but it showes what would have been done.

Output:

What if: Performing the operation "New drive" on target "Name: X Provider: Micro
soft.PowerShell.Core\FileSystem Root: \\hapi\desktop$\Decommission Log".
What if: Performing the operation "New drive" on target "Name: Y Provider: Micro
soft.PowerShell.Core\FileSystem Root: \\gonzo\Temp\AZ".
What if: Performing the operation "New drive" on target "Name: Z Provider: Micro
soft.PowerShell.Core\FileSystem Root: \\gonzo\Temp\001".

PS> 

Update 1: Further testing on my network (Peer-to-Peer) reveals that adding the Scope parameter (see above) will create the mapping, even though you get the same message, and it will last until you Reboot! It does NOT however persist after the reboot so it is not doing what it should. I still don't understand why it the message is displayed as the root is on another computer. Also, the mapping doesn't show in File Explorer although I can open a command prompt and successfully do a DIR on the drive.

Update 2: I tried another test mapping to my Synology NAS and it worked w/o the error message. But, alas it did NOT persist a reboot!

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your input RG. When I ran using your code that was given (removing the -WhatIf parameter and changing the filepath in $mappedDrives) I get the error "New-PSDrive : When you use the Persist parameter, the root must be a file system location on a remote computer" at the New-PSDrive @NPSDArgs line. The question I now want to ask is if the New-PSDrive cmdlet is appropriate for what I am trying to do (mapping a network drive) or should I switch commands to Net use? If so, how would I go about modifying the script to utilizes Net Use?
Foxtrot360, see revised answer.
Added the scope parameter however still receiving the same error. Ran with the -WhatIf parameter and did return something interesting: ((What if: Performing the operation "New drive" on target "Name: " Provider: Microsoft.PowerShell.Core\FileSystem Root: "\\gonzo\temp"".)) It seems that it is not picking up the drive letter "Name" from the csv file created. Could that be causing the error with the persist parameter?
Also, can dot sourcing the script help fix the issue of it not persisting after reboot?
Foxtrot360, sorry to say, but I think I'm in a bit over my pay grade! Hopefully, someone else with more knowledge will chine in. Good Luck!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.