~siggi-bjarnason/siggivbscript/vbscript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Option Explicit
Dim Locator, conn, strNameSpace, strServer, SQLServerObj, strDomain, uid, pwd

Const wbemImpersonationLevelImpersonate = 3
'On Error Resume Next 
If wscript.arguments.count > 0 Then
	strServer = wscript.arguments(0)
	wscript.echo Now() & " Connecting to " & strServer & "..."
Else
	strServer = ""
	wscript.echo Now() & " Connecting to local machine..."
End If

If wscript.arguments.count > 2 Then
	uid = wscript.arguments(1)
	pwd = wscript.arguments(2)
	wscript.echo Now() & " Connecting to " & strServer & " using " & uid
Else
	uid = ""
	pwd = ""
	wscript.echo Now() & " Connecting using current logins"
End If

strNameSpace = "root\CIMV2"

Set locator=CreateObject("WbemScripting.SwbemLocator")
If strServer = "" Then
	Set conn=locator.connectserver (, strNameSpace)
	If err.number <> 0 Then 
		wscript.echo "unable to connect"
		wscript.quit
	End If
	conn.security_.impersonationlevel = wbemImpersonationLevelImpersonate 
Else
	Set conn=locator.connectserver (strServer, strNameSpace,uid,pwd)
	If err.number <> 0 Then 
		wscript.echo "unable to connect"
		wscript.quit
	End If
	conn.security_.impersonationlevel = wbemImpersonationLevelImpersonate 
End If
Set SQLServerObj=conn.get("Win32_Service.name=""MSSQLServer""")
wscript.echo "Stopping..."
wscript.echo sqlserverobj.stopservice
'wscript.echo "SQL Server service is " & sqlserverobj.state
'wscript.echo "SQL Server Service Autostart set to " & sqlserverobj.startmode
wscript.echo ""
wscript.echo Now() & " Script Complete."