首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取Windows 7产品密钥Powershell

获取Windows 7产品密钥Powershell
EN

Stack Overflow用户
提问于 2019-10-11 16:07:31
回答 2查看 180关注 0票数 0

我正在尝试写一份健康报告,以便更容易地查看我控制下的所有pcs的概况。

这就是我到目前为止所知道的:

代码语言:javascript
复制
$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

我在尝试获取windows产品密钥时遇到困难。(很抱歉,前1行的格式无法将较长的行保留在自己的行上)

我写了第二个脚本,但不太确定如何在不粘贴代码的情况下将其添加到现有的脚本中。我可以调用它并从我的健康报告中运行脚本,然后拆分出产品密钥,然后将其发送到excel中吗?

代码语言:javascript
复制
($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
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-11 16:28:03

首先,我会把它们分开,这样就能更清楚地知道哪个函数在做什么工作。使用Functions的好处是,您不需要为每个功能使用单独的脚本文件。

如下所示:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2019-10-11 16:36:54

当然,你也可以从你的主脚本中调用其他脚本,比如:

在主脚本中使用&(调用操作符)

代码语言:javascript
复制
& "path\of\the\script\myScript.ps1"

另外,如果你在第二个脚本中有一个函数,那么我的建议是在你的主脚本中使用DOT Sourcing编译第二个脚本的函数,如下所示:

假设您的函数位于第二个脚本中,文件名为functionscript.ps1

代码语言:javascript
复制
function Do-Something {
  param($Thing)
  Write-Output "My output: $Thing"
  }

现在,在您的主脚本中使用:

代码语言:javascript
复制
. \path\of\the\script\functionscript.ps1
Do-Something -Thing  "Test-thing"

请看点,它将编译函数,您可以轻松地在主脚本中调用第二个脚本的函数。

此外,对于产品密钥,我更喜欢使用wmic,但DarkLite的产品密钥函数整洁干净,您可以使用点采购合并相同的密钥函数,您可以调用该函数。

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58336884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档