簡體   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