Showing posts with label VBS. Show all posts
Showing posts with label VBS. Show all posts

Friday, October 2, 2015

Capturing Windows Activation Key by SCCM

Most of the enterprise that are using Windows as the Operating System would most likely be using Microsoft Multiple Activation Keys or MAk for short for licensing.
There maybe a day which come when you may need to figure out how to get the number of clients in your environment that are activated using a particular product key. (This will NOT work for MAK but if needed, you can still use the generated product ID to tied to a valid ID)
This information unfortunately is not readily available in SCCM and would require some work to have it available.

The VBS that has been written below enables you to be able to retrieve the MAK and store it in a registry key (in the script, I have stored it on HKLM\Software\SCCM\ActivationKey).
Once you have this value available, you will be able to by means of SCCM to do a hardware inventory to retrieve the information and format it accordingly for use.
Hopefully this helps anyone who requires this.

Option Explicit 

Const HKEY_LOCAL_MACHINE = &H80000002
Dim objshell,path,DigitalID, Result ,Strcomputer,objregistry,strkeypath,strvaluename,strvalue,strvaluename1
Set objshell = CreateObject("WScript.Shell")
'Set registry key path
Path = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
'Registry key value
DigitalID = objshell.RegRead(Path & "DigitalProductId4")
Dim ProductName,ProductID,ProductKey,ProductData
'Get ProductName, ProductID, ProductKey
ProductName = "Product Name: " & objshell.RegRead(Path & "ProductName")
ProductID = "Product ID: " & objshell.RegRead(Path & "ProductID")
ProductKey = ConvertToKey(DigitalID) 
ProductData = ProductName  & vbNewLine & ProductID  & vbNewLine & ProductKey
'wscript.echo ProductData

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & _ 
    strComputer & "\root\default:StdRegProv")

'Check for the existance of the key and create if it's not found.
strKeyPath = "SOFTWARE\SCCM\"
strValueName = "ActivationKey"
objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

If IsNull(strValue) Then

objregistry.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
StrValueName1 = "ActivationKey"

objRegistry.SetStringValue HKEY_Local_Machine, strKeyPath, strValueName1,ProductKey

End If

'Convert binary to chars
Function ConvertToKey(Key)
    Const KeyOffset = 52
    Dim isWin8, Maps, i, j, Current, KeyOutput, Last, keypart1, insert
    'Check if OS is Windows 8
    isWin8 = (Key(66) \ 6) And 1
    Key(66) = (Key(66) And &HF7) Or ((isWin8 And 2) * 4)
    i = 24
    Maps = "BCDFGHJKMPQRTVWXY2346789"
        Current= 0
        j = 14
           Current = Current* 256
           Current = Key(j + KeyOffset) + Current
           Key(j + KeyOffset) = (Current \ 24)
           Current=Current Mod 24
            j = j -1
        Loop While j >= 0
        i = i -1
        KeyOutput = Mid(Maps,Current+ 1, 1) & KeyOutput
        Last = Current
    Loop While i >= 0 
    keypart1 = Mid(KeyOutput, 2, Last)
    insert = "N"
    KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0)
    If Last = 0 Then KeyOutput = insert & KeyOutput
    ConvertToKey = Mid(KeyOutput, 1, 5) & "-" & Mid(KeyOutput, 6, 5) & "-" & Mid(KeyOutput, 11, 5) & "-" & Mid(KeyOutput, 16, 5) & "-" & Mid(KeyOutput, 21, 5)
End Function

Wednesday, April 22, 2015

SCCM MIF File Generator

This VBS will provide you with a mean to generate the NOIDMIF.mif files for SCCM clients based on a pre-populated Excel sheet. It has been shared in the Microsoft Community Gallery and it can be downloaded here.

Output MIF File will be similar to the screenshot below
1) The excelsheet should be populated base don the format below with the Values in Column A to C based on your own information of Ship Date (Aka Purchase Date) and Known Hardware Warranty Date.
The Excel should be named as ServersInventory.xlsx for servers and computersinventory.xlsx for clients.
2) The file should be executed on a machine with Excel installed
3) Modify the struser and strpwd in the vbs to the necessary credentials to enable the MIF files generated to be copied to the target mahcine
How to use
1) Save the excel files and the downloaded vbs file in the same folder
2) Execute the vbs
3) Enter 1 if you populating for servers and 2 if its for workstations/notebooks
4) Enter the OU that you wish to target against
1) A scan will be done on the the specified OU against the information available in the excel sheet.
2) MIF files will be generated and copied for matches found and the machine name will be populated in the excel. (Screenshot as below)

Friday, November 1, 2013

