clevergod
Junior Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору /Проверка состояния (включения-выключения пк) с отправкой результата на емэйл в виде Computer/LogIN_User. /1TurnOnPC_Send_Email.vbs: _______________________________________________________ Код: Const ForAppending = 8 Dim INPUT_FILE_NAME Dim rs Dim Script_Time INPUT_FILE_NAME = "\\server\TurnOff_Computers\" & CStr(Date) & "_Powered-ON_WS.txt" Set con = CreateObject("ADODB.Connection") Set com = CreateObject("ADODB.Command") Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile(INPUT_FILE_NAME) objFile.Close Set objTextFile = objFSO.OpenTextFile(INPUT_FILE_NAME, ForAppending, True) Script_Time = Now objTextFile.WriteLine("Computer_Name;User_Name") con.Provider = "ADsDSOObject" con.Properties("ADSI Flag") = 1 con.Open "Active Directory Provider" Set Com.ActiveConnection = con com.Properties("Page Size") = 3000 Com.CommandText = "<LDAP://OU=Computers,DC=company,DC=com>;(objectclass=computer);name,location;oneLevel" Set rs = Com.Execute Do While rs.EOF = False Call WSrequest(rs.Fields("name")) rs.MoveNext Loop objTextFile.WriteLine("Script start_time: " & CStr(Script_Time)) objTextFile.WriteLine("Script end_time: " & CStr(Now)) Script_Time = (Now - Script_Time)*24*60 objTextFile.WriteLine("Duration was " & CCur(Script_Time) & " minutes!") objTextFile.Close Call send_Mail ' Wscript.Echo "Script is completed!" Sub WSrequest(nmComp) On Error Resume Next Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") Set objWMIService = objSWbemLocator.ConnectServer(nmComp, "root\CIMV2",,,,,128) Set colItems1 = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48) For Each objItem in colItems1 If IsNull(objItem.Name) then i = 1 Else WSinfoStr = objItem.Name & ";" & objItem.UserName objTextFile.WriteLine(WSinfoStr) End If Next End Sub Sub send_Mail strSmtpServer="mail.company.com" ' имя почтового сервера strSmtpPort=25 strSmtpAuth="no" strSmtpSsl="no" strSenderEmail="administrator@company.com" ' Имя отправителя strRecipientEmail="user@company.com" ' Имя получателя strAttachment="\\server\TurnOff_Computers\" & CStr(Date) & "_Powered-ON_WS.csv" ' Set objMessage = CreateObject("CDO.Message") objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSmtpServer objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = strSmtpPort objMessage.Configuration.Fields.Update ' objMessage.Subject = "Report of Powered-ON workstations. " & CStr(Date) & "." objMessage.From = strSenderEmail objMessage.To = strRecipientEmail objMessage.HTMLBody = "Body of Report:" objMessage.AddAttachment strAttachment objMessage.Send End Sub _______________________________________________________ /Выключение ПК по списку /2TurnOFF_PC.vbs: _______________________________________________________ Код: Dim OpSysSet, Flags Dim IntErrlevel arrTargetComps = Array("Comp1","Comp2","Comp3") For Each strTargetComp In arrTargetComps Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//" & strTargetComp ).ExecQuery("select * from Win32_OperatingSystem where Primary=true") ' 0-Logoff, 1-Shutdown, 2-Reboot, 4-Force ' 8-Power Off, 16-Force If Hung Flags = 1 ' Выключаем IntErrlevel = 0 For Each OpSys In OpSysSet OpSys.Win32Shutdown Flags,IntErrlevel Next Next _______________________________________________________ /Перебор и вывод на экран компьютеров домена /3FindPC_inAD.vbs: _______________________________________________________ Код: On Error Resume Next Set Root = GetObject("LDAP://RootDSE") DomObj = Root.Get("DefaultNamingContext") Set objConn = CreateObject("ADODB.Connection") objConn.Provider = "ADsDSOObject" objConn.Open "Active Directory Provider" Set objCMD = CreateObject("ADODB.Command") objCMD.ActiveConnection = objConn objCMD.CommandText = "<LDAP://" & DomObj & ">;(&(objectClass=computer));name;subtree" objCMD.Properties("Page Size") = 1000 objCMD.Properties("Timeout") = 30 objCMD.Properties("Cache Results") = False Set objRS = objCMD.Execute objRS.MoveFirst While Not objRS.EOF wscript.echo objRS.Fields("name") objRS.MoveNext Wend _______________________________________________________ Уважаемые знатоки, необходимо объеденить эти скрипты (1-2-3) для получения мега нужно вещи - как выключение 2 скрипт каждую ночь включенных пк в виде результатов 1 скрипта, с перебором пк в домене 3 скрипта но с исключением допустим OU=Servers. Пока что реализовано батником: /TurnOff_Computers.cmd: _______________________________________________________ FOR /F "eol=; tokens=1" %%i in (\\server\TurnOff_Computers\ComputerList.txt) do ping -n 1 %%i | find "TTL=" && echo %%i >> \\server\TurnOff_Computers\%date%_Computers_ON.txt FOR /F "eol=; tokens=1" %%i in (\\server\TurnOff_Computers\%date%_Computers_ON.txt) do shutdown -s -f -m \\%%i _______________________________________________________ Затем vbs по отправке результата на Email sendMail.vbs: _______________________________________________________ Set objEmail = CreateObject("CDO.Message") Dim MyTime MyTime = Now Dim Temp Dim MyDate Temp = Day(Now) if (Temp < 10) then MyDate = "0" MyDate = MyDate & Temp & "." Temp = Month(Now) if (Temp < 10) then MyDate = MyDate & "0" MyDate = MyDate & Temp & "." & Year(Now) objEmail.From = "administrator@company.com" objEmail.To = "user@company.com" objEmail.Subject = "Computers turned on " & MyTime objEmail.Textbody = "" objEmail.Addattachment "\\server\TurnOff_Computers\" & MyDate & "_Computers_ON.txt" objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.company.com" objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 objEmail.Configuration.Fields.Update objEmail.Send _______________________________________________________, 10) then MyDate = MyDate |