Get Windows 7 Product Key Powershell

329 Views Asked by At

I'm trying to write up a health report to more easily see an overview of all pcs under my control.

This is what i have so far:

$ErrorActionPreference = "silentlycontinue"
$ComputerList = get-content C:\Users\Administrator\Desktop\DavidsScripts\FloorHealth\input.txt
$collectionVariable = New-Object System.Collections.ArrayList

ForEach ($Computer in $ComputerList) {
# Create temp object
$temp = New-Object System.Object
# Add members to temp object

$temp | Add-Member -MemberType NoteProperty -Name "Keys" (Get-WmiObject -query ‘select * from 
SoftwareLicensingService’).OA3xOriginalProductKey
$temp | Add-Member -MemberType NoteProperty -Name "PC" (Get-WMIObject -ComputerName $Computer 
Win32_ComputerSystem | Select-Object -ExpandProperty name)
$temp | Add-Member -MemberType NoteProperty -Name "IP" -Value $Computer
$temp | Add-Member -MemberType NoteProperty -Name "MACAddress" -Value (gwmi -ComputerName $Computer 
-Class Win32_NetworkAdapterConfiguration | where {$_.IPAddress -like "$Computer"}).MACAddress
$temp | Add-Member -MemberType NoteProperty -Name "Ram" (Get-WMIObject -class Win32_PhysicalMemory - 
ComputerName $Computer | Measure-Object -Property capacity -Sum | % {[Math]::Round(($_.sum / 
1GB),2)})
$temp | Add-Member -MemberType NoteProperty -Name "Firewall" -Value ((netsh -r $Computer advfirewall 
show currentprofile state)[3] -replace 'State' -replace '\s')
# Add the temp object to ArrayList
$collectionVariable.Add($temp)
}

Write-Output $collectionVariable
$collectionVariable | Export-Excel -now 
C:\Users\Administrator\Desktop\DavidsScripts\FloorHealth\floorhealth.xlsx

