Saturday, September 1, 2018

Powershell: Show Windows version build info, with UBR

I wanted to pull in all of the Windows version info into a variable...
Release Number, Build Number, and UBR / 'Update Build Revision'
https://2.bp.blogspot.com/-K8ywTdl-FOM/W4vxEU5ycRI/AAAAAAABxLg/IFSG-lio-y0jHHETkQhXXP-CyZEfaUyXwCLcBGAs/s400/OSv04.jpg








These standard commands were lacking -
  • Get-WindowsEdition -Online 
  • [environment]::OSVersion
  • [environment]::OSVersion.Version
  • (Get-CimInstance Win32_OperatingSystem)version

The 'Release Number' and UBR don't show up.

The UBR is not super useful, except for adding completeness...
But the 'Release Number' is what most Windows version conversations rely on.

Sure, one can type 'winver', and see it in a pop-up window...
But I wanted something more tangible, and portable... As a $variable

Mostly - I wanted figure out how to get to this info from the command-line...

Anyway - It's in the Registry...
This does not require elevation. And it can be pulled in with one line (well two, but really one):
Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' `
|
select ReleaseID,CurrentBuildNumber,UBR
https://2.bp.blogspot.com/-jaQmfLwCBPc/W4tck4BhYGI/AAAAAAABxK8/8V-NzOGx_Ucz12foZfsMsXdORSQJnkfxQCEwYBhgL/s1600/OSv02.jpg

 


Or it can be prettied up with three lines:
$Reg_NT_CurVer = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' `
|
select ReleaseID,CurrentBuildNumber,UBR
$BuildNo = ($Reg_NT_CurVer.CurrentBuildNumber)+"."+($Reg_NT_CurVer.UBR)
Write-Host " Version" $Reg_NT_CurVer.ReleaseID "(OS Bulld" $BuildNo") " -BackgroundColor Black
# On lines '1' and '2' above, that select is not really needed - I just put it there for clarity
https://4.bp.blogspot.com/-nSh8vHsEjSI/W4tcwYdU3sI/AAAAAAABxLA/hPyeRt9CRXoGsPgzJb7J2myaVtIlzsfKwCEwYBhgL/s1600/OSv01.jpg



Some additional notes:
# ReleaseID / Release is coded 'YYMM' - So, for example: '1803', was released 2018 March
# CurrentBuildNumber / CBN
# UBR is 'Update Build Revision'

# Below is just another way of putting the same info on screen.
# I re-labled the headers. The 'Out-String trim', just removes extra lines'

Write-Host "~~~~~~~~~~~~~~" -ForegroundColor Yellow
(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' `
|
FL @{L='Release';E={$_.ReleaseID}},@{L='CBN';E={$_.CurrentBuildNumber}},@{L='UBR';E={$_.UBR}} `
| Out-String).Trim()     # (FL = format-list)

Write-Host "~~~~~~~~~~~~~~" -ForegroundColor Yellow
(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' `
|
FT @{L='Release';E={$_.ReleaseID}},@{L='CBN';E={$_.CurrentBuildNumber}},@{L='UBR';E={$_.UBR}} `

| Out-String).Trim()     # (FT = format-table)
https://4.bp.blogspot.com/-B4w7ImrZXWc/W4tdeQe6pNI/AAAAAAABxLI/J2hUr5Y1RhEQ4Hp-ritC1Lp8VMjghnUjQCLcBGAs/s1600/OSv03.jpg


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.
#######################
}