Jump to content

cmd line to pull serial number from remote device


Recommended Posts

Hello friends,

Long time no see, sorry I've been gone, and only come back when I need help.

 

I'm trying to grab serial numbers to help with inventory. We have multiple locations with 4 to 14 devices. Inventory has never been a high priority, but I was hired to try to tackle it. I am getting a error code of 0, which I believe means the cmd didn't run successfully.

$sclientname comes from a ping test that part is working. $admin account and password comes from a gui input. I've tested that those are good as well. So I'm doing something wrong with my send. Maybe @comspec would work better? I tried it, and I'm getting the same issue. I'm missing something here. Thanks for any help provided, please let me know if more code is needed to work through this.

$domain = @LogonDNSDomain
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                $sWMICquery = "wmic /node:" & $sClientName & " /user:" & $adminAccount & " /password:" & $adminPassword & " bios get serialnumber /format:value"
            Local $iPID = RunAs($adminAccount,$domain,$adminPassword,2,'cmd.exe',$sWMICquery)

 

Thanks for your time

 

Link to comment
Share on other sites

@JayHawkfl
Without "something" that runs your command, it's quite normal that RunAs() function doesn't return anything.
You could use @ComSpec as well as the WMIC object to do such thing.

Here's an example with the first method:

#include <AutoItConstants.au3>
#include <StringConstants.au3>

Global $strCommand = "wmic bios get serialnumber /format:value", _
       $intPID, _
       $strStdOutput, _
       $strSerialNumber


$intPID = Run(@ComSpec & " /c " & $strCommand, "", @SW_HIDE, $STDOUT_CHILD)
If @error Then
    ConsoleWrite("!Error running cmd! Error: " & @error & @CRLF)
Else

    While 1
        $strStdOutput &= StdoutRead($intPID)
        If @error Then ExitLoop
    WEnd

    $strSerialNumber = StringRegExp($strStdOutput, 'SerialNumber=(\V+)', $STR_REGEXPARRAYMATCH)[0]

    ConsoleWrite($strSerialNumber & @CRLF)

EndIf

:)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Edit - I think I got it, give me 20 minutes to make my changes

 

I had it. I got it working, and then I changed something and now I don't know what I've done. I've tried it with and without the ' /c'. I think that part needs to be out for what ever reason. 

I got it to work with a cmd line export.

