none
Help with Changing Drive letter RRS feed

  • Question

  • https://stackoverflow.com/questions/16665895/vbscript-to-change-drive-letter#

    I need to change a drive letter (M to H), for my purposes I have modified the code in the above link as follows, however there are errors which I do not know how to resolve, they are:

    1. how do I assign wscript:   2. how do I assign objWMIService: 3. how do I assign colVolumes = objWMIservice.ExecQuery

     Thanks for any assistance, also, if run as restricted user will it execute?
    'This works on Windows 10
            ' Elevate the script to run As Administrator 

            If Not WScript.Arguments.Named.Exists("elevate") Then
                CreateObject("Shell.Application").ShellExecute(WScript.FullName _
                  , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1)
                WScript.Quit()
            End If






            objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")

            colVolumes = objWMIService.ExecQuery _
            ("Select * from Win32_Volume Where Name = '" & "M" & ":\\'")

            For Each objVolume In colVolumes
                objVolume.DriveLetter = "H" & ":"
                objVolume.Put_()
            Next


    Monday, October 29, 2018 6:16 AM

All replies

  • Hi,

    Your code is vbs,but this forum is discussing and asking questions about the vb.net.

    You can ask in the following link.

    VBS

    or you want to change driver letter with vb.net ?

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 29, 2018 7:26 AM
  • Hello,

    Code in link you have shared is of .vbs what you can do is you can create .vbs file and can execute same vbs file from vbnet as below

     Dim start As New ProcessStartInfo  
            start.FileName = "C:\WINDOWS\system32\cscript.exe" 
            start.Arguments = "C:\Testfiles\file.vbs" 
            start.UseShellExecute = False 
            start.RedirectStandardOutput = True 
            start.RedirectStandardError = True 
     
            Dim myproc As New Process  
            myproc.StartInfo = start  
            myproc.Start()  
            Dim so As System.IO.StreamReader  
            Dim se As System.IO.StreamReader  
            se = myproc.StandardError  
            so = myproc.StandardOutput  
            myproc.WaitForExit()  
            MsgBox(so.ReadToEnd)  
            MsgBox(se.ReadToEnd) 



    Monday, October 29, 2018 8:39 AM
  • Thanks to Alex, yes i want to do this in vb.net I am using visual studio 2010

    Thanks also to Tech Aspirant also, tried your suggestion but app reports at line myproc.start "Cannot start process because a file name has not been provided"

    So not sure if remainder of project will work until this is resolved

    Tuesday, October 30, 2018 5:57 AM
  • Thanks to Alex, yes i want to do this in vb.net I am using visual studio 2010

    Thanks also to Tech Aspirant also, tried your suggestion but app reports at line myproc.start "Cannot start process because a file name has not been provided"

    So not sure if remainder of project will work until this is resolved

    Hi,

    try the code

     Dim disks As ManagementObjectSearcher = New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_Volume WHERE DriveLetter = 'M:'")
    
            For Each disk As ManagementObject In disks.[Get]()
                disk.[Get]()
                disk("DriveLetter") = "H:"
                disk.Put()
            Next

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 31, 2018 4:56 AM
  • Thanks for that, Alex

    I would prefer your solution if I can get it to work ( I have resolved the script version which I will post later for others. it is a bit messy with the window appearing for authority)

    It seems to take a while in the loop, then stops at disk.put with message "access denied" and unauthorised access. I am logged on as admin

    Wednesday, October 31, 2018 5:43 AM
  • Unfortuneately I have to leave for now, back in 22 hours
    Wednesday, October 31, 2018 6:13 AM
  • Thanks for that, Alex

    I would prefer your solution if I can get it to work ( I have resolved the script version which I will post later for others. it is a bit messy with the window appearing for authority)

    It seems to take a while in the loop, then stops at disk.put with message "access denied" and unauthorised access. I am logged on as admin

    Hi,

    I can change my driver letter with my code,your error should be you have not modified the permissions.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 2, 2018 1:50 AM
  • Thanks for that Alex, I have checked the security on the drive & it has full permissions for everyone, any ideas on which specific permission & where needs to be amended?
    Friday, November 2, 2018 6:15 AM
  • For those interested in the vb script version:

    ' Create Script file

            Dim SourceDriveLetter As String
            Dim NewDriveLetter As String
            Dim ScriptFileFolder_Name As String

            ScriptFileFolder_Name = "C:\_Recipes\test11.vbs"
            SourceDriveLetter = "M"
            NewDriveLetter = "P"

            Dim objwriter As New System.IO.StreamWriter(ScriptFileFolder_Name, False)
            
            objwriter.WriteLine("If Not WScript.Arguments.Named.Exists(" & Chr(34) & "elevate" & Chr(34) & ") Then")
            objwriter.WriteLine("CreateObject(" & Chr(34) & "Shell.Application" & Chr(34) & ").ShellExecute WScript.FullName _")
            objwriter.WriteLine(", WScript.ScriptFullName & " & Chr(34) & " /elevate" & Chr(34) & ", " & Chr(34) & Chr(34) & ", " & Chr(34) & "runas" & Chr(34) & ", 1")
            objwriter.WriteLine("WScript.Quit")
            objwriter.WriteLine("End If")

            objwriter.WriteLine("set objWMIService = GetObject(" & Chr(34) & "winmgmts:" & Chr(34) & " _")
            objwriter.WriteLine("& " & Chr(34) & "{impersonationLevel=impersonate}!\\" & Chr(34) & " & " & Chr(34) & "." & Chr(34) & " & " & Chr(34) & "\root\cimv2" & Chr(34) & ")")

            objwriter.WriteLine("set colVolumes = objWMIService.ExecQuery _")
            objwriter.WriteLine("(" & Chr(34) & "Select * from Win32_Volume Where Name = '" & Chr(34) & " & " & Chr(34) & SourceDriveLetter & Chr(34) & " & " & Chr(34) & ":\\'" & Chr(34) & ")")

            objwriter.WriteLine("For Each objVolume In colVolumes")
            objwriter.WriteLine("objVolume.DriveLetter = " & Chr(34) & NewDriveLetter & Chr(34) & " & " & Chr(34) & ":" & Chr(34))
            objwriter.WriteLine("objVolume.Put_")
            objwriter.WriteLine("Next")
            objwriter.Close()


            'Execute script file

            Dim start As New ProcessStartInfo

            start.FileName = "C:\Windows\System32\cscript.exe"
            start.Arguments = ScriptFileFolder_Name

            start.UseShellExecute = False
            start.RedirectStandardOutput = True
            start.RedirectStandardError = True



            Dim myproc As New Process
            myproc.StartInfo = start
            myproc.Start()
            Dim so As System.IO.StreamReader
            Dim se As System.IO.StreamReader
            se = myproc.StandardError
            so = myproc.StandardOutput
            myproc.WaitForExit()
            'MsgBox(so.ReadToEnd)
            'MsgBox(se.ReadToEnd)

    Saturday, November 3, 2018 6:06 AM