windows 如何创建 PowerShell 脚本以将文件复制到 USB 闪存驱动器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6500277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:13:45  来源:igfitidea点击:

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

windowspowershellscriptingcopy

提问by Frank R.

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.)

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

回答by Michael Kelley

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:

我会设置光盘的卷名,并检查所有连接的驱动器并找到具有该卷名的驱动器。这是我在 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

回答by Roman Kuzmin

This code gets the last ready to use removable drive (e.g. an USB drive just plugged-in):

此代码最后准备好使用可移动驱动器(例如刚刚插入的 USB 驱动器):

$drives = [System.IO.DriveInfo]::GetDrives()
$r = $drives | Where-Object { $_.DriveType -eq 'Removable' -and $_.IsReady }
if ($r) {
    return @($r)[-1]
}
throw "No removable drives found."

This way does not require the fixed volume name to be pre-set. We can use different USB drives without knowing/setting their names.

这种方式不需要预先设置固定的卷名。我们可以在不知道/设置名称的情况下使用不同的 USB 驱动器。



UPDATETo complete drag-and-drop part of the task you can do this.

更新要完成任务的拖放部分,您可以执行此操作。

Create the PowerShell script (use Notepad, for example) C:\TEMP_110628_041140\Copy-ToRemovableDrive.ps1 (the path is up to you):

创建 PowerShell 脚本(例如使用记事本)C:\TEMP_110628_041140\Copy-ToRemovableDrive.ps1(路径由您决定):

param($Source)

$drives = [System.IO.DriveInfo]::GetDrives()
$r = $drives | Where-Object { $_.DriveType -eq 'Removable' -and $_.IsReady }
if (!$r) {
    throw "No removable drives found."
}

$drive = @($r)[-1]
Copy-Item -LiteralPath $Source -Destination $drive.Name -Force -Recurse

Create the file Copy-ToRemovableDrive.bat (for example on your desktop), it uses the PowerShell script:

创建文件 Copy-ToRemovableDrive.bat(例如在您的桌面上),它使用 PowerShell 脚本:

powershell -file C:\TEMP\_110628_041140\Copy-ToRemovableDrive.ps1 %1

Now you can plug your USB drive and drag a file to the Copy-ToRemovableDrive.baticon at your desktop. This should copy the dragged file to the just plugged USB drive.

现在,您可以插入 USB 驱动器并将文件拖到Copy-ToRemovableDrive.bat桌面上的图标上。这应该将拖动的文件复制到刚刚插入的 USB 驱动器。