RunWait(@ComSpec & 'wmic /node:' & $sClientName &' bios get serialnumber > c:\users\abs\desktop\'&$sClientName&' serial.txt',"", @SW_HIDE)

But it's giving me 0 error codes again.

$sWMICquery ="wmic /node:" &  $sClientName & " bios get serialnumber"
                MsgBox(0,'',$sWMICquery)
                ;$sWMICquery = "wmic bios get serialnumber"
            Local $iPID = RunWait(@ComSpec & ' /c' & $sWMICquery,"",@SW_SHOW)
            MsgBox(0,'153',$iPID)
                While ProcessExists($iPID)
                    $sOutput = StdoutRead($iPID)
                WEnd
                MsgBox(0,'157',$iPID)

I'll keep going on it shortly, I just gotta walk away from this for a minute.

 

Thanks again for any and all help.

Edited by JayHawkfl
I think I got it working

Thanks for your time

 

Link to comment
Share on other sites

this is a combination of my ping set up and cmd line pull from below. I probably still have some extraneous lines in here from my last version but it works. I will fine tune it in the coming weeks, but I gotta get this project started before my bosses rip me up.

 

Thanks for the help @francescodimuro

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=QuickInv0_1.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;This will grab information from the network from our network spread sheets
;Things to clean up
;After the second while loop, the ip results array will be larger then needed. https://www.autoitscript.com/forum/topic/132027-array-delete-blank-element/ Hannes08 had a good idea posted august 18 of 2011
;Add 30 second timer after the msgbox indicating timer
#include <Array.au3> ; Required for _ArrayDisplay only.
#include <AutoItConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <Inet.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <WinAPIFiles.au3>
#include <WindowsConstants.au3>

Global $VersionNumber = "1.0"
Global $varArp  ;store results from Arp CMD
Global $arrayArp
Global $sFilePath = @ScriptDir ; Search the current script directory.
Global $localIP = @IPAddress1
Global $varOctet1
Global $varOctet2
Global $varOctet3
Global $varOctet4
Global $i = 151 ;counter
Global $m = 1 ;secondary counter
Global $IParray[8] = ['192','168','200','21']
Global $pingTestResult[255]
Global $IPListArray[255]
Global $deviceName[0]
Global $filePathExcel = @ScriptDir& "\_netExport.html"
Global $deviceNameStorage = ''
Global $sClientName = "10.70.140.21"
Global $sUserID = 0
Global $sPassword = 0
Global $sOutput = ""
Global $iniPath = @ScriptDir&'\NetworkDiscovery.ini'
Global $sWMICquery = "Test"
Global $proceedOrCancel
Global $adminAccount = "aaaa"
Global $adminPassword = "aaa"
Global $inilist = 0
$proceedOrCancel = MsgBox(17,"Warning",'This will delete any previous Network scans in the '& @ScriptDir&'                                                                                                                       OK to proceed, cancel to exit and save previous data elsewhere.')
If $proceedOrCancel <> 1 Then
    Exit
EndIf

FileDelete($iniPath)

TCPStartup()

If $localIP = "127.0.0.1" Then
    MsgBox(0,"Invalid IP for this application","The IP address is a loopback Address. Please enter IPv4 address manually")
    Run(@ComSpec &' /k ipconfig')
    Sleep(250)
;MsgBox(0,"",$localIP)
$Form1 = GUICreate("IP Input", 485, 178, 192, 124)
$Input1 = GUICtrlCreateInput("192", 32, 64, 121, 21)
$Input2 = GUICtrlCreateInput("168", 168, 64, 121, 21)
$Input3 = GUICtrlCreateInput("200", 304, 64, 121, 21)
$Button1 = GUICtrlCreateButton("You got it", 96, 128, 235, 25)
$Label1 = GUICtrlCreateLabel("X", 435, 68, 14, 17)
$Label2 = GUICtrlCreateLabel("Please enter the First three octets from the IPv4 address, with out any '.'", 48, 32, 347, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $varOctet1 = GUICtrlRead($Input1)
            $varOctet2 = GUICtrlRead($Input2)
            $varOctet3 = GUICtrlRead($Input3)
            GUIDelete($Form1)
            ExitLoop
    EndSwitch
WEnd
EndIf

$localIPString = IsString($localIP)

If $localIPString <> 1 Then
    MsgBox(0,"","Making an adjustment, one moment.",1)
    $localIP = String($localIP)
    Sleep(750)
    MsgBox(0,"","Done, lets keep moving.",1)
EndIf

FileCopy(@ScriptDir&'\config.ini','\\masterpc\C$\scripts',1)
$IParray = StringSplit($localIP,'.',3)
$varOctet1 = $IParray[0]
$varOctet2 = $IParray[1]
$varOctet3 = $IParray[2]
$varOctet4 = $IParray[3]

PingAll()

ResetTimers()

while $i <254
    $IPtoPing = $varOctet1&'.'&$varOctet2&'.'&$varOctet3&'.'&$i
    $pingtemp = Ping($IPtoPing,15)
    if $pingtemp <> 0 Then
        $IPListArray[$m] = $pingtemp
        $pingTestResult[$m] = $IPtoPing
        $sIp = TCPNameToIP($IPtoPing)
        $sResult = _TCPIpToName($sIp)
        IniWrite($iniPath,$sResult,'IP Address',$IPtoPing)
        $iniStorage = $sResult&'Discovered.ini'
        _ArrayAdd($inilist,$iniStorage)
        _ArrayAdd($deviceName,$sResult)
        $sClientName = $IPtoPing
        $domain = @LogonDNSDomain
        RunWait(@ComSpec & ' /c ' & 'wmic /node:' & $sClientName &' bios get serialnumber > %temp%\serial.txt' ,"", @SW_HIDE)
        $file=(@TempDir & "/serial.txt")
        $fileread= FileRead($file)
        IniWrite($iniPath,$sResult,'',$fileread)
        ;MsgBox(0, $sClientName , $fileread)
        FileDelete(@TempDir & "/serial.txt")
        $m = $m + 1
    EndIf
    $i = $i + 1
WEnd

Func ResetTimers()
    $i = 1
    $m = 1
EndFunc

func PingAll()
    $IPtoPing = $varOctet1&'.'&$varOctet2&'.'&$varOctet3&'.'&$i
    While $i <> 256
        $IPtoPing = $varOctet1&'.'&$varOctet2&'.'&$varOctet3&'.'&$i
        Ping($IPtoPing,5)
        $i = $i+1
    WEnd
EndFunc

 

Thanks for your time

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...