Created
April 7, 2020 16:50
-
-
Save MrCitron/8fb1b621aa00bca63037b617afa6cd6f to your computer and use it in GitHub Desktop.
Powershell script to ease the copy of many CD contents to harddrive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run with & powershell.exe -executionpolicy bypass -file .\copy_cd.ps1 | |
Add-Type -TypeDefinition @' | |
using System; | |
using System.Runtime.InteropServices; | |
using System.ComponentModel; | |
namespace CDROM | |
{ | |
public class Commands | |
{ | |
[DllImport("winmm.dll")] | |
static extern Int32 mciSendString(string command, string buffer, int bufferSize, IntPtr hwndCallback); | |
public static void Eject() | |
{ | |
string rt = ""; | |
mciSendString("set CDAudio door open", rt, 127, IntPtr.Zero); | |
} | |
public static void Close() | |
{ | |
string rt = ""; | |
mciSendString("set CDAudio door closed", rt, 127, IntPtr.Zero); | |
} | |
} | |
} | |
'@ | |
Add-Type @' | |
using System; | |
using System.Runtime.InteropServices; | |
public class Tricks { | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool SetForegroundWindow(IntPtr hWnd); | |
} | |
'@ | |
$parent = Get-Process -id ((Get-WmiObject win32_process -Filter "processid='$pid'").parentprocessid) | |
Function Get-Focus { | |
If ($parent.Name -eq "cmd") { | |
# Being run by via cmd prompt (batch file) | |
$h = (Get-Process cmd).MainWindowHandle | |
[void] [Tricks]::SetForegroundWindow($h) | |
} | |
else { | |
# being run in powershell ISE or console | |
$h = (Get-Process -id $pid).MainWindowHandle | |
[void] [Tricks]::SetForegroundWindow($h) | |
} | |
} | |
$shell = New-Object -Com Shell.Application | |
$cdPath = $shell.NameSpace("d:\") | |
$destination = $shell.NameSpace("c:\temp") | |
$continue = $true | |
while ($continue) { | |
[CDROM.Commands]::Eject() | |
$x = 0 | |
while ($x -ne 13 -and $x -ne 27) { | |
Write-Output "Insérez un disque et appuyez sur entrée ou appuyez sur echap pour terminer" | |
Get-Focus | |
$x = $HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode | |
$HOST.UI.RawUI.Flushinputbuffer() | |
# Write-Output $x | |
} | |
switch ($x) { | |
27 { $continue = $false } | |
} | |
[CDROM.Commands]::Close() | |
if ($continue) { | |
Start-Sleep 10 | |
$destination.CopyHere($cdPath.Items()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment