Scripts

Run Hardware or Software Inventory

This function can be used to trigger a Hardware or Software inventory.

To trigger Hardware inventory call the function using:

RunInventory(“Hardware”)

To trigger Software inventory call the function using:

RunInventory(“Software”)

Sub RunInventory(StrType)

	If StrType = "Hardware" Then
		StrType = "Hardware Inventory Collection Cycle"
	ElseIf StrType = "Software" Then
		StrType = "Software Inventory Collection Cycle"
	Else
		Exit Sub
	End If

    ' Create the CPAppletMgr instance.
    Dim controlPanelAppletManager
    Set controlPanelAppletManager = CreateObject("CPApplet.CPAppletMgr")

    ' Get the available ClientActions object.
    Dim clientActions
    Set clientActions = controlPanelAppletManager.GetClientActions()

    ' Loop through the available client actions. Run the matching client action when it is found.
    Dim clientAction
    For Each clientAction In clientActions
        If clientAction.Name =  StrType  Then
            clientAction.PerformAction  
        End If
    Next
    

End Sub

Uninstall MSI if Installed for the Current User

This function takes an MSI product code as input and uninstalls the software if its installed for the current user (ProductState =5)

Sub UnInstPkg (StrProdCode)

Dim oWSHShell : Set oWSHShell = CreateObject("WScript.Shell")
Dim oMSI : Set oMSI = CreateObject("WindowsInstaller.Installer")
Dim UnInstVal : UnInstVal = 1

 Do While UnInstVal <> 0
  If oMSI.ProductState (StrProdCode) = 5 Then
   ReturnVal = oWSHShell.Run("MSIExec.exe /X " & StrProdCode & " /QB!-", 0, True)
   if Not (ReturnVal = 0 OR ReturnVal = 3010) then WScript.Quit ReturnVal
   UnInstPkg (StrProdCode)
  Else
   UnInstVal = 0
  End If

  Loop
End Sub

Prompt to set OSDComputerName

I use this script to set the computer name on virtual computers during task sequence.

The computer naming script in the task sequence is using the serial number which for virtual computers is too long and will cause the task sequence to fail. Therefore its needed to somehow change the computer name. I have placed this script on a network share which i map after the computer have been given the long and faulty serial-number name. The script will prompt for a new computer name which will then overwrite the OSDComputerName task sequence variable.

Set env = CreateObject("Microsoft.SMS.TSEnvironment")
 Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
 Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",UserName,Password)
 Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
 For Each objItem in colItems
   env("OSDComputerName") = InputBox("Please enter a Computer Name:", "Computer Name")
 Next

Find all files with specific content

This Script will search c:\temp to find all files called test.txt that contains the string “server2”.

Get-ChildItem -Path c:\temp -File -Recurse -include test.txt | Select-String -pattern "server2" | Group path | Select Name

 

Replace Text in a File

This script will replace the occurence of “server1” with “server2” in all files called config.ini placed under c:\temp

Get-ChildItem -Path c:\temp -File -Recurse -include config.ini | %{(Get-Content $_) -replace "server1","server2" | Set-Content $_.fullname}

 

Windows Time Zone

I made a script to update the Windows Time Zone. In my example i want to use the Romance Standard time which is easily done by specifying this as an argument to the function SetTimeZone

SetTimeZone("Romance Standard Time")

Sub SetTimeZone(timezone)

On Error Resume Next

Set objSh = CreateObject("WScript.Shell")

Dim process, processid, strUpdateCommand
 Set process = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2:Win32_process")

'Add time change privilege to the process object
 process.Security_.Privileges.AddAsString "SeSystemTimePrivilege",True
 strUpdateCommand = "control.exe timedate.cpl,,/Z" & timezone

'Launch control.exe to refresh time zone information
process.create strUpdateCommand,Null,Null,processid

End Sub