简体   繁体   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

hey im trying to build a powershell script that will pull a serial number from a remote pc and then match that against an xlsx file which would then match a column against another xlsx file i have gotten to the point where i can pull the remote sn and have everything put in to a csv output but i am having issues matching the data then filtering based on the match and then outputting only what i need im new to scripting so im pretty sure its more my lack of experiance than anything else this is my code so far 嘿,我正在尝试构建一个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

First you need to save the wmi information to a variable: 首先,您需要将wmi信息保存到变量中:

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

Then you would need to loop through the spreadsheet and compare to data in Computer spreadsheet. 然后,您将需要遍历电子表格并与计算机电子表格中的数据进行比较。 If it matches loop through user spreadsheet for match: 如果匹配,则循环遍历用户电子表格以进行匹配:

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

I'll try to help you get there. 我会尽力帮助您到达那里。

I created an Excel file called Serial.xslx. 我创建了一个名为Serial.xslx的Excel文件。 Here's what it looks like 这是它的样子

SerialNumber    DeployedTo
212             Ham
4M24N32         Stephen

I then import this as $list . 然后,我将其导入为$list

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

Next,to get the Win32_Bios info, so I can grab the SerialNumber property. 接下来,要获取Win32_Bios信息,因此可以获取SerialNumber属性。

$bios = get-WmiObject Win32_Bios   

Finally, I'll filter through the $list (which contains the Excel file), and find a row which has a SerialNumber that matches this computers serial number. 最后,我将筛选$list (包含Excel文件),并找到一行具有与此计算机序列号匹配的SerialNumber的行。 If I find a matching one, then I grab the .DeployedTo value for that record. 如果找到匹配的记录,则获取该记录的.DeployedTo值。

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

All that remains is to demonstrate that it works. 剩下的就是证明它有效。

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

>the computer with serial 4M24N32 is deployed to Stephen

Now, you've got two separate excel files, so I would either manually join them into one, or repeat this same basic approach. 现在,您有两个单独的excel文件,因此我可以手动将它们合并为一个,或者重复相同的基本方法。

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

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