Expanding your Scense building bricks collection

bricks

Even as Scense has a large feature set out of the box, that help you as an IT admin to manage the systems in your IT infra, you still might find some actions that you can’t do or require quite some (configuration) work.

Just as an example:
If you have to change a handfull of registry settings, you could use the built-in registry functionality. This allows you to set a single value at a time. Great for a few, but not practical if there are a lot of changes to apply. For example to load a default configuration for some Adobe application with 200+ registry settings, just to apply defaults.
There are alternative options available, like saving a textfile (.reg) and importing this with a reg.exe or regedit.exe import function. This however splits the configuration and the apply-action, and wouldn’t it be neat if it would be just a single setting option. Most likely you are familiar with the extensions hosted on the AppiXoft forum, and there is an extension that covers the registry import (phew). So download it, import in Scense (double-click the SPK) and you are covered. It even has a nice GUI that allows you to paste the contents of the exported registry, so it will be imported.

But what if there is no extension that does what you want to accomplish?
Sure: you could call AppiXoft and file a feature request, or post on the forum that you are looking for a specific feature (and hope someone has it).

Now comes the best part: Scense has the ability to give you the tools, so you can do it yourselves!
To show you how this can be done, and prove it is no rocket science, we will pick an extension that is good already, but make it more versatile to fit a slightly different purpose. To be honest: you need to have some scripting skills. However, if you know VBscript or powershell, this should be easy to accomplish.

My goal (in this blog):
I want to be able to change (network)drive features. This could be several actions, but for now we just focus on (un)hiding them.
There is an existing extension that allows drive hiding, but it requires to set the hiding options for all 26 driveletters (A-Z) at once. Changing a single one is not an option, and that is what we will create. The final result will be placed in the extensions on the AppiXoft forum of course 🙂

If you just want to have this ready-made extension, visit the Appixoft forum, check extensions and get the (updated) Show/hide drives extension.

Cool… you want to know more…

There are some handy things to know when starting scripting in Scense:

  • In Scense Explorer enable editing scripts:
    In the menu, select File > Options
    ScenseExplorer-ActionTemplateEditing.PNG
    Tick the option for “Allow Action template editing”, as it allows editing scripts that have a GUI for editing settings. While editing, you can hit F6 to toggle the script editor mode…
  • Check the Embedded Scripting Guide
    This is a PDF copied to your system whenever the Scense Explorer is installed. You can find this document also on the install ISO.
  • Some scripting experience helps

If you have imported one of the existing scripts into your Scense environment (download from the forum, doubleclick the SPK) you can open these scripts. In most cases you will be presented with a user interface asking you for some settings. Now hit F6, and you will see the raw code…
ScenseScripting-F6.png

If you don’t know what to do with all the text, ask someone with scripting knowledge to help you. The language used by Scense is based on VbScripting, with some extensions (additional commands). Additionally scripts can be extended with a header, which allows to create the ‘Simple’ GUI, which allows non-scripting aware IT admins to change settings for the script.

The original script used for this mini-tutorial does have some flaws. First it has some configuration issues, as you need to specify the visibility of all driveletters. Most of the time however you want to (un)hide a specific driveletter for some users.
Next it was created in the good old Windows XP era, back then the IT guy was nothing less of a god once logged on to the system. Since then Microsoft has created some improved OS versions, in which security has been bumped a bit. Writing to the Policies registry branche has been denied for the user (but this script still expects to be able to do that). More about this later on…

So I actually almost completely rewrote the existing script, as a lot had to be replaced.

Original script flow:

  • Get the selection from the user, on which drive(s) to hide
  • Calculate the bitmask
    Hiding driveletters depends on a bitmask in which each driveletter has a bit. If the bit is a ‘1’, that drive will be hidden.
  • Write the new value to the registry key:
    HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    Assign the calculated value to NoDrives (DWord)

New script flow:

  • Get the driveletter to (un)hide from the user selection
  • Get the current bitmask with the drives that are hidden
  • Depending on the option, re-calculate the new bitmask
    It is possible to select hide or unhide the drive. The bit in the bitmask is enabled/disabled, depending on the choice.
  • Write back the updated value to the registry

There was an issue with writing the value to the registry. As described before, Microsoft no longer allows updating settings in the Policies part of the registry. This allows admins to enforce settings, and users can not change this.
Luckily Scense is able to run commands as the ODS (admin) user. This way it is possible to alter the value. However, in that case the ‘HKCU’ (Current User) is not correct, as it would address the ODS user, and we want to change it for the actual user.

Luckily the registry for all users is published in the HKEY_USERS-branche. We just need the SID for the user… Arg…
But not really an issue, as Scense offers this SID as a Scense variable. So we just apply this variable (%USERSID%) and the ODS user is able to change the setting for our user.

The resulting script code:

