Showing posts with label vbs. Show all posts
Showing posts with label vbs. Show all posts

Thursday, 21 August 2014

VBS - create local administrator "useradmin" with SN serialnumber as password

' created by Martin Junge 14.5.2012
' the script creates a local Administrator "UserAdmin"
' Password is the individual "Dell Service Tag" + "?"

Dim objWMI : Set objWMI = GetObject("winmgmts:")
Dim colSettingsComp : Set colSettings = objWMI.ExecQuery("Select * from Win32_ComputerSystem")
Dim colSettingsBios : Set colSettingsBios = objWMI.ExecQuery("Select * from Win32_BIOS")
Dim objComputer, strModel, strSerial
For Each objComputer in colSettings
  strModel = objComputer.Model
Next
For Each objComputer in colSettingsBios
  strSerial = objComputer.SerialNumber
Next

'wscript.echo strSerial


accUserName = "Useradmin"
accUserPass = strSerial & "?"

            QueryForUser(accUserName)
                'this section creates the new username if it doesn't exist
                Set objNetwork = CreateObject("Wscript.Network")
                strComputer = objNetwork.ComputerName
                Set objComputer = GetObject("WinNT://" &strComputer)

                Set colAccounts = GetObject("WinNT://" & strComputer & "")
                Set objUser = colAccounts.Create("user", accUserName)
                objUser.SetPassword accUserPass
                objUser.Put "UserFlags", 65600 '
                objUser.SetInfo

                'add user to administrators group

                'Set objGroup = GetObject("WinNT://" & strComputer & "/Administratoren,group")
                'Set objUser = GetObject("WinNT://" & strComputer & "/" & accUserName & ",user")
                'objGroup.Add(objUser.ADsPath)

           'msgbox "username was created"

                'this section just changes the password if the user exists
                Sub QueryForUser(strUserName)
                    Set objlocal = GetObject("WinNT://.")
                    objlocal.Filter = Array("user")
                    For Each User In objlocal
                    If lcase(User.Name) = lcase(strUserName) Then
                        strComputer = "."
                        Set objUser = GetObject("WinNT://" & strComputer & "/" & accUserName & ", user")
                        objUser.SetPassword accUserPass
                        objUser.SetInfo
                        'msgbox User.Name & " already exists." & vbCrLf & "The password was re-set."
                        WScript.Quit
                    End If   
            Next
                    End Sub

VBS - write computer details to AD computer account description

original post, how to delegate permissions in AD
http://www.compit.se/?p=282


output:
martin.junge / SN: F4***R1 / Dell Latitude E6420 / Windows 8.1 Pro  x64 / Last Boot Time: 08.08.2014 / Last Update Time: 21.7.2014 / Bios vers.: A21 / bitlocker disabled


---my modified vbs file---

On Error Resume Next

strComputer = "."
Set objSysInfo = CreateObject( "WinNTSystemInfo" )
strUserName = objSysInfo.UserName


Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colcomputersystem = objWMIService.ExecQuery _
    ("Select * from Win32_computersystem")
Set colBIOS = objWMIService.ExecQuery _
    ("Select * from Win32_BIOS")

For each objcomputersystem in colcomputersystem
    Getcomputersystem = objcomputersystem.Model
    if Getcomputersystem = "VMware Virtual Platform" then Getcomputersystem = Right(Getcomputersystem, 17) end if

    if Getcomputersystem = "Virtual Machine" then
        Dim SysVarReg, Value
        Set SysVarReg = WScript.CreateObject("WScript.Shell")
        value = SysVarReg.RegRead ("HKLM\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters\PhysicalHostName")
        Getcomputersystem = Getcomputersystem & " on " & value
    end if

    GetComputerManufacturer = objComputerSystem.Manufacturer
Next

'---get Manufacturer--------------------
   'String cleaning - Manufacturer includes only first word, and the serial removes any spaces.
txtCount = InStr(GetComputerManufacturer," ") - 1
GetComputerManufacturer = Left(GetComputerManufacturer,txtCount)
if GetComputerManufacturer = "Hewlett-Packard" then GetComputerManufacturer = "" end if
if GetComputerManufacturer = "TOSHIBA" then GetComputerManufacturer = "Toshiba" end if
if GetComputerManufacturer = "VMware," then GetComputerManufacturer = Left(GetComputerManufacturer, 6) end if

if GetComputerManufacturer = "LENOVO" then
    Dim objWMIService, colItems
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("SELECT Version FROM Win32_ComputerSystemProduct")
    For Each objItem in colItems
        Getcomputersystem = objItem.Version & " " & objItem.Name
    Next
    GetComputerManufacturer = "Lenovo"
