0

My Powershell script assumes that the user running the script has access to a particular remote share on a fileserver. If they don't, I want to prompt the user to "map a network drive" first prior to running the script.

How would I do that?

2 Answers 2

0

Not sure if I understood the question correctly, but I am assuming here that we are checking here if user has required share mapped, not if user has access to required share.


I realize that this is not exactly answering your question but I would suggest a different approach.

You could try mapping the share in your script using New-PsDrive cmdlet and if needed prompting user for credential with Get-Credential

New-PSDrive -Name TestName -PSProvider FileSystem -Root \\fileshare.local\share -Credential (Get-Credential)

At the end of your script detach from it using Remove-PsDrive cmdlet.

Mapped Share will only be visible in the script context (won't appear in Windows Explorer).


Finally, you could also use this method to check if share is mapped

    If(Get-PSDrive | Where-Object DisplayRoot -EQ '\\fileshare.local\share'){ "Mappped" } else { "Nope" }
0

Here's PowerShell code that will do what you're looking for.

$connected = $false
try {
  if ((get-item \\mycomp1\myshare).Name -eq "myshare") { $connected = $true }
} catch {
}
if (-not($connected)) {
  echo "Please map network drive to \\mycomp1\myshare using your domain credentials, and try again"
  exit
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .