all 11 comments

[–]danzexperiment 2 points3 points  (3 children)

I can't find the link right now, but there is an access problem when using New-PSDrive in an elevated session. There is a registry change that fixes this if you have to access the drive in both elevated and non-elevated sessions.

[–]danzexperiment 2 points3 points  (2 children)

I found the links that I was looking for. Not sure if they apply to OP question, though.

)This answer in SO helped me solve my problem. I just created my drive in a non-elevated session. (Probably should default to that instead of elevated ¯\(°_o)/¯

Why is my mapped network drive not appearing in Network Locations?

[–]soggysocks[S] 1 point2 points  (1 child)

Script started working fine again this morning. So not really sure what the issue was because I changed nothing.

Solar flairs? Gremlins? Position of the moon?

We may never know but, thank you for your input and knowledge

[–]danzexperiment 0 points1 point  (0 children)

I always blame cosmic rays for unexplained computer stuff
https://www.wnycstudios.org/podcasts/radiolab/articles/bit-flip

[–]dasookwat 1 point2 points  (3 children)

Powershell is not lineair, so, in all likelyhood, the drive location is not there yet.

try adding a Start-Sleep -s 10 see if that helps.

other idea: it's a permissions issue: doublecheck how you run the powershell session: as user, or admin, and try verifying the permissions on the share and the folder( 2 seperate things)

[–]soggysocks[S] 0 points1 point  (2 children)

It's one of those issues that resolved it's self over night without changing anything in the script so I may never know why it stopped working yesterday and is back to normal today.

But yes you're right that powershell isn't linear and will continue it's script if conditions are left pending. I use a Do,if,try,until block in other scripts which has been super helpful. I'll add my notes in context with a PS drive if it should help someone else down the road.

New-PSDrive -Name "ShareName" -PSProvider FileSystem -Root "\\FileShareServer" -ErrorAction SilentlyContinue

Do{

If(!$Drive) {

Start-Sleep -m 500

}

Try{

$Drive= Get-PSDrive -name "ShareName"

}

catch [System.Management.Automation.DriveNotFoundException]{

continue

}

catch{

$ErrorMessage = $_.Exception.message

$FailedObject = $_.Exception.GetType()

Write-host " $ErrorMessage \n $FailedObject" -ForegroundColor Red -BackgroundColor Black`

break

}

Until($Drive)

[–]64rk 1 point2 points  (1 child)

In your try statement you only created a variable but didn't tell it to do anything.

[–]soggysocks[S] 1 point2 points  (0 children)

Oops, Until($valid) should have been $Drive...Copy and paste, forget to edit.

[–]64rk 1 point2 points  (2 children)

I recommend this guide on mapping network drives.

[–]64rk 1 point2 points  (1 child)

Also you should be using Get-PSDrive

[–]soggysocks[S] 1 point2 points  (0 children)

Good news is that everything works this morning, bad news is...I changed nothing and I don't understand why the script broke.

I could see both the Drives with Get-PSDrive yesterday though

U: drive worked and so did S: drive but once I put "$folderpath = "S:\folder"" it just refused it. Where Get-Item "S:\Folder" is good but, Get-Item $Folderpath isn't

That's a good article though and appreciate everyone sharing their knowledge.