Error mapping/unmapping shares to multiple systems RRS feed

  • Question

  • So... odd problem.  I'm trying to run a script that maps a drive to a UNC path \\servername\c$ and then check for the existence of a path.  Fairly straightforward, but I'm getting a weird error.  If I have up to four or five servers in my "serverlist.txt" file, then the script works fine.  Any more than that and I get the following:

    Get-ChildItem : Cannot find drive. A drive with the name 'z' does not exist.
    At D:\Scripts\Powershell\prog_test.ps1:10 char:4
    + dir <<<<  "z:\program files\PROGRAM\"
        + CategoryInfo          : ObjectNotFound: (z:String) [Get-ChildItem], DriveNotFoundExcepti
        + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

    Once that starts to happen, I can no longer access z:. If I try to map a drive from the powershell command line, by using "net use z: \\system\path" for instance, I get "the comment completed successfully", but when I type z: I get the same error as above. 

    The z: drive does show up in My Computer and can be accessed through there and through the regular shell, just not through PS.

    Additionally, I can map a drive to another letter and it works.

    Here's the script:

    ForEach ($system in Get-Content "systemlist.txt")
    $drive = $(New-Object -Com WScript.Network)
    $drive.MapNetworkDrive("z:", "\\$system\c$")

    write-output "Connecting to $system"

    if(test-path -path "z:\program files\PROGRAM"){echo $system >> positive_results.txt}

    if(!(test-path -path "z:\program files\PROGRAM")){echo $system >> negative_results.txt}

    write-output -message "Disconnecting from $system"



    Like I said, pretty simple and straightforward.

    Also, "PROGRAM" above is not the actual line in the script, I just replaced it here as it's not relevant what the actual path is.
    Monday, September 17, 2012 11:40 PM


  • It seems as if removenetworkdrive is not completing before the mapnetworkdrive executes the next time through the loop. Try putting in a delay, or testing for the existence of "Z:\" in a loop like so:


    while ( test-path "Z:\")


            write-host "." -nonewline

            start-sleep -milliseconds 100



    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    Tuesday, September 18, 2012 2:51 AM