end if

if GetComputerManufacturer = "Microsoft Virtual Machine" then
    VMHost = WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters\PhysicalHostName")
    GetComputerManufacturer = GetComputerManufacturer & "on " & VMHost
end if
'---end Manufacturer-------

'---get serialnumber and BIOS version----------
GetSerialNumber = Replace(GetSerialNumber, " ", "")
For each objBIOS in colBIOS
    GetSerialNumber = objBIOS.SerialNumber
    GetBiosVers = objBIOS.SMBIOSBIOSVersion
Next
if GetComputerManufacturer = "VMware" then GetSerialNumber = Left(GetSerialNumber, 6) end if

'---end serialnumber-------

'---get processor architecture type---
Dim WshShell
Dim OsType
Set WshShell = CreateObject("WScript.Shell")
OsType = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
if OsType = "AMD64" then
OsType = "x64"
end if
'---end processor architecture type---

'---get Operating system---
Dim OSVersion
OSVersion = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName")
if OSVersion = "Microsoft Windows XP" then OSVersion = "Windows XP" end if
if OSVersion = "Microsoft Windows 2000" then OSVersion = "Windows 2000" end if
if OSVersion = "Microsoft Windows Server 2003" then OSVersion = "Windows Server 2003" end if
if OSVersion = "Microsoft Windows Server 2003 R2" then OSVersion = "Windows Server 2003 R2" end if
'---end Operating system---

'---get service pack version---
Const Impersonate = "winmgmts:{impersonationLevel=impersonate}!\\"
Set oWMI = GetObject(Impersonate & strComputer & "\root\cimv2")
Set QueryWMI = oWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each oItem In QueryWMI
spVer = oItem.ServicePackMajorVersion
if spVer = 0 then spVer ="" else spVer = "SP" & spVer end if
Next
'---end service pack version---

'---get last boot time start----
Dim strBoot, strBootDate, strBootDay, strBootHour, strBootMins
Dim strBootMonth, strBootTime, strBootYear, strMsg, strQuery
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
For Each objItem in colItems
strBootYear = Left( objItem.LastBootUpTime, 4 )
strBootMonth = Mid( objItem.LastBootUpTime, 5, 2 )
strBootDay = Mid( objItem.LastBootUpTime, 7, 2 )
strBootHour = Mid( objItem.LastBootUpTime, 9, 2 )
strBootMins = Mid( objItem.LastBootUpTime, 11, 2 )
strBootTime = strBootHour & ":" & strBootMins
Next
'---end last boot time----

'---get last windows update time------
Set objSession = CreateObject("Microsoft.Update.Session")
Set objSearcher = objSession.CreateUpdateSearcher
Set colHistory = objSearcher.QueryHistory(1, 1)
For Each objEntry in colHistory
 strHotfix = objEntry.Date
Next

'---end last windows update time------

'---get bitlocker status---

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\Security\MicrosoftVolumeEncryption")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_EncryptableVolume",,48)
For Each objItem in colItems
    bitlockerdiveletter = objItem.DriveLetter
    bitlockerstatus = objItem.ProtectionStatus
    if bitlockerdiveletter = "C:" then
        if bitlockerstatus=1 then bitlockerstatusnote = "bitlocker enabled" else bitlockerstatusnote = "bitlocker disabled" end if
    end if
next
'---end bitlocker status---

'---define AD description text for clients and servers
If InStr(OSVersion, "Server") Then
    strMessage = GetComputerManufacturer & " " & trim(Getcomputersystem) & " / " & "SN: " & trim(GetSerialNumber) & " / " & OSVersion & " " & spVer  & " " & OsType & " / " & "Last Boot Time: " & strBootDay & "." & strBootMonth & "." & strBootYear & " / Last Update Time: " &  day(strHotfix) & "." & month(strHotfix) & "." & year(strHotfix) & " / Bios vers.: " & GetBiosVers
else
    strMessage = strUserName & " / " & "SN: " & trim(GetSerialNumber) & " / " & GetComputerManufacturer & " " & trim(Getcomputersystem) & " / " & OSVersion & " " & spVer  & " " & OsType & " / " & "Last Boot Time: " & strBootDay & "." & strBootMonth & "." & strBootYear & " / Last Update Time: " &  day(strHotfix) & "." & month(strHotfix) & "." & year(strHotfix) & " / Bios vers.: " & GetBiosVers & " / " & bitlockerstatusnote
