Sunday, August 19, 2018

Powershell TWAIN / scan from USB

This is just me being lazy, really...
  1. Plug in USB scanner...
  2. Start Button
  3. Open the 'Devices and Printers' applet
  4. Find the scanner
  5. Right-Click, and 'Start a Scan'

Or -
Write something that will find the scanner, and open the 'Start a Scan' window, when the script is ran...


I enabled the Quick Launch bar, and have this in there as a shortcut - I just changed the icon to a 'scanner'. (from here: %SystemRoot%\System32\SHELL32.dll)


This is the command for the shortcut...
Target:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe –NoProfile -windowstyle hidden -file "C:\Users\Rich\PSScripts\StartScan.ps1"

This line can be ran, to discover the 'Device description' for your device...
(gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)}).Description

Here is the scripts text:
$DeviceDesc = "CanoScan"
$FindTWAIN = (gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} | Where-Object {($_.Description  -match $DeviceDesc)})
If ($FindTWAIN.Name -match $DeviceDesc){
$DeviceID = ($FindTWAIN.DeviceID).Replace("\","#")
$ClassGuid = $FindTWAIN.ClassGuid
$FullCommand = "`"C:\WINDOWS\system32\rundll32.exe`" fdprint,InvokeTask /ss `"\\?\"
$Params = "`"\\?\"+$DeviceID+"#"+$ClassGuid+"`""
& "C:\WINDOWS\system32\rundll32.exe" fdprint,InvokeTask /ss $Params
}

If ($FindTWAIN.Name -Notmatch $DeviceDesc){
#######################
$MsgBoxInfo = "$DeviceDesc device not found..."
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
[Microsoft.VisualBasic.Interaction]::MsgBox($MsgBoxInfo, "OKOnly,SystemModal,Exclamation", "Nothing Found.") | Out-null
 ## Out-null just supresses the 'ok' to the (ISE?) screen, after pressing the OK button.
#######################
}