How can I create a PowerShell script to copy a file to a USB flash drive?

前端 未结 2 1236
旧巷少年郎
旧巷少年郎 2021-01-07 01:52

I have a virtual hard disk .vhd file that I would like to backup on a daily basis by clicking on a shortcut on my Windows Vista laptop. I wrote a half-hazard batch script fi

2条回答
  •  花落未央
    2021-01-07 02:31

    I would set the volume name of the disc, and examine all connected drives and find the drive with that volume name. Here's how I do it in PowerShell:

    param([parameter(mandatory=$true)]$VolumeName,
          [parameter(mandatory=$true)]$SrcDir)
    
    # find connected backup drive:
    $backupDrive = $null
    get-wmiobject win32_logicaldisk | % {
        if ($_.VolumeName -eq $VolumeName) {
            $backupDrive = $_.DeviceID
        }
    }
    if ($backupDrive -eq $null) {
        throw "$VolumeName drive not found!"
    }
    
    # mirror 
    $backupPath = $backupDrive + "\"
    & robocopy.exe $SrcDir $backupPath /MIR /Z
    

提交回复
热议问题