Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

VPS Script - Takeown + Robocopy

- - - - -

  • Please log in to reply
4 replies to this topic

#1
lloydie2009

lloydie2009
  • Member
  • 2 posts
  • Joined 18-February 13
  • OS:Windows 7 x64
  • Country: Country Flag
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


How to remove advertisement from MSFN

#2
IcemanND

IcemanND

    MSFN Junkie

  • Super Moderator
  • 3,266 posts
  • Joined 24-September 03
  • OS:Windows 8.1 x64
  • Country: Country Flag
Are you running this with admin rights from a CMD window or just double clicking?

#3
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,430 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
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 User
GetInput()

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 Characters
If InStr(LCase(User),"exit") Or _
InStr(LCase(User),"quit") Then
'-> Exit Or Quit Script
WScript.Quit()
Else
'-> Code Here For Takeown
Act.Run("takeown.exe /F C:\users\" & User),0,True
'-> Code Here For RoboCopy
Act.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 Seconds
Act.Popup "Script Completed",5,"Finshed",4128
End If
Else
'-> To Disable Red X,Cancel And Prevent No Text,And Less 2 Characters
MsgBox 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 If
End Function



GunSmokingMan



#4
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,823 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

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

#5
allen2

allen2

    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,814 posts
  • Joined 13-January 06


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 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users