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

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

问题描述

我有一个虚拟硬盘 .vhd 文件,我想每天通过单击 Windows Vista 笔记本电脑上的快捷方式来备份它.我写了一个半危险的批处理脚本文件(BACKUP.BAT)来完成这项工作,它打开cmd窗口并将文件复制到闪存驱动器,但我想模仿(宏)复制的显示方式您手动将文件拖放到我计算机的闪存驱动器中.另一个问题是,根据执行此操作的计算机,USB 闪存驱动器可能分配有驱动器 E:(WinXP),而在其他计算机(Vista/7)上,它可能是驱动器 F:.(似乎没有办法在插入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天全站免登陆