none
Creating Diskpart command Using VBScript

Answers

  • To literally answer the question based on the subject line and the first post:

    You can execute a program from a VBScript script by creating an instance of the WshShell object, and use the WshShell object's Run method. For example (not tested):


    Dim WshShell
    Set WshShell = CreateObject("WScript.Shell")
    
    Dim Command
    Command = "diskpart command line"
    
    Dim Result
    Result = WshShell.Run(Command, 1, True)

    ...or something similar.

    Bill

    Monday, March 04, 2013 7:01 PM

All replies

  • Why?
    What exactly do you want to achieve?
    Why not invoke diskpart through a batch file?

    Monday, March 04, 2013 11:09 AM
  • VBScript has no facility for partitioning a disk.  Use DISKPART tool.

    ¯\_(ツ)_/¯

    Monday, March 04, 2013 4:44 PM
  • To literally answer the question based on the subject line and the first post:

    You can execute a program from a VBScript script by creating an instance of the WshShell object, and use the WshShell object's Run method. For example (not tested):


    Dim WshShell
    Set WshShell = CreateObject("WScript.Shell")
    
    Dim Command
    Command = "diskpart command line"
    
    Dim Result
    Result = WshShell.Run(Command, 1, True)

    ...or something similar.

    Bill

    Monday, March 04, 2013 7:01 PM
  • Bill, diskpart.exe does not accept any parameters. They need to be placed into a script file that is piped into diskpart.exe.
    Monday, March 04, 2013 7:12 PM
  • Understood that my example didn't give an example of every possible step. The script could be extended to:

    1. Use the FileSystemObject object to create a temporary file

    2. Write the needed diskpart commands into the temporary file

    3. Use the aforementioned Run method to run the diskpart.exe program, passing the temporary file's path and filename after diskpart.exe's /s parameter

    4. Delete the temporary file

    Bill

    Monday, March 04, 2013 7:23 PM
  • Or you can can try something ...

    ' Run diskpart
    set objShell = WScript.CreateObject("WScript.Shell")
    set objExec = objShell.Exec("diskpart.exe")
    
    'Montar
    
    'Desmontar
    strOutput = ExecuteDiskPartCommand("SEL DISK 0")
    strOutput = ExecuteDiskPartCommand("SEL PARTITION 2")
    strOutput = ExecuteDiskPartCommand("REMOVE MOUNT=E:\TESTE")
    ExitDiskPart
    
    
    
    WScript.echo "OK"
    
    
    
    Function ExecuteDiskPartCommand (strCommand)
    
        ' Run the command we want
        objExec.StdIn.Write strCommand & VbCrLf
    
        ' If we read the output now, we will get the one from previous command (?). As we will always
        ' run a dummy command after every valid command, we can safely ignore this
        Do While True
            IgnoreThis = objExec.StdOut.ReadLine & vbcrlf
    
            ' Command finishes when diskpart prompt is shown again
            If InStr(IgnoreThis, "DISKPART>") <> 0 Then 
    	Exit Do
        Loop
    
        ' Run a dummy command, so the next time we call this function and try to read output,
        ' we can safely ignore the result
        objExec.StdIn.Write VbCrLf
    
        ' Read command's output
        ExecuteDiskPartCommand = ""
        Do While True
            ExecuteDiskPartCommand = ExecuteDiskPartCommand & objExec.StdOut.ReadLine & vbcrlf
    
            ' Command finishes when diskpart prompt is shown again
            If InStr(ExecuteDiskPartCommand, "DISKPART>") <> 0 Then Exit Do
        Loop
    
    End Function
    
    Sub ExitDiskPart
        ' Run exit command to exit the tool
        objExec.StdIn.Write "exit" & VbCrLf
    End Sub

    As the link below

    http://blogs.msdn.com/b/alejacma/archive/2011/04/26/how-to-automate-a-command-line-utility-like-diskpart-vbscript.aspx


    att, Aparecido Deveza

    Tuesday, April 16, 2013 7:59 PM