 Rank: Advanced Member Medals:  Joined: 2/6/2014(UTC) Posts: 314   Thanks: 7 times Was thanked: 31 time(s) in 28 post(s)
|
Hi, @kilo1548, I have to image a bunch of devices for one of our clients. you really just need to modify the service definition to use a new GUID for the "s=" parameter. As luck would have it, I wrote a script specifically to address this problem. I've made it as generic as possible specifically so other people could benefit from it. It also has the ability to assign a specific GUID to a device should you need that capability (I did). With the GUID-assignment feature you could set the first 2 groups to represent a specific client or dept, for example. Or you could use it to re-create your image from one of your clones so that it is as consistent as possible. Or to emulate another device to test out commands or whatever. I've found several effective use-cases for it. It works very well, though you must REBOOT (not simply RESTART the service) if you are using it from within the Commands feature, since restarting the service there kills the process that's trying to restart the service, resulting in the service not coming back up. Save it as newscid.vbs and call it like so: Code:cscript.exe //nologo newscid.vbs reboot new
or Code:cscript.exe //nologo newscid.vbs reboot 12345678-1234-1234-1234-1234567890ab
You can even use an extension to run this from the Commands for a specific device GUID so that it will automatically run every single time one of the vanilla clones comes online (add whatever other customizations you need to it as well and you've got an easy way to prep 'em all). :) Code:'//=============================================================================
' "NewSCID"
' Displays or resets the active device GUID for SC.
' If you pass "new" it resets the GUID, essentially creating a new instance.
'//============================================================================
' Love it? There's more where this came from. 12pd.com
'//============================================================================
Const kVersion = "1.1.0"
' default settings
Const kService = "ScreenConnect Client%" ' service pattern name to match
Const kReboot = 0 ' reboot computer? 0=no, 1=yes
Const kRestart = 1 ' restart service? 0=no, 1=yes
Const kWaitMax = 30 ' seconds; increase wait time for slow machines
Const kBackup = 0 ' backup GUID? 0=no, 1=yes
'//=============================================================================
WScript.Echo "NewSCID " & kVersion
'//=============================================================================
'//=============================================================================
' variables
Dim oArgs, lIter, ksFilter
Dim lReboot, lRestart, iWaitMax, lBackup
Dim sOldSCGuid, sNewSCGuid
' apply defaults
lReboot = kReboot
lRestart = kRestart
iWaitMax = kWaitMax
lBackup = kBackup
sNewSCGuid = ""
' parse input
Set oArgs = WScript.Arguments
If oArgs.Count > 0 Then
For lIter = 0 To oArgs.Count-1
ksFilter = LCase(oArgs(lIter))
Select Case True
' help
Case ((ksFilter = "/?") Or (ksFilter = "/help") Or (ksFilter = "help"))
WScript.Echo "NewSCID " & kVersion
WScript.Echo "Displays or resets the active device GUID for SC."
WScript.Echo ""
WScript.Echo "Usage:"
WScript.Echo " NewSCID <options>"
WScript.Echo ""
WScript.Echo "Examples:"
WScript.Echo " NewSCID /?"
WScript.Echo " NewSCID "
WScript.Echo " NewSCID new [#]"
WScript.Echo " NewSCID reboot new"
WScript.Echo " NewSCID <guid> restart 10"
WScript.Echo ""
WScript.Echo "Options:"
WScript.Echo " The default behavior is to display the SC GUID."
WScript.Echo " /? Displays this help text."
WScript.Echo " new Assigns a new random GUID, essentially creating a new instance (ideal for cloned devices)."
WScript.Echo " restart Restart the service after updating the GUID. This should only be used interactively on-device, not through the SC Command feature."
WScript.Echo " # Any whole number, representing seconds, to wait for each service to stop or start."
WScript.Echo " norestart Disable service restart."
WScript.Echo " reboot Restart the computer after updating the GUID. This option is ideal for use within the SC Command feature."
WScript.Echo " noreboot Disable reboot."
WScript.Echo " backup Create a backup of the current GUID to ""%curdir%\sc-%timestamp%.log""."
WScript.Echo " nobackup Do no create a backup."
WScript.Echo ""
' check for GUID input
Case (Len(ksFilter) = 36)
sNewSCGuid = ksFilter
' new
Case (ksFilter = "new")
sNewSCGuid = GuidGen()
' adjust max wait time
Case (IsNumeric(ksFilter))
iWaitMax = CInt(ksFilter) ' seconds
' reboot
Case (ksFilter = "reboot")
lReboot = 1
' noreboot
Case (ksFilter = "noreboot")
lReboot = 0
' restart
Case (ksFilter = "restart")
lRestart = 1
' norestart
Case (ksFilter = "norestart")
lRestart = 0
' backup
Case (ksFilter = "backup")
lBackup = 1
' nobackup
Case (ksFilter = "nobackup")
lBackup = 0
End Select
Next
End If
'//=============================================================================
' view or change?
If sNewSCGuid = "" Then
' parse existing - we will only display it
sOldSCGuid = SCGuid()
WScript.Echo "SCGuid is " & sOldSCGuid
Else
' parse old
sOldSCGuid = SCGuid()
WScript.Echo "SCGuid was " & sOldSCGuid
' announce new
WScript.Echo "SCGuid will be " & sNewSCGuid
' set new
sNewSCGuid = SCGuidSet(sNewSCGuid)
WScript.Echo "SCGuid is " & sNewSCGuid
' reboot/restart
If (lReboot) Then
Call RebootDevice()
ElseIf (lRestart) Then
Call RestartService()
End If
End If
'//=============================================================================
'//=============================================================================
Function RebootDevice()
Dim oShell
Set oShell = CreateObject("WScript.Shell")
WScript.Echo "Rebooting"
oShell.Run "%comspec% /c shutdown /r /t 0 /f", "", TRUE
Set oShell = Nothing
End Function
'//=============================================================================
'//=============================================================================
Function RestartService()
Dim scName, oQuery, oWMI, cService
Dim iWait
' parse services
Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
Set oQuery = oWMI.ExecQuery("Select * from Win32_Service where Name like '" & kService & "'")
For each cService in oQuery
scName = Trim( cService.Name )
WScript.Echo "Restarting " & scName
' stop the service
iWait = 0
cService.StopService()
Do
If cService.State = "Stopped" Then
Exit Do
End If
Call Sleep(1)
iWait = iWait + 1
Loop Until iWait = iWaitMax
WScript.Echo "Stopped: " & scName
' start the service
iWait = 0
cService.StartService()
Do
If cService.State = "Running" Then
Exit Do
End If
Call Sleep(1)
iWait = iWait + 1
Loop Until iWait = iWaitMax
WScript.Echo "Started: " & scName
Next
End Function
'//=============================================================================
'//=============================================================================
Function SCGuidSet(sNewGuid)
On Error Resume Next
Dim oReg, kRoot, sKey, sEntry, sValue, aVersion, lIter, aParts
Dim scName, oQuery, oWMI, cService, sOldGuid
kRoot = &H80000002
' parse services
Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set oQuery = oWMI.ExecQuery("Select * from Win32_Service where Name like '" & kService & "'")
For Each cService in oQuery
scName = Trim( cService.Name )
sKey = "SYSTEM\CurrentControlSet\services\" & scName
sEntry = "ImagePath"
oReg.GetExpandedStringValue kRoot, sKey, sEntry, sValue
' parse the SC service for relevant data
aVersion = Split( sValue, "&" )
For lIter = 0 to UBound(aVersion)
aParts = Split(aVersion(lIter), "=")
If aParts(0) = "s" Then
sOldGuid = aParts(1)
Exit For
End If
Next
' record it if necessary
If (lBackup = 1) Then
Call LogSCID(scName & vbTab & sOldGuid)
End If
' update it with the new SC Guid
sValue = Replace(sValue, sOldGuid, sNewGuid)
oReg.SetExpandedStringValue kRoot, sKey, sEntry, sValue
sNewGuid = SCGuid()
Next
' return
Set cService = Nothing
Set oQuery = Nothing
Set oWMI = Nothing
Set oReg = Nothing
SCGuidSet = sNewGuid
End Function
'//=============================================================================
'//=============================================================================
Function SCGuid()
On Error Resume Next
Dim oReg, kRoot, sKey, sEntry, sVersion, aVersion, lIter, aParts
Dim scName, oQuery, oWMI, cService
kRoot = &H80000002
' parse services
Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set oQuery = oWMI.ExecQuery("Select * from Win32_Service where Name like '" & kService & "'")
For each cService in oQuery
scName = Trim( cService.Name )
sKey = "SYSTEM\CurrentControlSet\services\" & scName
sEntry = "ImagePath"
oReg.GetStringValue kRoot, sKey, sEntry, sVersion
If sVersion = "" Then oReg.GetExpandedStringValue kRoot, sKey, sEntry, sVersion
' parse the SC service for relevant data
aVersion = Split( sVersion, "&" )
sVersion = ""
For lIter = 0 to UBound(aVersion)
aParts = Split(aVersion(lIter), "=")
If aParts(0) = "s" Then
sVersion = aParts(1)
Exit For
End If
Next
' record it if necessary
If (lBackup = 1) Then
Call LogSCID(scName & vbTab & sVersion)
End If
Next
' return
Set cService = Nothing
Set oQuery = Nothing
Set oWMI = Nothing
Set oReg = Nothing
SCGuid = sVersion
End Function
'//=============================================================================
'//=============================================================================
Function LogSCID(sLog)
Dim sFile, oFSO, tsFile
sFile = "sc-" & ISO8601T(Now) & ".txt"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set tsFile = oFSO.OpenTextFile( sFile, 1, 0 )
tsFile.WriteLine sLog
tsFile.Close
Set tsFile = Nothing
Set oFSO = Nothing
End Function
'//=============================================================================
'//=============================================================================
Function ISO8601T(dDate)
' yyyymmddThhnnss format
ISO8601T = Year( dDate ) _
& Right("0" & Trim ( Month( dDate ) ), 2) _
& Right("0" & Trim ( Day( dDate ) ), 2) & "T" _
& Right("0" & Trim ( Hour( dDate ) ), 2) _
& Right("0" & Trim ( Minute( dDate ) ), 2) _
& Right("0" & Trim ( Second( dDate ) ), 2)
End Function
'//=============================================================================
'//=============================================================================
Sub Sleep(ByVal iSeconds)
Dim dStart, bDone
dStart = Now()
bDone = False
While Not bDone
If DateDiff("s", dStart, Now()) >= CInt(iSeconds) Then
bDone = True
End If
Wend
End Sub
'//=============================================================================
'//=============================================================================
Function GuidGen()
Dim oTypeLib, sNewGuid
' create a new guid
Set oTypeLib = CreateObject("Scriptlet.TypeLib")
sNewGuid = LCase(oTypeLib.Guid)
Set oTypeLib = Nothing
' trim Null
sNewGuid = Left(sNewGuid, Len(sNewGuid)-2)
' remove braces
sNewGuid = Replace(sNewGuid, "{", "")
sNewGuid = Replace(sNewGuid, "}", "")
' return
GuidGen = sNewGuid
End Function
'//=============================================================================
|
 1 user thanked shawnkhall for this useful post.
|
|