Jump to content

Help with script that queries WMI then copies files based on WMI query result


rmattson
 Share

Recommended Posts

I'm new to AutoIt and I'm trying to create a script/exe that is used in a login script. The exe would do a WMI query for the Model name of the PC and if that returned model name matches a particular value which in this instance is OptiPlex 160, then the script would continue and check for the existence of certain files. If the WMI query for the model name didnt match the value defined in the script which would be OptiPlex160 then the script would exit. I'm using the AutoIt scriptomatic tool which I can export the WMI portion from and trim to just query for the Model info. but I have no idea what all those values are and how to change the WMI query to not export to msgbox. What I'm needing help with is declaring the value in the script that I need the WMI query to either match and continue or not match to end the script. Any assistance would be greatly appreciated.

Link to comment
Share on other sites

If _IsOptiplex_160() Then
    MsgBox( 4096, "Result", "The system is an Optiplex 160")
    ;; Replace the MsgBox() with your file check code here
Else
    Exit
EndIf


Func _IsOptiplex_160()
    Local $objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
    Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", 0x10 + 0x20)

    If IsObj($colItems) Then
        For $objItem In $colItems
           If StringRegExp($objItem.Model, "(?i)^.*optiplex\s?160") Then
        Return True
            Else
                Return False
            EndIf
        Next
    Else
        Return False
    EndIf
EndFunc   ;==>_IsOptiplex_160

EDIT:

Removed reference to $Output that should not have been there.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

If _IsOptiplex_160() Then
    MsgBox( 4096, "Result", "The system is an Optiplex 160")
    ;; Replace the MsgBox() with your file check code here
Else
    Exit
EndIf


Func _IsOptiplex_160()
    Local $objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
    Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", 0x10 + 0x20)

    If IsObj($colItems) Then
        For $objItem In $colItems
           If StringRegExp($objItem.Model, "(?i)^.*optiplex\s?160") Then
        Return True
            Else
                Return False
            EndIf
        Next
    Else
        Return False
    EndIf
EndFunc   ;==>_IsOptiplex_160

EDIT:

Removed reference to $Output that should not have been there.

Thank you for the prompt response. There is a bit of confusion my part as I'm not sure if I'm looking at this correctly you commented on line 3 that I should replace the MsgBox() with my file check code. Is the line starting with Func_IsOptiplex_160() where the WMI query starts?
Link to comment
Share on other sites

I wrapped your query in a function that returns True or fals. If the function returns true then it's an Optiplex 160 otherwise it's not. You place your file check code where I have the message box. It will only run that code if the function returned True otherwise it will Exit.

That seems to be pretty much what you asked for.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Well it seems that the script or function isn't working right as when I put in my file copy based on the result of the WMI query for the Model name it runs no matter the returned Model type from WMI. I dont know if it matters or not but on Dell PCs the entry for Model in Win32_ComputerSystem has 17 blank spaces before the end of the string or quotation mark. I even had to modify my WMI filter in group policy to include the 17 spaces before the last single quotation mark. Here is a export from WMICodeCreator:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colItems = objWMIService.ExecQuery( _

"SELECT * FROM Win32_ComputerSystem WHERE Model = 'Latitude E5400 '",,48)

For Each objItem in colItems

Wscript.Echo "-----------------------------------"

Wscript.Echo "Win32_ComputerSystem instance"

Wscript.Echo "-----------------------------------"

Wscript.Echo "Model: " & objItem.Model

Next

Edited by rmattson
Link to comment
Share on other sites

using the RegEx that I gave you, the trailing spaces won't matter at all. Since we are checking for the model number, you also shouldn't need a WHERE statement in the query. I'm also running a Dell system (different model) and the code I gave returned properly when I changed the expression to check for my model.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I'm still not sure why the file copy I have is running any Model of PC after "If _IsOptiplex_160() Then" is processed. Here is the code:

$OURCE=("\\path\sshtml.scr")

$OURCE5=("\\path\scripts\saver")

$SSCR=("@WindowsDir\system32\sshtml.scr")

$AVER=("c:\saver")

If _IsOptiplex_160 () Then ;If the PC is a OptiPlex 160 continue the script

;~ MsgBox( 4096, "Result", "The system is an Optiplex 160")

;; Replace the MsgBox() with your file check code here

ElseIf Not FileExists($SSCR) OR ($AVER) Then

FileCopy($OURCE,@SystemDir,9)

DirCopy($OURCE5,$AVER,9)

Else

Exit

EndIf

Func _IsOptiplex_160 ()

Local $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")

Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", 0x10 + 0x20)

If IsObj($colItems) Then

For $objItem In $colItems

If StringRegExp($objItem.Model, "(?i)^.*optiplex\s?160 ") Then

Return True

Else

Return False

EndIf

Next

Else

Return False

EndIf

EndFunc ;==>_IsOptiplex_160

Link to comment
Share on other sites

Extremely bad usage of an IF/EndIf call is the problem

$OURCE=("\\path\sshtml.scr")
$OURCE5=("\\path\scripts\saver")
$SSCR=("@WindowsDir\system32\sshtml.scr")
$AVER=("c:\saver")


If _IsOptiplex_160 () Then ;If the PC is a OptiPlex 160 continue the script
If FileExists($SSCR) = 0 OR FileExists($AVER) = 0 Then
FileCopy($OURCE,@SystemDir,9)
DirCopy($OURCE5,$AVER,9)
EndIf
Else
Exit
EndIf


Func _IsOptiplex_160 ()
Local $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", 0x10 + 0x20)

If IsObj($colItems) Then
For $objItem In $colItems
If StringRegExp($objItem.Model, "(?i)^.*optiplex\s?160 ") Then
Return True
Else
Return False
EndIf
Next
Else
Return False
EndIf
EndFunc ;==>_IsOptiplex_160

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Extremely bad usage of an IF/EndIf call is the problem

$OURCE=("\\path\sshtml.scr")
$OURCE5=("\\path\scripts\saver")
$SSCR=("@WindowsDir\system32\sshtml.scr")
$AVER=("c:\saver")


If _IsOptiplex_160 () Then ;If the PC is a OptiPlex 160 continue the script
If FileExists($SSCR) = 0 OR FileExists($AVER) = 0 Then
FileCopy($OURCE,@SystemDir,9)
DirCopy($OURCE5,$AVER,9)
EndIf
Else
Exit
EndIf


Func _IsOptiplex_160 ()
Local $objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", 0x10 + 0x20)

If IsObj($colItems) Then
For $objItem In $colItems
If StringRegExp($objItem.Model, "(?i)^.*optiplex\s?160 ") Then
Return True
Else
Return False
EndIf
Next
Else
Return False
EndIf
EndFunc ;==>_IsOptiplex_160

Thank you again for all your help. What would cause the file exist/copy to run on non OptiPlex Models though?
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...