Welcome to MSFN

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.


lloydie2009

VPS Script - Takeown + Robocopy

5 posts in this topic

Hello,

OK, let's test your skills because mine are cak.

Can someone turn that into a VBS file, preferably so if there are any errors, it doesn't skip to the end of the file (the issue I'm having now, if it fails, it still deletes the folder)

cd c:\local

set /P username="Enter Target Username"

takeown.exe C:\Users\%username% /D Y /F

cacls c:\users\%username% /t /e /g Administrators:f

robocopy c:\users\%username% c:\local\profilebackup\%username% /E /R:1 /W:1

remprof %username%

rd /s /q c:\users\%username%

Here's my attempt:

I am trying to create a VBS to take ownership of a user input parameter folder, and then copy that to another location however am having issues.

I think it's the ownership bit that causing a problem which if run in a single script works fine, and is only when trying to add the robocopy section of the script I am having issues.

I'm afraid I have limited knowledge, and there is no error that appears, it prompts for the username as it should, and then closes a few moments later, it would appear when it hit's the robocopy section, however if you then look at c:\users, ownership has not been taken.

Set objSysInfo = CreateObject("ADSystemInfo")

Set objNetwork = CreateObject("Wscript.Network")

Set objFSO = CreateObject("Scripting.FileSystemObject")

strsUsername = InputBox ("please enter target username")

Set objShell = CreateObject("Wscript.Shell")

objShell.Run "takeown.exe /F C:\users\"&sUsername&""

objShell.Run "robocopy.exe c:\users\"&sUsername&" c:\local\profilebackup\localprofile /e /zb /R:3 /W:2"

Any suggestions on how i can accomplish this would be great, i need to have a InputBox as this is for Admin to use from different accounts, so the script cannot auto-determine the folders.

Many Thanks

0

Share this post


Link to post
Share on other sites

Are you running this with admin rights from a CMD window or just double clicking?

0

Share this post


Link to post
Share on other sites

I could be wrong but

1:\strsUsername varible name you are trying to pass

2:\&sUsername& varible name in script

3:\&sUsername& varible name in script

would cause the script to use a varible that does not have any information.

Here is a script that prevents the Input from being close using the Cancel Button or

the X and only process the Input if it 2 characters or longer.

The only thing I have not tested is the Takeown and RoboCopy, the rest of the script

runs without errors.

UserIn_Takeown_RoboCopy.vbs

'-> This code is property of Gunsmokingman and Or Jake1Eye and you must have his permission to use.'-> This is only posted as example code and meant only to used as such.Dim Act :Set Act = CreateObject("Wscript.Shell")Dim UserGetInput()Function GetInput()User = InputBox(_"Type in the target User Name" & vbCrLf & vbCrLf & _"To exit or quit type Exit or Quit","","",6500,5500)If User <> "" And Len(User) >= 2 Then'->Make Sure Input More Then 2 CharactersIf InStr(LCase(User),"exit") Or _InStr(LCase(User),"quit") Then'-> Exit Or Quit ScriptWScript.Quit()Else'-> Code Here For TakeownAct.Run("takeown.exe /F C:\users\" & User),0,True'-> Code Here For RoboCopyAct.Run("robocopy.exe c:\users\" & User & _" c:\local\profilebackup\localprofile /e /zb /R:3 /W:2")0,True'-> End Of Script Popup Will Self Close In 5 SecondsAct.Popup "Script Completed",5,"Finshed",4128End IfElse'-> To Disable Red X,Cancel And Prevent No Text,And Less 2 CharactersMsgBox vbtab & "Error" & vbcrlf & vbcrlf & _"The Cancel, Red X have been disable" & vbCrLf & _"or no Text was filled in or less then 2" & vbCrLf & _"characters where inputted",4128,"Error"GetInput()End IfEnd Function
0

Share this post


Link to post
Share on other sites

Can someone turn that into a VBS file, preferably so if there are any errors, it doesn't skip to the end of the file (the issue I'm having now, if it fails, it still deletes the folder)

Why not simply introduce some error control?

cd c:\local

:loop

SET username=MickeyMouse

set /P username="Enter Target Username"

IF NOT EXIST c:\users\%username% ECHO NO such username&GOTO :loop

takeown.exe C:\Users\%username% /D Y /F

cacls c:\users\%username% /t /e /g Administrators:f

robocopy c:\users\%username% c:\local\profilebackup\%username% /E /R:1 /W:1

remprof %username%

rd /s /q c:\users\%username%

This assumes that username has no spaces (and that an user name "MickeyMouse" does not exist on that system ;) )

jaclaz

0

Share this post


Link to post
Share on other sites

Can someone turn that into a VBS file, preferably so if there are any errors, it doesn't skip to the end of the file (the issue I'm having now, if it fails, it still deletes the folder)

Why not simply introduce some error control?

cd c:\local

:loop

SET username=MickeyMouse

set /P username="Enter Target Username"

IF NOT EXIST c:\users\%username% ECHO NO such username&GOTO :loop

takeown.exe C:\Users\%username% /D Y /F

cacls c:\users\%username% /t /e /g Administrators:f

robocopy c:\users\%username% c:\local\profilebackup\%username% /E /R:1 /W:1

remprof %username%

rd /s /q c:\users\%username%

This assumes that username has no spaces (and that an user name "MickeyMouse" does not exist on that system ;) )

jaclaz

You would have to add more checks to the batch file before the "rd /s /q" so won't lose data. You need to be sure that at least the robocopy worked successfully (ie: copied all data) and most likely need to parse robocopy log file properly and it could be a hassle in pure batch.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.