Link to home
Create AccountLog in
Windows Server 2008

Windows Server 2008

--

Questions

--

Followers

Top Experts

Avatar of westhelpdesk
westhelpdesk

Script for disk2vhd
Here is what i am looking for..

Wanted to run disk2vhd nightly on the server and have it backup to a NAS devicea and/or usb drive.....wanted this to be automated and if possible to have it delete the vhd previous on the device....

Does anyone have a script for this???

I have no nothing about scripting and have searched but found nothing...

Any help is appreciated>>>>.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Subhashish LahaSubhashish Laha🇮🇳


Avatar of westhelpdeskwesthelpdesk

ASKER

Yes, I know that!!!! But that doesnt provide a script for me to do that......I no nothing about scripts from my original post and looking for script from my original post...thanks though....but no good for i already seen that article for that is where i download the app from....

Avatar of subhashchysubhashchy🇮🇳

try this

@echo off
net use Y: \\path-to-your-networkshare /u:administrator passowrd
if exist Y:\test.vhd del Y:\test.vhd
disk2vhd C:  Y:\test.vhd

Open in new window


 line # 2, replace \\path-to-your-networkshare  with the actual path of your network device, replace administrator and password with the real passwords.

It will map a network Drive Y: and create image of your C: in the Y: drive as a file name test. before that it will del the existing file if present.

let me know if you want to modify something

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


I have both a C and D drive that i need backed up nightly to a NAS device into a shared folder on that device...is it possible to make this that???

Thanks for all your help....

Okay, this is in powershell. Just copy it into notepad and save it as something.ps1

 
$UNCPathToShare="\\NAS\backup"

$DriveLetter="x:"

$filename = (get-date -uformat %d%b%y) + "_" `
            + $Env:COMPUTERNAME + ".vhd"

$drive = new-object -com wscript.network
$drive.MapNetworkDrive($DriveLetter, $UNCPathToShare)

c:\disk2vhd.exe c: d: ($driveletter + "\" + $filename)

$backups = dir x: *.vhd

if ($backups.Count -gt 1) {
        foreach ($backup in $backups) {
          remove-item ($driveletter + "\" + $backup) `
             -Exclude $filename
               }
   }
    
$drive.RemoveNetworkDrive($DriveLetter)

Open in new window



Change the path for $UNCPathToShare to whatever the share is that your exporting to.

I can't get disk2vhd to work on my machine period via the command line, so I'm not 100% certain that this will even run (have you been successful running this from command line?).

The file name is today's date and the machine's hostname.

The script will delete any other backup aside from Today's backup, so long as there is more than one file in the folder that is .vhd (this runs after the export, so there should be two files and it will delete the older one).

Note that if you have never used powershell to run scripts, you'll need to set the execution level. Open Powershell.exe and run
Set-ExecutionPolicy remotesigned -force

Open in new window






Avatar of subhashchysubhashchy🇮🇳

Powershell will do as well. If u want to use batch,then change c: with * and save it

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


wasnt looking for powershell.....how about vbs scribt????????

Powershell works on Server 2008 (installed by default)... why do you want vbscript?

just a preference...never used powershell and no nothing about it...

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


hey manufest,

Your script worked fine for me up to creating the vhd, but i'm having am struggling to get the removal of previous .vhd to work. Seems it fails at the Exclude command. Got any further ideas. Thanks in advance.

Missing expression after unary operator '-'.
At C:\mapdrive.ps1:20 char:15
+              - <<<< Exclude $filename
    + CategoryInfo          : ParserError: (-:String) [], Pars
    + FullyQualifiedErrorId : MissingExpressionAfterOperator

Still waiting for response for not working

SOLUTION
Avatar of manufestmanufest

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

ASKER CERTIFIED SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Check that last post!! I had to put a 'wait' command in after the disk2vhd command and before the cd command, to allow completion of creating the vhd before changing drives to delete old .vhd's.

Hope this helps!!


$filename = (get-date -uformat %d%b%y) + "_" `
            + $Env:COMPUTERNAME + ".vhd"

$DriveLetter="w:"

c:\disk2vhd c: ($driveletter + "\" + $filename) | wait-job
 
cd $driveletter

remove-item .\* -exclude "$filename",admin* -force

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


still working on this

thanks to both of you for your help
Windows Server 2008

Windows Server 2008

--

Questions

--

Followers

Top Experts

Windows Server 2008 and Windows Server 2008 R2, based on the Microsoft Vista codebase, is the last 32-bit server operating system released by Microsoft. It has a number of versions, including including Foundation, Standard, Enterprise, Datacenter, Web, HPC Server, Itanium and Storage; new features included server core installation and Hyper-V.