‘<ScenseScriptHeader>
‘<ScriptParameters>
‘ <Parameter Name=”Action” ScriptVariableName=”vAction”>
‘ <GUI Control=”DropdownList”>
‘ <ListElements>
‘ <Element Name=”Do not change visibility” Value=”NOCHANGE” />
‘ <Element Name=”Hide driveletter” Value=”HIDE” />
‘ <Element Name=”Make driveletter visible” Value=”SHOW” />
‘ </ListElements>
‘ </GUI>
‘ </Parameter>
‘ <Parameter Name=”Apply to drive” ScriptVariableName=”vDriveID”>
‘ <GUI Control=”DropdownList”>
‘ <ListElements>
‘ <Element Name=”Drive A:\” Value=”0″ />
‘ <Element Name=”Drive B:\” Value=”1″ />
‘ <Element Name=”Drive C:\” Value=”2″ />
‘ <Element Name=”Drive D:\” Value=”3″ />
‘ <Element Name=”Drive E:\” Value=”4″ />
‘ <Element Name=”Drive F:\” Value=”5″ />
‘ <Element Name=”Drive G:\” Value=”6″ />
‘ <Element Name=”Drive H:\” Value=”7″ />
‘ <Element Name=”Drive I:\” Value=”8″ />
‘ <Element Name=”Drive J:\” Value=”9″ />
‘ <Element Name=”Drive K:\” Value=”10″ />
‘ <Element Name=”Drive L:\” Value=”11″ />
‘ <Element Name=”Drive M:\” Value=”12″ />
‘ <Element Name=”Drive N:\” Value=”13″ />
‘ <Element Name=”Drive O:\” Value=”14″ />
‘ <Element Name=”Drive P:\” Value=”15″ />
‘ <Element Name=”Drive Q:\” Value=”16″ />
‘ <Element Name=”Drive R:\” Value=”17″ />
‘ <Element Name=”Drive S:\” Value=”18″ />
‘ <Element Name=”Drive T:\” Value=”19″ />
‘ <Element Name=”Drive U:\” Value=”20″ />
‘ <Element Name=”Drive V:\” Value=”21″ />
‘ <Element Name=”Drive W:\” Value=”22″ />
‘ <Element Name=”Drive X:\” Value=”23″ />
‘ <Element Name=”Drive Y:\” Value=”24″ />
‘ <Element Name=”Drive Z:\” Value=”25″ />
‘ </ListElements>
‘ </GUI>
‘ </Parameter>
‘</ScriptParameters>
‘</ScenseScriptHeader>
‘<ParameterCodeBlock>
vDriveID = “7”
vAction = “HIDE”
‘</ParameterCodeBlock>

‘ Scense Runtime Extension
‘ Hide Drives
‘ Edwin Friesen
‘ Version 1.0 – June 2011
‘ —————————————-
‘ Upgraded by ScenseGuru
‘ Version 2.0 – May 2018
‘ – Win10 compliant
‘ – Single drive action only

Sub Scense_Main()

Dim RegPath

‘Registry Key Root
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006

‘Registry Key Type
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_DWORD = 4

‘ What drive do we want to hide (bitmask value)
DriveValue = 0 + 2^CInt(vDriveID)
WriteScenseLog “Drive ID” & vDriveID & ” (” & DriveValue & “)”

‘ Determine and log the current value
RegPath = ReplaceVariables(“%USERSID%\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer”)
curVal = 0
If Registry.GetKeyValue (, HKEY_USERS, RegPath, “NoDrives”) Then
curval = 0 + Registry.KeyValue
End If
WriteScenseLog “CurVal = ” & curval

‘ Depending on the action: set or reset the bitmask value
If (vAction = “HIDE”) Then
newval = curval Or DriveValue
WriteScenseLog “NewVal = ” & newval
If Not Registry.WriteKeyValue (,HKEY_USERS, RegPath, “NoDrives”, newval, REG_DWORD) Then
WriteScenseLog Registry.Result
End If
End If
If (vAction = “SHOW”) Then
newval = curval And (67108864 – DriveValue – 1)
WriteScenseLog “NewVal = ” & newval
If Not Registry.WriteKeyValue (,HKEY_USERS, RegPath, “NoDrives”, newval, REG_DWORD) Then
WriteScenseLog Registry.Result
End If
End If

End Sub

The gray text is the header, which shows the GUI. This one is a bit long, as all 26 driveletters have to be defined. The result of this header is:
ScenseScriptingGUI.PNG
This is a lot simpler for a Scense admin to change, than editing (and potentially breaking) the script code…

Next we determine the bitmask. The CINT function to convert the string from the selection can only handle 2^16 bits, but we have 26 letters in the alphabet, requiring up to 2^26. I used a trick that I do the calculation in code, as the variables allow 2^31 (signed DWord).

Next we determine the registry location for the actual user, which is done by replacing the SID in the path in the registry using the ReplaceVariables-Scense-function. We use this key to read the current value of the bitmask. If the value can’t be read (policy not defined) the default of 0 (no drives hidden) is used.

Then depending on the action (Show/hide) the resulting bitmask is calculated and saved to the registry.

Simple, isn’t it…?
Now you can alter existing extensions or create your own with new functionalities!

Cheers,
ScenseGuru

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s