Windows Update via VBS

Put together something that will enable you to perform Windows Update by means of the vbs.
In this vbs, I have decided that I do not want to include any updates for Dot Net Framework and Windows Search.

'ServerSelection values
ssDefault = 0
ssManagedServer   = 1
ssWindowsUpdate   = 2
ssOthers          = 3

'InStr values
intSearchStartChar = 1

dim strTitle

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

updateSearcher.ServerSelection = ssWindowsUpdate
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")

WScript.Echo "List of applicable items on the machine:"

For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    WScript.Echo I + 1 & "> " & update.Title

If searchResult.Updates.Count = 0 Then
    WScript.Echo "There are no applicable updates."
End If

WScript.Echo vbCRLF & "Creating collection of updates to download:"

Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")

For I = 0 to searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    addThisUpdate = false
if instr(lcase(update.title),".net framework") = 0 then  
if instr(lcase(update.title),"search") = 0 then  
If update.InstallationBehavior.CanRequestUserInput = true Then
        WScript.Echo I + 1 & "> skipping: " & update.Title & _
        " because it requires user input"
        If update.EulaAccepted = false Then
            WScript.Echo I + 1 & "> note: " & update.Title & _
            " has a license agreement that must be accepted:"
            WScript.Echo update.EulaText
            WScript.Echo "Do you accept this license agreement? (Y/N)"
            ''strInput = WScript.StdIn.ReadLine
            strInput = "Y"
            If (strInput = "Y" or strInput = "y") Then
                addThisUpdate = true
                WScript.Echo I + 1 & "> skipping: " & update.Title & _
                " because the license agreement was declined"
            End If
            addThisUpdate = true
        End If
    End If
End if
End if
    If addThisUpdate = true Then
        WScript.Echo I + 1 & "> adding: " & update.Title
    End If

If updatesToDownload.Count = 0 Then
    WScript.Echo "All applicable updates were skipped."
End If
WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader()

downloader.Updates = updatesToDownload
 Set downloadResult = downloader.Download()
   WScript.Echo "Download Result: " & downloadResult.ResultCode

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

rebootMayBeRequired = false

WScript.Echo vbCRLF & "Successfully downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
    set update = searchResult.Updates.Item(I)
    If update.IsDownloaded = true Then
        WScript.Echo I + 1 & "> " & update.Title
        If update.InstallationBehavior.RebootBehavior > 0 Then
            rebootMayBeRequired = true
        End If
    End If

If updatesToInstall.Count = 0 Then
    WScript.Echo "No updates were successfully downloaded."
End If

If rebootMayBeRequired = true Then
    WScript.Echo vbCRLF & "These updates may require a reboot."
End If

WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
''strInput = WScript.StdIn.ReadLine
strInput = "Y"

If (strInput = "Y" or strInput = "y") Then
    WScript.Echo "Installing updates..."
    Set installer = updateSession.CreateUpdateInstaller()
    installer.Updates = updatesToInstall
    Set installationResult = installer.Install()
    'Output results of install
    WScript.Echo "Installation Result: " & _
    WScript.Echo "Reboot Required: " & _
    installationResult.RebootRequired & vbCRLF
    WScript.Echo "Listing of updates installed " & _
    "and individual installation results:"
    For I = 0 to updatesToInstall.Count - 1
        WScript.Echo I + 1 & "> " & _
        updatesToInstall.Item(i).Title & _
        ": " & installationResult.GetUpdateResult(i).ResultCode        
I = I + 1
End If

Thursday, February 9, 2012

Locking a Machine in a OCS Desktop Sharing/SCCM Remote Tools Session

With the many tools available now, there are many means for the support staff to access into a user's machine to render assistance. OCS desktop sharing as well as SCCM Tools is one of that is being used my my current organization.

These tools however come with a challenge. How will the support staff be able to maintain the security of the machine when the support case ends as they are not to logoff from the machine.

For this I have come up with a simple vbs that will enable the support staff to lock the screen remotely from their machine (Provided RPC connectivity is operational)

strcomputer = InputBox("Please enter computer name:", "Machine Name to Lock")

strCommand = "cmd.exe /k %windir%\System32\rundll32.exe user32.dll, LockWorkStation"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")

errReturn = objProcess.Create(strCommand, null, null, intProcessID)

If errReturn = 0 Then
Wscript.Echo "**" & ucase(strcomputer) & " has been locked successfully**!"
End If

Just copy the code in grey as above and save it as a vbs file.

Alternatively if it is preferred to lock the machine locally, the below  can be run either at command prompt or run window

%windir%\System32\rundll32.exe user32.dll, LockWorkStation