Recently my CFO purchased a new computer for his home office. He wanted to be able to VPN into the office and have access to all of his network shares and applications. After installing all his applications I was trying to decide on how to go about mapping his network drives after he logged onto the VPN. I could have used a plain old batch file but once again I decided I could do a better job using PowerShell.
So, I started researching what I wanted to do.
Since his home PC was not a part of the office domain he would need to authenticate before he could access those network shares. So, I decided to use the cmdlet Get-Credential to prompt him for his username and password.
Then I started working with the cmdlet New-PSDrive which did a good job of mapping a drive and allowed the credentials to be passed in. However, I quickly discovered that using that cmdlet only allows access to that mapped drive within that PowerShell instance. This was a deal breaker. I wanted to have him run the PowerShell Script and have access to the drives outside of powershell. So I did some more searching and found I could use the following code to make this work.
$creds=Get-Credential
$map=New-Object-ComObject WScript.Network
$map.MapNetworkDrive(“H:“,“\\SVR1\Users“,0,$creds.UserName,$creds.GetNetworkCredential().Password)
$map.MapNetworkDrive(“I:“,“\\SVR1\Admin“,0)
$map.MapNetworkDrive(“M:“,“\\SVR1\Data“,0)
$map.MapNetworkDrive(“N:“,“\\SVR1\Software“,0)
This script works fine for my basic purposes for now. But I have ideas to expand upon it and maybe use a foreach loop to reduce the amount of lines of code.
Filed under: PowerShell, System Admin, Windows Server Tagged: Login Script, Powershell, VPN