Im Having a hard time trying to get the windows Product Key. (sorry about the formatting on the first 1 couldn't keep the longer lines on there own line)

I wrote up a second script but not quite sure how to add it to my existing without just pasting the code into it. Can i call it and run the script from inside my health report then split out just the product key, then send it into excel?

($targets = get-content C:\Users\Administrator\Desktop\DavidsScripts\FloorHealth\input.txt)
$hklm = 2147483650
$regPath = "Software\Microsoft\Windows NT\CurrentVersion"
$regValue = "DigitalProductId"
Foreach ($target in $targets) {
    $productKey = $null
    $win32os = $null
    $wmi = [WMIClass]"\\$target\root\default:stdRegProv"
    $data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
    $binArray = ($data.uValue)[52..66]
    $charsArray = 
"B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9"
    ## decrypt base24 encoded binary data
    For ($i = 24; $i -ge 0; $i--) {
        $k = 0
        For ($j = 14; $j -ge 0; $j--) {
            $k = $k * 256 -bxor $binArray[$j]
            $binArray[$j] = [math]::truncate($k / 24)
            $k = $k % 24
        }
        $productKey = $charsArray[$k] + $productKey
        If (($i % 5 -eq 0) -and ($i -ne 0)) {
            $productKey = "-" + $productKey
        }
    }
    $obj = New-Object Object
    $obj | Add-Member Noteproperty ProductKey -value $productkey
    $obj
}
2

There are 2 best solutions below

0
DarkLite1 On BEST ANSWER

First of all I would split things up, so it's more clear what function is doing what job. The advantage of using Functions is that you don't need a separate script file for each functionality.

Something like this:

Function Get-WindowsProductKey {
    Param (
        [String[]]$ComputerName = $env:COMPUTERNAME
    )

    Function Convert-ProductKey {
        Param (
            [Parameter(Mandatory)]
            [String[]]$DigitialProductID,
            [String[]]$charsArray = @(
                "B", "C", "D", "F", "G", "H", "J", "K", "M",
                "P", "Q", "R", "T", "V", "W", "X", "Y", "2",
                "3", "4", "6", "7", "8", "9"
            )
        )
        ## decrypt base24 encoded binary data
        For ($i = 24; $i -ge 0; $i--) {
            $k = 0
            For ($j = 14; $j -ge 0; $j--) {
                $k = $k * 256 -bxor $DigitialProductID[$j]
                $DigitialProductID[$j] = [math]::truncate($k / 24)
                $k = $k % 24
            }
            $productKey = $charsArray[$k] + $productKey
            If (($i % 5 -eq 0) -and ($i -ne 0)) {
                $productKey = "-" + $productKey
            }
        }

        $productKey
    }

    Function Get-DigitalProductID {
        Param (
            [Parameter(Mandatory)]
            [String]$ComputerName,
            $hklm = 2147483650,
            $regPath = "Software\Microsoft\Windows NT\CurrentVersion",
            $regValue = "DigitalProductId"
        )

        $wmi = [WMIClass]"\\$C\root\default:stdRegProv"
        $data = $wmi.GetBinaryValue($hklm, $regPath, $regValue)
        ($data.uValue)[52..66]
    }

    Foreach ($C in $ComputerName) {
        $DigitalProductID = Get-DigitalProductID -ComputerName $C
        $ProductKey = Convert-ProductKey -DigitialProductID $DigitalProductID

        [PSCustomObject]@{
            ComputerName = $C
            ProductKey   = $ProductKey
        }
    }
}

Function Get-ComputerDetail {
    Param (
        [String[]]$ComputerName = $env:COMPUTERNAME
    )

    ForEach ($C in $ComputerName) {
        $Mac = (Get-WMIObject -ComputerName $C -Class 'Win32_NetworkAdapterConfiguration' | Where-Object { $_.IPAddress -like "$C" }).MACAddress

        $Ram = (Get-WMIObject  -ComputerName $C -class 'Win32_PhysicalMemory' |
            Measure-Object -Property capacity -Sum |
            ForEach-Object { [Math]::Round(($_.sum / 1GB), 2) })

        $Firewall = ((netsh -r $C advfirewall show currentprofile state)[3] -replace 'State' -replace '\s')

        $Keys = (Get-WmiObject -ComputerName $C -Query 'select * from SoftwareLicensingService').OA3xOriginalProductKey

        [PSCustomObject]@{
            ComputerName      = (Get-WMIObject -ComputerName $C -class  'Win32_ComputerSystem').Name
            IP                = $C
            Keys              = $Keys
            MACAddress        = $Mac
            RAM               = $Ram
            Firewall          = $Firewall
            WindowsProductKey = Get-WindowsProductKey -ComputerName $C
        }
    }
}

$ComputerNames =  Get-Content -Path 'C:\Users\Administrator\Desktop\DavidsScripts\FloorHealth\input.txt)'
$ComputerDetails = Get-ComputerDetail -ComputerName $ComputerNames
$ComputerDetails | Export-Excel -Now
0
Ranadip Dutta On

Definitely you can call other script from your main script like:

Inside your main script use &(call operator)

& "path\of\the\script\myScript.ps1"

Also, if you have a function in the second script , then my recommendation is to just compile the function of the second script in your main script by using DOT Sourcing like:

Assuming your function is in the second script as this and your name of the file as functionscript.ps1:

function Do-Something {
  param($Thing)
  Write-Output "My output: $Thing"
  }

Now, in your main script use:

. \path\of\the\script\functionscript.ps1
Do-Something -Thing  "Test-thing"

See the Dot, that will compile the function and you can call the function of the second script in the main script easily.

Also, for product key, I would prefer to use the wmic but DarkLite's product key function is neat and clean and you can incorporate the same using dot sourcing and you can call the function.

Hope it helps.