end if
'---end define---

'WScript.Echo strMessage   'for testing

objComputer.Description = strMessage
objComputer.SetInfo

'---eof---

CMD - force windows update for all servers in txt file


4 files needed!

start-update.bat
servers.txt
updatehf.bat
updatehf.vbs
psexec.exe

---start-update.bat-----
@echo off

rem set LISTFILE=%1
set LISTFILE=servers.txt

for /f %%a in (%LISTFILE%) do (
    echo.
    echo ---------------------------------------
    echo.
    echo %%a
    ping -n 1 %%a | find "TTL="
        if not errorlevel 1 (
            echo copy updatehf.vbs to %%a
                xcopy c:\temp\updatehf.vbs "\\%%a\c$\temp\" /Y /Q
            echo copy updatehf.bat to %%a
                xcopy c:\temp\updatehf.bat "\\%%a\c$\temp\" /Y | find "0 File"
                if errorlevel 1 (
                    echo open a new cmd for %%a to run updatehf.bat
                    start /min cmd /k ""C:\Windows\psexec.exe" -accepteula -s -w "c:\temp" \\%%a "c:\temp\updatehf.bat" & exit"
                    )
            )
        if errorlevel 1 (
            echo %%a unavailable
            )
    )

----eof---


---updatehf.bat---
cscript c:\temp\updatehf.vbs action:install mode:silent email:youremail@yourdomain.com restart:0
exit

---eof---



---servers.txt---
server1
server2
server3
...
---eof---

Wednesday, 20 August 2014

VBS - last reboot time

strComputer = "." ' Local computer

set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
for each objOS in colOS
    objWMIDateTime.Value = objOS.LastBootUpTime
    Wscript.Echo "Last Boot Up Time: " & objWMIDateTime.GetVarDate & vbcrlf & _
        "System Up Time: " &  TimeSpan(objWMIDateTime.GetVarDate,Now) & _
        " (hh:mm:ss)"
next

Function TimeSpan(dt1, dt2)
    ' Function to display the difference between
    ' 2 dates in hh:mm:ss format
    If (isDate(dt1) And IsDate(dt2)) = false Then
        TimeSpan = "00:00:00"
        Exit Function
        End If

        seconds = Abs(DateDiff("S", dt1, dt2))
        minutes = seconds \ 60
        hours = minutes \ 60
        minutes = minutes mod 60
        seconds = seconds mod 60

        if len(hours) = 1 then hours = "0" & hours

        TimeSpan = hours & ":" & _
            RIGHT("00" & minutes, 2) & ":" & _
            RIGHT("00" & seconds, 2)
End Function

VBS - check if service status is "running", if not kill it and start again


strComputer = "."

Set ServiceSet = GetObject("winmgmts:").ExecQuery("select * from Win32_Service where Name='Spooler'")

for each Service in ServiceSet
RetVal = Service.StartService()
'wscript.echo RetVal
if not RetVal = 10 then    'control if service has state running
'WScript.Echo "Name: " & Service.Name & ", " & "Status: " & Service.Status & ", " & "ProcessId: " & Service.ProcessId & ", " & "ServiceState: " & Service.State
strProcessId = Service.ProcessId
Dim oShell, oExec, sLine
Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("taskkill /f /pid " & strProcessId)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:win32_service.name='Spooler'")
objWMIService.StartService
end if
next

VBS - disable all HP printers notifications / popups


' Author martin junge
' Version 0.1 - 16-7-2014
' -----------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems, strComputer, intPrinters

strComputer = "."
intPrinters = 1

dim strtemp, strtemp1, strtemp2, strDrucker, strRegkey, WshShell

Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer")

On Error Resume Next
For Each objItem In colItems
if (instr(objItem.DriverName, "HP")) then
'WScript.Echo "Printer: " & objItem.DeviceID & ", " & objItem.DriverName

Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
strDrucker = objItem.DeviceID
strtemp = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\"
'----
strtemp1 = "\PrinterDriverData\SSNPNotifyEventSetting"
strRegkey = strtemp & strDrucker & strtemp1
'wscript.echo strRegkey
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite strRegkey, 0, "REG_DWORD"
'----
strtemp2 = "\PrinterDriverData\SSNPDisableAll"
strRegkey = strtemp & strDrucker & strtemp2
'wscript.echo strRegkey
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite strRegkey, 1, "REG_DWORD"
'----
end if
intPrinters = intPrinters + 1
Next


WScript.Quit

' End of Sample Printer VBScript