繁体   English   中英

Powershell脚本提取远程PC序列号,然后与xlsx文件匹配,然后与另一个文件匹配,仅输出所需的信息

[英]Powershell script to pull remote pc serial number then match against an xlsx file then match against another and output only needed info

嘿,我正在尝试构建一个Powershell脚本,该脚本将从远程PC中提取一个序列号,然后将其与xlsx文件进行匹配,然后再将该列与另一个xlsx文件进行匹配,我已经到了可以拉远远程sn和将所有内容都放入csv输出中,但是我遇到了与数据匹配的问题,然后根据匹配进行过滤,然后仅输出我需要的脚本新内容,因此我很确定它比其他任何东西都缺乏经验,这是我的代码至今

    $computers = Get-Content c:\script\computerlist.txt
    Get-wmiobject Win32_Bios -ComputerName $computers | Select-Object __SERVER, SerialNumber| Format-Table |out-file C:\script\computerinfo.txt

    $computerinfo = Import-Excel C:\script\compDB.xlsx
    $userinfo = Import-Excel C:\script\userDB.xlsx

    $Computerinfo[2].SERIAL -eq
    $Computerinfo[2].DATE_ADDED
    $Computerinfo[2].OS
    $Computerinfo[2].MODEL
    $Computerinfo[2].USER
    $userinfo[2].NAME_FIRST
    $userinfo[2].NAME_LAST
    $userinfo[2].NT_USERID

    ''
    'Computer Info' 
    '----------'

     $computerinfo ,$userinfo | Format-Table - | Out-File c:\script\computerinfo.csv

首先,您需要将wmi信息保存到变量中:

$WMIinfo = Get-wmiobject Win32_Bios -ComputerName $computers | Select-Object __SERVER, SerialNumber

然后,您将需要遍历电子表格并与计算机电子表格中的数据进行比较。 如果匹配,则循环遍历用户电子表格以进行匹配:

foreach ($CompEntry in $Computerinfo) {
    if ($WMIinfo.serial -eq $CompEntry.serial) {
       foreach ($UserEntry in $userinfo) {
           if ($UserEntry.NT_USERID -eq $CompEntry.USER) {
                #output information you want here
           }
       }
    }

我会尽力帮助您到达那里。

我创建了一个名为Serial.xslx的Excel文件。 这是它的样子

SerialNumber    DeployedTo
212             Ham
4M24N32         Stephen

然后,我将其导入为$list

$list = import-excel C:\temp\serial.xlsx

接下来,要获取Win32_Bios信息,因此可以获取SerialNumber属性。

$bios = get-WmiObject Win32_Bios   

最后,我将筛选$list (包含Excel文件),并找到一行具有与此计算机序列号匹配的SerialNumber的行。 如果找到匹配的记录,则获取该记录的.DeployedTo值。

$user = $list | Where SerialNumber -eq $bios.SerialNumber | Select -ExpandProperty DeployedTo

剩下的就是证明它有效。

"the computer with serial $($bios.SerialNumber) is deployed to $user"

>the computer with serial 4M24N32 is deployed to Stephen

现在,您有两个单独的excel文件,因此我可以手动将它们合并为一个,或者重复相同的基本方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM