I have a very simple WMI test-script that I am running locally on a server to diagnose a problem I'm having getting WMI some data.
Essentially, all WMI queries I run on this machine (locally) fail with an error code 0x80041017.
Option Explicit
Dim WmiQuery
WmiQuery = "SELECT * FROM Win32_Processor"
Dim objSWbemLocator
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Dim objWMIService
Set objWMIService = objSWbemLocator.ConnectServer("localhost", "root\cimv2")
Dim results
Set results = objWMIService.ExecQuery (WmiQuery)
Dim row
For Each row in results
Next
I'm not even trying to view any properties yet, but it fails on line 16, which is the For Each row in results line.
Here's the output of running it in a console:
c:\test>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
c:\test\test.vbs(16, 1) (null): 0x80041017
Running that query through wbemtest gives the description "Invalid query", even though the same query runs on other servers. Is something not registered in WMI or something?
I think it may be to do with how you're getting your WMI object.
How about this:
Option Explicit
Dim wmi,col,itm
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set col = wmi.ExecQuery("Select * from Win32_Processor")
For Each itm in col
WScript.Echo itm.Name
Next
Set wmi = Nothing
Set col = Nothing
WScript.Quit
Seems to work for me... Don't think you need all the WBemLocator guff...
Related
I‘m trying to set via VBS the Win32_NTEventlogFile to keep entries for 10 days. I know I need an elevated shell to modify the security log. I’ve used the samples from Microsoft with this code
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Security)}!\\" & _
strComputer & "\root\cimv2")
Set colLogFiles = objWMIService.ExecQuery _
("Select * from Win32_NTEventLogFile " _
& "Where LogFileName='Security'")
For Each objLogFile in colLogFiles
objLogfile.MaxFileSize = (50*1024)
objLogFile.OverwriteOutDated=10
objLogFile.OverwritePolicy=OutDated
objLogFile.Put_(&H20000)
Next
This works for changing the maximum file size, but it does not work for setting the retention cycle.
When I query OverwriteOutDated again after changing it it says ‚0‘ in WMI. When I access it with powershell PS Get-Eventlog -List
It reports
Retain: -1
OverflowAction: DoNotOverwrite
But when I change it via powershell with
Limit-EventLog -LogName Security -RetentionDays 10 -OverflowAction OverwriteOlder
It works
Retain: 10
OverflowAction: OverwriteOlder
There is no error in the WMI call reported. I just seems to mess up the entry. Am I forgetting something when trying to change the retention cycle via WMI?
I have a Bamboo Plan to execute a VB script which connects to Microsoft SQL Server and performs some operation on SQL server with a return value, Script works fine when i execute manually but fails when i trigger the script from Bamboo Job
the VBScript lies on Window machine and so had configured a remote agent to execute this script Normal VBS scripts works fine except the VBscript with the connection string to DBfails with error message as "Microsoft OLE DB Provider for SQL Server: Login failed for user 'DOMAIN\USERNAME$"
VB Script
Option Explicit
Dim strFile, objFSO, objFile, strConnect, adoSQLConnection, adoSQLRecordset
Dim adoSQLCommand, arrValues, pat, line,cr, var
Dim JobID, Jquery, SQLexecute, SQLTextFile, qfile
Const ForReading = 1, ForWriting = 2
' Specify csv file.
strFile = "D:\MIS_Scripts\DEV_MIS_Scripts\CSV\Domain.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)
' Connection string for SQL database.
strConnect = "Provider=SQLOLEDB.1;Data Source=SQLAU301MEL0435.globaltest.anz.com\TESTAUTOMATION;Initial Catalog=Visualisation;Integrated Security=SSPI"
' Connect to SQL Server database.
Set adoSQLConnection = CreateObject("ADODB.Connection")
adoSQLConnection.ConnectionString = strConnect
adoSQLConnection.Open
' Create Recordset object for SQL Server database.
Set adoSQLRecordset = CreateObject("ADODB.Recordset")
Set adoSQLRecordset.ActiveConnection = adoSQLConnection
Jquery = "select max(JobID) from dbo.Job_Test"
adoSQLRecordset.open Jquery, strConnect
JobID = adoSQLRecordset(0).value
adoSQLRecordset.close
WScript.echo JobID
enter code here
objFile.Close
adoSQLConnection.Close
Manual Execution steps on cmd : cscript filname.vbs
is there any steps i am missing here
Your connection string appears to be using integrated security meaning the SQL Server login will use the account running the script. So, does is the account running the agent setup for the right access to SQL Server?
You can use a username/password and use a plan variables that you send along to the VB Script...
I want to share my setup directory in installshield. I did some search and found a VB Script and a CMD Command:
Option Explicit
Const FILE_SHARE = 0
Const MAXIMUM_CONNECTIONS = 25
Dim objShare
'Connect to WMI
Dim objWMIService: Set objWMIService = _
GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
'Query share names for existing share
Dim colShares: Set colShares = objWMIService.ExecQuery _
("Select * from Win32_Share Where Name = 'MyShare'")
'Delete share if one exists with the same name already
For Each objShare in colShares
objShare.Delete
Next
'Create new share
Dim objNewShare: Set objNewShare = objWMIService.Get("Win32_Share")
Dim strFilePath: strFilePath = Session.Property("CustomActionData")
strFilePath = Left(strFilePath, Len(strFilePath) - 1)
objNewShare.Create strFilePath, "MyShare", _
FILE_SHARE, MAXIMUM_CONNECTIONS, "MyShare"
cmd command:
net share sharename=[INSTALLDIR]
When i run VBScript i don't see any error but I can't share my folder.
When i run cmd command, command can't share because it requires admin privilege; but I am not sure, how i can provide admin privilege to it;
can i share folder? how?
Try. You need to have access rights on location that you are trying to share.
net share Share=E:\Shared /Grant:Everyone,full
Note: You can change the share rights based on your need. This is strictly an example.
i found a very simple solution. i went in file and folders tab and right clicked on installdir an then in sharing tab i checked Share Folder check box and Finish! Very Simple Without code and Command!
I am trying to execute the following script on Win7 (x64) to check if any volumes need to be defragmented.
Set VolumeList = GetObject("winmgmts:").ExecQuery("Select * from Win32_Volume")
For Each objVolume in VolumeList
errResult = objVolume.DefragAnalysis(blnRecommended, objReport)
If errResult = 0 then
Wscript.Echo "Used space: " & objReport.UsedSpace
Wscript.Echo "Volume name: " & objReport.VolumeName
Wscript.Echo "Volume size: " & objReport.VolumeSize
If blnRecommended = True Then
Wscript.Echo "This volume should be defragged."
Else
Wscript.Echo "This volume does not need to be defragged."
End If
Wscript.Echo
Else
MsgBox errResult
End If
Next
I have tried to run this script on two different Win7 systems.
On the first, I get an OUT OF MEMORY error on GetObject("winmgmts:").ExecQuery("Select * from Win32_Volume").
On the second, I get no OUT OF MEMORY error on GetObject, but I get error 11 (Unknown Error) in errResult (output of DefragAnalysis-method).
Both Win7 systems have been installed and configured in the same way.
Perhaps this is not important, but when I check the WMI properties, it says "Connected to <Local Computer>" and not (as in Win XP) "SUCCESSFULLY connected to <Local Computer>".
Code works just fine for me, but perhaps it'll help when you explicitly connect to the right namespace:
Set wmi = GetObject("winmgmts://./root/cimv2")
Set VolumeList = wmi.ExecQuery("SELECT * FROM Win32_Volume")
Also I'd recommend restricting the query to just local disks that have a drive letter assigned to them:
SELECT * FROM Win32_Volume WHERE DriveType = 3 AND DriveLetter IS NOT NULL
Use WBEMTest or WMIDiag to check if your WMI connection is working at all. Check the Application and System eventlogs for errors and warnings, too.
The reason for the error 11 was that the script was not run with elevated privileges. Once it was run as administrator, it worked fine. Thanks
As we know it's easy to get client site name in windows 2003 via WMI_NTdomain.clientsitename, object status by WMI_NTdomain.status , but that class doesn't exist in Windows 2000. So can you show me how to get those value by script or command line?
My old system is still running well on windows 2000, i don't want to change it at now.
Grab HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Netlogon\Parameters\DynamicSiteName with reg.exe, vbscript, or your favorite scripting/programing language.
Edit:
I admit that I haven't seen W2k for some time now. Does this VB Script output usefull information:
option explicit
dim adSys
Set adSys = CreateObject("ADSystemInfo")
WScript.Echo "SiteName=" & adSys.SiteName
'WScript.Echo "Computername DN=" & adSys.ComputerName
'WScript.Echo "Username DN=" & adSys.UserName
'WScript.Echo "DomainDNSName (Comp)=" & adSys.DomainDNSName
'WScript.Echo "DomainShortName (Comp)=" & adSys.DomainShortName
'WScript.Echo "ForestDNSName (Comp)=" & adSys.ForestDNSName
You could also use the WMI ScriptOMatic to search for the relevent class.