如何创建PowerShell脚本将文件复制到USB闪存驱动器? [英] How can I create a PowerShell script to copy a file to a USB flash drive?

查看:300
本文介绍了如何创建PowerShell脚本将文件复制到USB闪存驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个虚拟硬盘.vhd文件,我想通过点击Windows Vista笔记本电脑上的快捷方式每天备份。我写了一个半危险的批处理脚本文件(BACKUP.BAT)完成工作,它打开cmd窗口并将文件复制到闪存驱动器,但我想模仿(宏)复制的显示方式您手动将文件拖放到我的计算机中的闪存驱动器。另一个问题是,根据什么计算机这样做,USB闪存驱动器可以有驱动器E:分配给它(WinXP)和在其他计算机(Vista / 7),它可以是驱动器F:。 (在将USB闪存驱动器插入USB端口时,似乎没有办法将固定的驱动器盘符静态分配给USB闪存驱动器。)

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 file (BACKUP.BAT) which accomplishes the job, where it open the cmd window and copies the file to the flash drive, but I would like to mimic (macro) the way the copying is displayed when you manually drag and drop the file into the flash drive in my computer. Another problem is that depending on what computer this is done, the USB flash drive could have drive E: assigned to it (WinXP) and on other computers (Vista/7) it could be drive F:. (There doesnt seem to be a way to statically assign a fixed drive letter to the USB flash drive when it is inserted into the USB port.)

推荐答案

我将设置光盘的卷名称,并检查所有连接的驱动器,并找到具有该卷名称的驱动器。这是我在PowerShell中的做法:

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

这篇关于如何创建PowerShell脚本将文件复制到USB闪存驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