Here is another usefull class that will run as an administrator for xcopy operations on protected filesystems. In my case this is a large folder structure for our clients’ files that we do not want the ordinary user to be able to move or delete: users being what they are they’ll drag and drop anything given half the chance ;p. I use it to clone a standard folder template with different ACLs on different folders and sub-folders.
Option Strict Off
Option Explicit On
Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Security
#Region “XcopyClass Class”
”’
”’ COM class to run Xcopy commands as DocRootAdmin
”’
”’
”’ source: http://www.dreamincode.net/code/snippet1231.htm
”’ Modified to run as Domain Admin
”’ and to conform to CKA coding standards
”’
_ Public Class XcopyClass
#Region “COM GUIDs”
‘ These GUIDs provide the COM identity for this class
‘ and its COM interfaces. If you change them, existing
‘ clients will no longer be able to access the class. Public Const ClassId As String = “321f8234-8343-415f-9c77-3628ff655502″
Public Const InterfaceId As String = “a4d2eb4c-935b-45e2-bc3a-f8e34fea2bf5″
Public Const EventsId As String = “1ef73250-4246-459c-9808-1ee2fdd9a045″ #End Region
#Region “Constructor”
‘ A creatable COM class must have a Public Sub New()
‘ with no parameters, otherwise, the class will not be
‘ registered in the COM registry and cannot be created
‘ via CreateObject. Public Sub New()
MyBase.New()
End Sub
#End Region
#Region “Declarations”
Private _Status As Status
#End Region
#Region “Properties”
”’ ”’ Exposes object status to code that uses this COM object
”’
”’
”’
”’
”’ Status is set by class methods to indicate success
”’ or exceptions/errors and their causes
”’ Public ReadOnly Property Status() As Status
Get
Return _Status
End Get
End Property
#End Region
#Region “Public Methods”
”’ ”’ Run Xcopy as admin user
”’
”’
the xcopy arguments: switches, source and target ”’ True or False
”’
”’ This function runs xcopy as the DocRootAdmin user
”’ Public Function ProcessXCopy(ByVal arg As String) As Boolean
Dim flgReturn As Boolean = False
Dim XCopyArguments As String = arg
Dim XCopyProcess As New Process()
Dim XCopyStartInfo As New ProcessStartInfo()
Dim ExitCode As Integer
Dim securePwd As New SecureString()
Try
XCopyStartInfo.FileName = “CMD.exe “
‘set the domain and user XCopyStartInfo.Domain = AppData.GetAppData(“DocRootDomain”)
XCopyStartInfo.UserName = AppData.GetAppData(“DocRootAdmin”)
‘convert password to a secure string and set it For Each character As Char In AppData.GetAppData(“DocRootPassword”)
securePwd.AppendChar(character)
Next
securePwd.MakeReadOnly()
XCopyStartInfo.Password = securePwd
‘do not write error output to standard stream XCopyStartInfo.RedirectStandardError = False ‘do not write output to Process.StandardOutput Stream XCopyStartInfo.RedirectStandardOutput = False ‘do not read input from Process.StandardInput (i/e; the keyboard) XCopyStartInfo.RedirectStandardInput = False
XCopyStartInfo.UseShellExecute = False ‘Dont show a command window XCopyStartInfo.CreateNoWindow = True
‘start cmd.exe & the XCOPY process XCopyProcess.Start()
‘set the wait period for exiting the process XCopyProcess.WaitForExit(5000)
‘get the return code ExitCode = XCopyProcess.ExitCode
‘Now we need to see if the process was successful If ExitCode > 0 And Not XCopyProcess.HasExited Then
XCopyProcess.Kill()
flgReturn = False
Else
flgReturn = True
End If
”’
”’ Set Status object with current error status for reporting ”’
”’
Error Status: True or False ”’
”’
”’
”’
”’ When a method fails for any reason,
”’ it calls this method to populate the Status object ”’ with error details for client code to interogate.
”’ Private Sub SetStatus(ByVal StatusSet As Boolean, ByVal StatusCode As ErrorCode, ByVal StatusSource As String, ByVal StatusMessage As String)
How to run Xcopy with elevated privileges
Here is another usefull class that will run as an administrator for xcopy operations on protected filesystems. In my case this is a large folder structure for our clients’ files that we do not want the ordinary user to be able to move or delete: users being what they are they’ll drag and drop anything given half the chance ;p. I use it to clone a standard folder template with different ACLs on different folders and sub-folders.
Option Strict Off
Option Explicit On
Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Security
#Region “XcopyClass Class”
”’
”’ COM class to run Xcopy commands as DocRootAdmin
”’
”’
”’ source: http://www.dreamincode.net/code/snippet1231.htm
”’ Modified to run as Domain Admin
”’ and to conform to CKA coding standards
”’
_
Public Class XcopyClass
#Region “COM GUIDs”
‘ These GUIDs provide the COM identity for this class
‘ and its COM interfaces. If you change them, existing
‘ clients will no longer be able to access the class.
Public Const ClassId As String = “321f8234-8343-415f-9c77-3628ff655502″
Public Const InterfaceId As String = “a4d2eb4c-935b-45e2-bc3a-f8e34fea2bf5″
Public Const EventsId As String = “1ef73250-4246-459c-9808-1ee2fdd9a045″
#End Region
#Region “Constructor”
‘ A creatable COM class must have a Public Sub New()
‘ with no parameters, otherwise, the class will not be
‘ registered in the COM registry and cannot be created
‘ via CreateObject.
Public Sub New()
MyBase.New()
End Sub
#End Region
#Region “Declarations”
Private _Status As Status
#End Region
#Region “Properties”
”’
”’ Exposes object status to code that uses this COM object
”’
”’
”’
”’
”’ Status is set by class methods to indicate success
”’ or exceptions/errors and their causes
”’
Public ReadOnly Property Status() As Status
Get
Return _Status
End Get
End Property
#End Region
#Region “Public Methods”
”’
”’ Run Xcopy as admin user
”’
”’
the xcopy arguments: switches, source and target ”’ True or False
”’
”’ This function runs xcopy as the DocRootAdmin user
”’
Public Function ProcessXCopy(ByVal arg As String) As Boolean
Dim flgReturn As Boolean = False
Dim XCopyArguments As String = arg
Dim XCopyProcess As New Process()
Dim XCopyStartInfo As New ProcessStartInfo()
Dim ExitCode As Integer
Dim securePwd As New SecureString()
Try
XCopyStartInfo.FileName = “CMD.exe “
‘set the domain and user
XCopyStartInfo.Domain = AppData.GetAppData(“DocRootDomain”)
XCopyStartInfo.UserName = AppData.GetAppData(“DocRootAdmin”)
‘convert password to a secure string and set it
For Each character As Char In AppData.GetAppData(“DocRootPassword”)
securePwd.AppendChar(character)
Next
securePwd.MakeReadOnly()
XCopyStartInfo.Password = securePwd
‘do not write error output to standard stream
XCopyStartInfo.RedirectStandardError = False
‘do not write output to Process.StandardOutput Stream
XCopyStartInfo.RedirectStandardOutput = False
‘do not read input from Process.StandardInput (i/e; the keyboard)
XCopyStartInfo.RedirectStandardInput = False
XCopyStartInfo.UseShellExecute = False
‘Dont show a command window
XCopyStartInfo.CreateNoWindow = True
XCopyStartInfo.Arguments = “/D /c XCOPY ” & XCopyArguments
XCopyProcess.EnableRaisingEvents = True
XCopyProcess.StartInfo = XCopyStartInfo
‘start cmd.exe & the XCOPY process
XCopyProcess.Start()
‘set the wait period for exiting the process
XCopyProcess.WaitForExit(5000)
‘get the return code
ExitCode = XCopyProcess.ExitCode
‘Now we need to see if the process was successful
If ExitCode > 0 And Not XCopyProcess.HasExited Then
XCopyProcess.Kill()
flgReturn = False
Else
flgReturn = True
End If
Catch ex As Exception
flgReturn = False
SetStatus(True, ErrorCode.CodeException, “XcopyClass->ProcessXCopy”, “Exception” & vbCrLf & vbCrLf & ex.Source & ” : ” & ex.Message)
Finally
XCopyProcess.Dispose()
XCopyStartInfo = Nothing
ProcessXCopy = flgReturn
End Try
End Function
#End Region
#Region “Private Methods”
”’
”’ Set Status object with current error status for reporting
”’
”’
Error Status: True or False ”’
”’
”’
”’
”’ When a method fails for any reason,
”’ it calls this method to populate the Status object
”’ with error details for client code to interogate.
”’
Private Sub SetStatus(ByVal StatusSet As Boolean, ByVal StatusCode As ErrorCode, ByVal StatusSource As String, ByVal StatusMessage As String)
If IsNothing(_Status) Then _Status = New Status
_Status.ErrorState = StatusSet
_Status.ErrorCode = StatusCode
_Status.ErrorSource = StatusSource
_Status.ErrorMessage = StatusMessage
End Sub
#End Region
End Class
#End Region