Sign in to follow this  
Followers 0
Valiante

WMI: SWbemLocator Object Syntax?

11 posts in this topic

I've searched the forums and have tried using example code word for word, but I'm persistently presented with the same error. Am I missing something?

CODE:

$strComputer = @ComputerName
$strUser = "username"
$strPassword = "password"

$wmiLocator = ObjCreate("WbemScripting.SWbemLocator")
$wmiService = $wmiLocator.ConnectServer($strComputer, "root\cimv2", $strUser, $strPassword)

ERROR:

>"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "F:\AU3\wmiLocator.au3"   
F:\AU3\wmiLocator.au3 (6) : ==> The requested action with this object has failed.: 
$wmiService = $wmiLocator.ConnectServer($strComputer, "root\cimv2", $strUser, $strPassword) 
$wmiService = $wmiLocator.ConnectServer($strComputer, "root\cimv2", $strUser, $strPassword)^ ERROR
>Exit code: 1   Time: 1.908

Share this post


Link to post
Share on other sites



@Valiante

This should get you going

$strComputer = "."
$strUser = ""
$strDomain = "domain"
$strPassword = ""

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF

$objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
$objWMIService = $objSWbemLocator.ConnectServer($strComputer,"root\CIMV2",$strUser, $strPassword) ;, "MS_409", "ntlmdomain:" & $strDomain)
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")

For $objItem in $colItems 
    ConsoleWrite($Output & $objItem.Name & @LF & @LF) ; returns the local CMP name !!
Next

Regards,

ptrex

1 person likes this

Share this post


Link to post
Share on other sites

Thanks ptrex.

Unfortunately that's pretty much the same code syntax-wise, and as such I get exactly the same error.

Note: This function works with VBscript so I know it's not the system at fault here, but I have strong to desire to port this over to AutoIt for user-friendliness and expansion.

Share this post


Link to post
Share on other sites

Thanks ptrex.

Unfortunately that's pretty much the same code syntax-wise, and as such I get exactly the same error.

Note: This function works with VBscript so I know it's not the system at fault here, but I have strong to desire to port this over to AutoIt for user-friendliness and expansion.

Post the exact VBScript code for the same function for comparison. The code by ptrex works fine for me, because I am logged in as an Admin. If I put username/password credentials instead of "", it fails. So I wonder about impersonate flags, etc., giving permissions here. Seeing the working VBScript would help.

muttley

PS: This version of your AutoIt script also works fine with null username/password, but fails with valid credentials:

$strComputer = @ComputerName
$strUser = ""
$strPassword = ""

$wmiLocator = ObjCreate("WbemScripting.SWbemLocator")
$wmiService = $wmiLocator.ConnectServer($strComputer, "root\cimv2", $strUser, $strPassword)
If IsObj($wmiService) Then
    ConsoleWrite("Debug: $wmiService is an object type: " & ObjName($wmiService) & @LF)
Else
    ConsoleWrite("Debug: Error:  $wmiService is not an object.")
EndIf

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Haha - I was substituting the otherwise blank values with my username & password. If I literally copy & paste the above, leaving them blank, it does indeed work. But what do I do about connecting to another machine (which is the main aim of this script)?

Share this post


Link to post
Share on other sites

Sorry for the double-post, but I thought I should quote the VBScript as requested by PsaltyDS.

Basically what it's for is to enable the Server service (which is disabled by policy in our domain) so we can temporarily do some remote admin where needed.

This VBScript works;

strComputer=InputBox("Please enter the target computername ",,"")
strUser=InputBox("Please enter the username you wish to connect with ")
strDomain=InputBox("Please enter the domain for " &strUser)
strPassword=InputBox("Please enter the password for " & strDomain&"\"&strUser)

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",strUser, strPassword)
If Err.Number<>"0" Then WScript.Echo "Cannot find machine: "& strComputer & WScript.Quit

Set WindowsServices = objWMIService.ExecQuery("Select * from Win32_Service where Name='LanmanServer'")

For each strService in WindowsServices
    StrService.ChangeStartMode("manual")
    strService.StartService()
Next

Share this post


Link to post
Share on other sites

@all

Without the COM error handler it is hard to know where you are going.

Regards

ptrex

Good point:
$strComputer = @ComputerName
$strUser = ""
$strPassword = ""

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc"); Install a custom error handler

$wmiLocator = ObjCreate("WbemScripting.SWbemLocator")
$wmiService = $wmiLocator.ConnectServer($strComputer, "root\cimv2", $strUser, $strPassword)
If IsObj($wmiService) Then
    ConsoleWrite("Debug: $wmiService is an object type: " & ObjName($wmiService) & @LF)
Else
    ConsoleWrite("Debug: Error:  $wmiService is not an object.")
EndIf

; This is my custom error handler 
Func MyErrFunc() 
   $HexNumber=hex($oMyError.number,8) 
   Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
                "Number is: " & $HexNumber & @CRLF & _
                "Windescription is: " & $oMyError.windescription ) 

   $g_eventerror = 1; something to check for when this function returns 
Endfunc

I get error code 80020009 if using credentials, which is just query failed - I think.

muttley


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Haha - I was substituting the otherwise blank values with my username & password. If I literally copy & paste the above, leaving them blank, it does indeed work. But what do I do about connecting to another machine (which is the main aim of this script)?

Just noticed something new: I works fine on a remote computer with credentials. It only fails with credentials on the LOCAL computer! I suspect it just doesn't want to create DUPLICATE authentication to the same machine, since you're already logged in to it. Try logging on as a different user than the credentials you are using and see if that gets you access to the local stuff.

muttley

1 person likes this

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Hmm... interestingly, if I specify an XP workstation as the target machine, it works, with or without credentials, as I'm a local admin on all our XP workstations. However, if I target a Vista machine (with UAC on and Server service disabled) it errors as above.

Next step: Try running the script from an XP workstation, then again from Vista with UAC switched off. I'll post the results.

Share this post


Link to post
Share on other sites

Ok, ignore that - the machine I was targeting originally was using a different IP address to the one registered in DNS (we have DNS issues here).

I found a machine that was responding to ping with the correct address and when targeting that with admin credentials, all is well.

So it was a networking issue after all. Dear me.

Share this post


Link to post
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
Sign in to follow this  
Followers 0