Script/Task to map share to drive letter, then start services

MrVault
MrVault used Ask the Experts™
on
Anyone know of a way to tell Windows to make sure a mapped drive exists for a share before starting certain services? If it's there, start them in a certain order. If not, map the drive first, then start them. If it ever fails try again and then if fails again, send an email and log an eventlog entry?

Our application requires a consistent path and everything is based on a drive letter.

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Network Manager
Commented:
This doesn't sound like a service since there would be no drive mapping when the service would be running while nobody logged onto the server it is running on.

This should get you close.

$driveletter = "z:"
$uncpath = "\\server\share"
$net = New-Object -com WScript.Network
$a = $net.EnumNetworkDrives()
$anz = $a.count()

    
for ($i = 0; $i -lt $anz; $i++) 
{
    $drive = $a.item($i)
    $path = $a.item($i+1)
    $z = $driveletter.CompareTo($drive)
    $x = $uncpath.CompareTo($path)
    $i = $i + 1
        If ($z -eq 0 -and $x -eq 0)
        {
            #run your program
        }
}
Map-Drive $driveletter $uncpath

#run your program

Function Map-Drive ([string]$dl, [string]$unc)
{
    $net = New-Object -com WScript.Network
    if ($dl.length -eq 1) { $dl = $dl +":" }
    $net.mapnetworkdrive($dl, $unc)
}

Open in new window


based off code found here.
http://pauerschell.blogspot.com/2010/03/powershell-and-network-drives.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial