Jump to content
eDecisions

Perform a task as the logged in user Map drive ect..

Recommended Posts

This has been bugging the h ell out of me, I need to map a drive, or run a program that can only be run by the logged in user, and if I map the drive from session 0 the user does not see it.

The below script works but wish Pulseway had better run as user features built in. This script will map a drive letter for the logged in user, change server and share name to your info. you can change the /TR "any commoand you want to run as the user"

 


$Taskname = "MapX"
$lastlogon = (New-Object -ComObject WScript.Shell).RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\LastLoggedOnUser")

SCHTASKS /CREATE /sc ONCE /st 00:00 /TN $Taskname /RU $lastlogon /TR "net use x: \\<server-name>\<share-name> /persistent:yes" /F
start-sleep -s 4
schtasks /Run /TN $Taskname
start-sleep -s 1

schtasks /delete /tn $Taskname

 

Share this post


Link to post
Share on other sites

with everything you want more... needed a quick way to change path and drive letter depending on which customer I was working at, so came up with this, make a new file called MapDrv.ps1 and paste the MapDrv.ps1 info into it, place it using your method to a common working directory on every compute you manage, then type the following from powershell window in pulseway for the client our working on:     MapDrv.ps1 S: \\server\path

so this way, you can just on the fly enter the drive letter and path and mapdrive.ps1 will map for the user, no need to make a script for every customer and allows for quick re-connection of a missing drive letter for a user.

 

 

#MapDrv.ps1

$drive=$args[0]
$path=$args[1]
$Taskname = "MapX"
$lastlogon = (New-Object -ComObject WScript.Shell).RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\LastLoggedOnUser")SCHTASKS /CREATE /sc ONCE /st 00:00 /TN $Taskname /RU $lastlogon /TR "net use $drive $path /persistent:yes" /F
start-sleep -s 4
schtasks /Run /TN $Taskname
start-sleep -s 1
schtasks /delete /tn $Taskname /F

 

Share this post


Link to post
Share on other sites

When I run this I get these errors:

ERROR: No mapping between account names and security IDs was done. ERROR: The system cannot find the file specified. ERROR: The specified task name "MapX" does not exist in the system.

Share this post


Link to post
Share on other sites

its not expected to run as a stand alone script, I set this so I could run from the powershell instance, so you'd save the script above into a working folder on the client, could could call it anything you like with a ps1

Then from the power shell window you can type c:\mypath\myname.ps1 x: \\server\share

were taking the %1 and %2 as $args0 and $args1, so you're completing the 2 items when you manually type the command in the powershell windows.

 

hope that makes sense.

Share this post


Link to post
Share on other sites

What I started doing was creating Script Groups, hence every customer has their own set of scripts, however the point of this one was a little different, imagine you just want to map a drive for today between 2 computers at the office for the client, this works. If you'd like to get rid of the copy file first, conciser building the file first in powershell with the commands, and then call that file, and delete it after run. I did that with the "SHOW MAPPED DRIVES OF LOGGED IN USER" that I just posted, take a peek of how I wrote a local batch file and then ran it and deleted it.

Script Group

Specific customer 1

      >Map drives

Specific Customer 2

     >Map Drivers

Specific customer 3

     >Map Drives

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...