Category Archives: PowerShell

Simple Powershell script connect to servers as a different user

A common practice in IT is to have a separate admin account to connect to servers.

Often in day to day administration of SharePoint servers, it’s necessary to connect to the c$ or d$ share to look at a log file, copy an installer, etc…

You can do this from windows, and it will usually prompt you for credentials, but that can be a pain if you regularly connect to a bunch of machines that need different credentials.

This script will prompt you for a password, then use that password along with a pre-defined user account and server list to connect you to each server in advance.

function mapdrives
{
   #Update these variables for your environment:
   $account = "domain\useraccout"
   $serverlist = @("Server1", "Server2", "Server3", "Etc...")
 
   $SecurePwd = read-host -assecurestring "Enter password for $account"
   $pwd = ConvertTo-PlainText($SecurePwd)
 
   foreach ($Server in $ServerList)
     {
        net use \\$server /d
        net use \\$Server /user:$account $pwd
     }
   write-host "Done mapping drives"  
}
 
# This function came from Oisin Grehan, MVP 
# via:  http://www.vistax64.com/powershell/159190-read-host-assecurestring-problem.html
Function ConvertTo-PlainText( [security.securestring]$secure ) 
{
   $marshal = [Runtime.InteropServices.Marshal]
   $marshal::PtrToStringAuto( $marshal::SecureStringToBSTR($secure) )
}
 
mapdrives