Ken Haynes
Ken Haynes

Reputation: 21

New-PSDrive works interactively but not in PowerShell script

I am attempting to use a New-PSDrive to mount a share and then read items from it.

function NRLFind ([object[]]$Releases) {
    $relvols = @{}

    New-PSDrive -Name NRL -PSProvider FileSystem -Root "\\FileServer\shareName" 
    foreach ($dir in $(Get-Item NRL:\* | where {$_.PsIsContainer})) {
        foreach($release in $Releases) {
            if ($d = get-item $($dir.FullName + '\' + $release.glob) -ErrorAction:SilentlyContinue) { 
              if(! $relvols[$release.name] -or $relvols[$release.name].LastWriteTime -lt $d.LastWriteTime) {
                $relvols[$release.name] = $d
              }
            }
        }
    }
    $relvols
}

It's bombing on the New-PSDrive line, with the error: New-PSDrive : Drive root "\FileServer\shareName" does not exist or it's not a folder.

The interesting part is that the New-PSDrive command works when run interactively, but not in the PS script.

Any ideas?

Upvotes: 0

Views: 1144

Answers (1)

Shay Levy
Shay Levy

Reputation: 126912

Make sure the user who runs the script has permissions to access the share.

Upvotes: 0

Related Questions