Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.41 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Dim strExcelPath, objExcel, objSheet, intRow, strUserDN, strPassword, comp
  4. Dim objUser
  5.  
  6. ' Spreadsheet file.
  7. strExcelPath = "c:List.xls"
  8.  
  9. ' Bind to Excel object.
  10. On Error Resume Next
  11. Set objExcel = CreateObject("Excel.Application")
  12. If (Err.Number <> 0) Then
  13. On Error GoTo 0
  14. Wscript.Echo "Excel application not found."
  15. Wscript.Quit
  16. End If
  17. On Error GoTo 0
  18.  
  19. ' Open spreadsheet.
  20. On Error Resume Next
  21. objExcel.Workbooks.Open strExcelPath
  22. If (Err.Number <> 0) Then
  23. On Error GoTo 0
  24. Wscript.Echo "Spreadsheet cannot be opened: " & strExcelPath
  25. Wscript.Quit
  26. End If
  27. On Error GoTo 0
  28.  
  29. ' Bind to worksheet.
  30. Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
  31.  
  32.  
  33. intRow = 2
  34. Do While objSheet.Cells(intRow, 1).Value <> ""
  35. comp = objSheet.Cells(intRow, 1).Value
  36. strUserDN = objSheet.Cells(intRow, 2).Value
  37. strPassword = objSheet.Cells(intRow, 3).Value
  38. On Error Resume Next
  39. Set objuser = GetObject ("WinNT://" & comp & "/" & strUserDN & ",user")
  40. If (Err.Number <> 0) Then
  41. On Error GoTo 0
  42. Wscript.Echo "Data NOT found: "
  43. Else
  44. objUser.SetPassword strPassword
  45. If (Err.Number <> 0) Then
  46. On Error GoTo 0
  47. Wscript.Echo "Password NOT set for: " & strUserDN
  48. Else
  49.  
  50.  
  51. End If
  52. End If
  53. intRow = intRow + 1
  54. Loop
  55.  
  56. ' Close the workbook.
  57. objExcel.ActiveWorkbook.Close
  58.  
  59. ' Quit Excel.
  60. objExcel.Application.Quit
  61.  
  62. Wscript.Echo "Done"
  63.  
  64. server = "YourServerName"
  65. Set oComputer = GetObject("WinNT://" & server & "")
  66. oComputer.Filter = Array("User")
  67. For Each oUser in oComputer
  68. WScript.Echo oUser.Name
  69. Next
  70.  
  71. 'Objective: check multiple servers for admin accounts status and report to html file
  72.  
  73. Set iFSO = CreateObject("Scripting.FilesyStemObject")
  74. Set oFSO = CreateObject("Scripting.FilesyStemObject")
  75.  
  76. InputFile = WScript.Arguments.Named("servers")
  77. if len(InputFile) < 1 then
  78. wscript.echo "Error: Servers Parameter not found" & vbCrLf
  79. show_usage
  80. wscript.quit
  81. end if
  82.  
  83. Outputfile= InputFile & "_guest_admins_" + cstr(Month(now()))+"_"+cstr(day(now()))+".htm"
  84.  
  85. if not ofso.FileExists(inputfile) then
  86. wscript.echo "Error: Server list file not Found."
  87. wscript.quit
  88. end if
  89.  
  90. Set ofile = ofso.createTextFile(OutputFile, True)
  91. Set ifile = iFSO.OpenTextFile(inputfile)
  92.  
  93. ofile.writeline "<html>" & html_head & "<body>"
  94.  
  95. ofile.writeline "<table border=1 cellpadding=1 cellspacing=0>"
  96. ofile.writeline o
  97. ofile.writeline "<tr><td>Hostname</td><td>User</td><td>Disabled</td><td>Locked</td><td>Expiration Date</td><td>Flags</td><td>BuiltIn</td></tr>"
  98.  
  99. Do until ifile.AtEndOfLine
  100. Computer = ifile.ReadLine
  101. if ping(Computer) then
  102. Builtin = ""
  103. if Check_WMI(Computer) then
  104. Builtin = GetBuiltInAccount(Computer)
  105. else
  106. Builtin = "WMI Fail"
  107. end if
  108.  
  109. strt = now
  110. wscript.echo "Checking Users for server: " & Computer
  111.  
  112. on error resume next
  113.  
  114. Set objGroup = GetObject("WinNT://" & Computer & "/Administrators,group")
  115.  
  116. if err.number <> 0 then
  117. wscript.echo "GetObject WinNT Failed"
  118. ofile.writeline "<tr><td>" & computer & "</td><td colspan=6 align=center>GetObject WinNT Fail: "& err.number &"</td></tr>"
  119. else
  120. on error goto 0
  121.  
  122. For Each objUser in objGroup.Members
  123. ofile.writeline GetUserNT(computer, objUser.Name, Builtin)
  124. Next
  125.  
  126. wscript.echo "Elapsed Time: " & datediff("s", strt, now) & " seconds"
  127. end if
  128. else
  129. wscript.echo computer & " does not reply ping"
  130. ofile.writeline "<tr><td>" & computer & "</td><td colspan=6 align=center>No Ping Reply</td></tr>"
  131. end if
  132. Loop
  133. ofile.writeline "</table>"
  134.  
  135. ofile.writeline "</body></html>"
  136.  
  137. function ping(target)
  138. strComputer = "."
  139. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
  140. Set colPingedComputers = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address = '"& target & "'")
  141. For each objComputer in colPingedComputers
  142. ' If the status code is Null or Not 0 then the ping failed
  143. If IsNull( objComputer.StatusCode ) Or objComputer.StatusCode <> 0 Then
  144. ' Set the function to return Boolean FALSE
  145. Ping = False
  146. Else
  147. ' Set the function to return Boolean TRUE
  148. Ping = True
  149. End If
  150. Next
  151. end function
  152.  
  153. sub show_usage
  154. wscript.echo "Usage: cscript chkusers /servers:list.txt" & vbcrlf
  155.  
  156. wscript.echo vbtab & "/servers Parameter is a Text File one Servername per Line" & vbcrlf
  157.  
  158. wscript.echo "Notes: This script generates an html report of server admin accounts."
  159. wscript.echo " Results are saved in a file named + date + htm extension."
  160. wscript.echo " Output example filename: list_guest_admins_" + cstr(Month(now()))+"_"+cstr(day(now()))+".htm"
  161.  
  162. end sub
  163.  
  164. Function Check_WMI(strServer)
  165. On Error Resume Next ' error handling off
  166.  
  167. ' create object reference, connect to namespace rootdefault
  168. Set oCimOmId = GetObject("winmgmts:"& strServer & "rootdefault:__cimomidentification=@")
  169.  
  170. ' Test whether WMI is present or not.
  171. If Err <> 0 then
  172. Check_WMI= true
  173. else
  174. Check_WMI= false
  175. end if
  176. on error goto 0
  177. end function
  178.  
  179. Sub EnumNameSpaces(strNameSpace)
  180. 'call enumnamespaces("root")
  181. Set objWMIService = GetObject("winmgmts:\" & strComputer & "" & strNameSpace)
  182. Set colNameSpaces = objWMIService.InstancesOf("__NAMESPACE")
  183. For Each objNameSpace In colNameSpaces
  184. Call EnumNameSpaces(strNameSpace & "" & objNameSpace.Name)
  185. Next
  186. End Sub
  187.  
  188. function CSS
  189. tt = "<style type=""text/css"">" & vbcrlf
  190. tt=tt & " body {font-family:Verdana;font-size: 10px;color: #49403B;background: #EFEFEF;}" & vbcrlf
  191. tt=tt & "table {font-family:Verdana;font-size: 12px; empty-cells:show; }" & vbcrlf
  192. tt=tt & "</style>" & vbcrlf
  193. CSS = tt
  194. end function
  195.  
  196. function html_head
  197. tt="<head>" & vbcrlf
  198. tt=tt & CSS
  199. html_head = tt & "</head>" & vbcrlf
  200. end function
  201.  
  202.  
  203. function GetUserNT(strComputer, usr, bltin)
  204. Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
  205.  
  206. o=""
  207. On Error Resume Next
  208.  
  209. Set objUser = GetObject("WinNT:// " & strComputer & "/" & usr & " ")
  210.  
  211. o=o& "<tr><td>"& strcomputer &"</td><td>"& usr &"</td>"
  212.  
  213. if len(objUser.AccountDisabled) = 0 then exit function
  214.  
  215. o=o& "<td>&nbsp;"& StrDisabled(objUser.AccountDisabled) &"</td>"
  216. o=o& "<td>&nbsp;"& StrLocked(objUser.IsAccountLocked) &"</td>"
  217.  
  218.  
  219. o=o& "<td>&nbsp;"
  220. o=o& objUser.Get("UserFlags") AND ADS_UF_DONT_EXPIRE_PASSWD
  221. o=o& "</td>"
  222.  
  223. o=o& "<td>&nbsp;"
  224. o=o& objUser.AccountExpirationDate
  225. o=o& "</td>"
  226.  
  227. if lcase(bltin) = lcase(usr) then
  228. o=o & "<td>&nbsp;Built-In</td>"
  229. elseif instr(bltin, "[[Fail]]") > 0 then
  230. o=o & "<td>&nbsp;"& bltin &"</td>"
  231. else
  232. o=o & "<td>&nbsp;</td>"
  233. end if
  234.  
  235. o = o & "</tr>"
  236.  
  237. GetUserNT = o
  238. end function
  239.  
  240. function StrLocked(str)
  241. if str = "True" then
  242. StrLocked = "Locked"
  243. else
  244. StrLocked = "Unlocked"
  245. end if
  246. end function
  247.  
  248. function StrDisabled(str)
  249. if str = "True" then
  250. StrDisabled = "Disabled"
  251. else
  252. StrDisabled = "Enabled"
  253. end if
  254. end function
  255.  
  256. function GetUsers(strComputer,grp,usr)
  257. On Error Resume Next
  258. Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
  259. Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount Where name = '"& usr &"'")
  260. o=""
  261. For Each objItem in colItems
  262. o=o& "<tr>"
  263. o=o& "<td>" & strComputer & "</td>"
  264. o=o& "<td>" & grp & "</td>"
  265. o=o& "<td>" & objItem.AccountType & "</td>"
  266. o=o& "<td>" & objItem.Caption & "</td>"
  267. o=o& "<td>" & objItem.Description & "</td>"
  268. o=o& "<td>" & objItem.Disabled & "</td>"
  269. o=o& "<td>" & objItem.Domain & "</td>"
  270. o=o& "<td>" & objItem.FullName & "</td>"
  271. o=o& "<td>" & objItem.LocalAccount & "</td>"
  272. o=o& "<td>" & objItem.Lockout & "</td>"
  273. o=o& "<td>" & objItem.Name & "</td>"
  274. o=o& "<td>" & objItem.PasswordChangeable & "</td>"
  275. o=o& "<td>" & objItem.PasswordExpires & "</td>"
  276. o=o& "<td>" & objItem.PasswordRequired & "</td>"
  277. o=o& "<td>" & objItem.SID & "</td>"
  278. o=o& "<td>" & objItem.SIDType & "</td>"
  279. o=o& "<td>" & objItem.Status & "</td>"
  280. o=o& "</tr>"
  281. Next
  282. on error goto 0
  283. GetUsers = o
  284. end function
  285.  
  286.  
  287.  
  288.  
  289.  
  290. Function getlcl(srvname)
  291. Set objComputer = GetObject("WinNT://" & srvname & "/Administrators,group")
  292.  
  293. wscript.echo "Local Accounts on " & srvname
  294. wscript.echo "-------------------------------------------------"
  295.  
  296. For Each objUser in objComputer.Members
  297. Wscript.Echo vbTab & objUser.Name
  298. Next
  299.  
  300. wscript.echo "-------------------------------------------------"
  301. end Function
  302.  
  303. function GetBuiltInAccount(strComputer)
  304.  
  305. on error resume next
  306. Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
  307. if err.number <> 0 then
  308. GetBuiltInAccout = "Get Object WMI [[Fail]]: " & err.number & ":: " & err.description
  309. err.clear
  310. exit function
  311. end if
  312. on error goto 0
  313.  
  314. on error resume next
  315. Set colAccounts = objWMIService.ExecQuery("Select * From Win32_UserAccount Where Domain = '" & strComputer & "'")
  316.  
  317. if err.number <> 0 then
  318. GetBuiltInAccout = "WMI_ExecQuery [[Fail]]: " & err.number & ":: " & err.description
  319. err.clear
  320. exit function
  321. end if
  322. on error goto 0
  323.  
  324. on error resume next
  325. For Each objAccount in colAccounts
  326. if err.number <> 0 then
  327. GetBuiltInAccout = "WMI_ExecQuery_ForEachAccount [[Fail]]: " & err.number & ":: " & err.description
  328. wscript.echo "WMI_ExecQuery_ForEachAccount [[Fail]]: " & err.number & ":: " & err.description
  329. err.clear
  330. exit function
  331. end if
  332. on error goto 0
  333.  
  334. If Left (objAccount.SID, 6) = "S-1-5-" and Right(objAccount.SID, 4) = "-500" Then
  335. GetBuiltInAccount = objAccount.Name
  336. exit function
  337.  
  338. End If
  339. Next
  340. end function
  341.  
  342. Option Explicit
  343.  
  344. Dim strExcelPath, objExcel, objSheet, intRow, strUserDN, strPassword, comp
  345. Dim objUser
  346.  
  347. ' Spreadsheet file.
  348. strExcelPath = "c:List.xls"
  349.  
  350. ' Bind to Excel object.
  351. On Error Resume Next
  352. Set objExcel = CreateObject("Excel.Application")
  353. If (Err.Number <> 0) Then
  354. On Error GoTo 0
  355. Wscript.Echo "Excel application not found."
  356. Wscript.Quit
  357. End If
  358. On Error GoTo 0
  359.  
  360. ' Open spreadsheet.
  361. On Error Resume Next
  362. objExcel.Workbooks.Open strExcelPath
  363. If (Err.Number <> 0) Then
  364. On Error GoTo 0
  365. Wscript.Echo "Spreadsheet cannot be opened: " & strExcelPath
  366. Wscript.Quit
  367. End If
  368. On Error GoTo 0
  369.  
  370. ' Bind to worksheet.
  371. Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
  372.  
  373.  
  374. intRow = 2
  375. Do While objSheet.Cells(intRow, 1).Value <> ""
  376. comp = objSheet.Cells(intRow, 1).Value
  377. strUserDN = objSheet.Cells(intRow, 2).Value
  378. strPassword = objSheet.Cells(intRow, 3).Value
  379. On Error Resume Next
  380. Set objuser = GetObject ("WinNT://" & comp & "/" & strUserDN & ",user")
  381. If (Err.Number <> 0) Then
  382. On Error GoTo 0
  383. Wscript.Echo "Data NOT found: "
  384. Else
  385. objUser.SetPassword strPassword
  386. If (Err.Number <> 0) Then
  387. On Error GoTo 0
  388. Wscript.Echo "Password NOT set for: " & strUserDN
  389. Else
  390.  
  391.  
  392. End If
  393. End If
  394. intRow = intRow + 1
  395. Loop
  396.  
  397. ' Close the workbook.
  398. objExcel.ActiveWorkbook.Close
  399.  
  400. ' Quit Excel.
  401. objExcel.Application.Quit
  402.  
  403. Wscript.Echo "Done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement