Adding another value to script to create an or else statement

I have a script that work well in defining no screen saver policy, but I would like to add another statement into it to also make it into a 60 minute screen lockout policy by defining what group the user is attach too, here is the script at the bottom

as you can see from the script I have the "Const SPECIAL = "ou settings users (no screen saver)", I have another setting I would like to put in there that would be

"Const SPECIAL = "60 minute screen saver"

What i want it to do is basically if you are in the 60 minute saver ou group, after 60 minutes of inactivity lock it out and if you are in the no screen saver dont lock it out
''' Applying power settings for special accounts

Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & Replace(ADSysInfo.UserName, "/", "\/"))

If IsEmpty(CurrentUser.MemberOf) Then
   strGroups = ""
   ElseIf TypeName(CurrentUser.MemberOf) = "String" Then
      strGroups = LCase(CurrentUser.MemberOf)
   Else
      strGroups = LCase(Join(CurrentUser.MemberOf))
End If


Const SPECIAL = "ou settings users (no screen saver)"
Const HKEY_CURRENT_USER = &H80000001

If InStr(strGroups, SPECIAL) Then

	strComputer = "."
	
	Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
	
	strKeyPath = "Control Panel\PowerCfg"
	strEntryName = "CurrentPowerPolicy"
	objReg.GetStringValue HKEY_CURRENT_USER,strKeyPath,strEntryName,strValue
	'Wscript.Echo "Current Power Policy: " & strValue
	
	If strValue <> 2 Then
		
		strValueSet = "2"
		objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, strEntryName, strValueSet
	
	End If

End If

Open in new window

erwin_mirandaAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
Oh...I didn't change the compilation option....sorry.....use this EXE instead.

And also this code.  Make sure you set the following:
      strSetScreenSaverTimeout = "\\server\share\setscreensavertimeout.exe"
      intTimeout = 3600

Regards,

Rob.
''' Applying power settings for special accounts

Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & Replace(ADSysInfo.UserName, "/", "\/"))

If IsEmpty(CurrentUser.MemberOf) Then
   strGroups = ""
   ElseIf TypeName(CurrentUser.MemberOf) = "String" Then
      strGroups = LCase(CurrentUser.MemberOf)
   Else
      strGroups = LCase(Join(CurrentUser.MemberOf))
End If


Const SPECIAL1 = "ou settings users (no screen saver)"
Const SPECIAL2 = "60 minute screen saver"

Const HKEY_CURRENT_USER = &H80000001

If InStr(strGroups, SPECIAL1) > 0 Then

	strComputer = "."
	
	Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
	
	strKeyPath = "Control Panel\PowerCfg"
	strEntryName = "CurrentPowerPolicy"
	objReg.GetStringValue HKEY_CURRENT_USER,strKeyPath,strEntryName,strValue
	'Wscript.Echo "Current Power Policy: " & strValue
	
	If strValue <> 2 Then
		
		strValueSet = "2"
		objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, strEntryName, strValueSet
	
	End If

ElseIf InStr(strGroups, SPECIAL2) > 0 Then

	' This is where you would set your 60 minute time-out
	strSetScreenSaverTimeout = "\\server\share\setscreensavertimeout.exe"
	intTimeout = 3600
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objShell = CreateObject("WScript.Shell")
	If objFSO.FileExists(strSetScreenSaverTimeout) = True Then
		strCommand = "cmd /c " & objFSO.GetFile(strSetScreenSaverTimeout).ShortPath & " " & intTimeout
		objShell.Run strCommand, 0, True
	End If

End If

Open in new window

SetScreenSaverTimeout.txt
0
 
RobSampsonCommented:
Hi, this would be the logic you need, but I'm not sure how to set the 60 minute time out just yet.  I can look into that tomorrow.

Regards,

Rob.
''' Applying power settings for special accounts

Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & Replace(ADSysInfo.UserName, "/", "\/"))

If IsEmpty(CurrentUser.MemberOf) Then
   strGroups = ""
   ElseIf TypeName(CurrentUser.MemberOf) = "String" Then
      strGroups = LCase(CurrentUser.MemberOf)
   Else
      strGroups = LCase(Join(CurrentUser.MemberOf))
End If


Const SPECIAL1 = "ou settings users (no screen saver)"
Const SPECIAL2 = "60 minute screen saver"

Const HKEY_CURRENT_USER = &H80000001

If InStr(strGroups, SPECIAL1) > 0 Then

	strComputer = "."
	
	Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
	
	strKeyPath = "Control Panel\PowerCfg"
	strEntryName = "CurrentPowerPolicy"
	objReg.GetStringValue HKEY_CURRENT_USER,strKeyPath,strEntryName,strValue
	'Wscript.Echo "Current Power Policy: " & strValue
	
	If strValue <> 2 Then
		
		strValueSet = "2"
		objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, strEntryName, strValueSet
	
	End If

ElseIf InStr(strGroups, SPECIAL2) > 0 Then

	' This is where you would set your 60 minute time-out

End If

Open in new window

0
 
erwin_mirandaAuthor Commented:
Hello

I added this to the value of " This is where you would set your 60 minute time-out"

      Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer   & "\root\default:StdRegProv")
      
      strKeyPath = "Control Panel\Desktop"
      objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
      ValueName = "ScreenSaveTimeout"
      strValue = "1800"
      objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue


but it still did not work...
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
RobSampsonCommented:
Try this instead.  I'm not sure if you'll need to use these two lines:
      'Set objShell = CreateObject("WScript.Shell")
      'objShell.Run "RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters", 0, True

they are commented out at the moment, but if it doesn't work, try removing the apostrophe at the start, and see if that command helps.

Regards,

Rob.
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer   & "\root\default:StdRegProv")
      
      'strKeyPath = "Control Panel\Desktop"
      strKeyPath = "Software\Policies\Microsoft\Windows\Control Panel\Desktop"
      objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
      ValueName = "ScreenSaveTimeout"
      strValue = "1800"
      objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue
      'Set objShell = CreateObject("WScript.Shell")
      'objShell.Run "RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters", 0, True

Open in new window

0
 
erwin_mirandaAuthor Commented:
'strKeyPath = "Control Panel\Desktop"

is there suppose to be a paranthese at the beginning of this..
0
 
erwin_mirandaAuthor Commented:
I am sorry I am idiot...apostrophe
0
 
erwin_mirandaAuthor Commented:
Hello Sir, I got an error
it says \\domain controler\netlogon\Upity\_scripts\3-PowerSpecialUsers.vbs
Line 40
Char2
Error 0x80041021
Code 80041021
Source (null)

I found that this line is number 40
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer   & "\root\default:StdRegProv")
0
 
erwin_mirandaAuthor Commented:
Okay I got an idea, what if create another script like so

''' Applying power settings for special accounts

Set ADSysInfo = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & Replace(ADSysInfo.UserName, "/", "\/"))

If IsEmpty(CurrentUser.MemberOf) Then
   strGroups = ""
   ElseIf TypeName(CurrentUser.MemberOf) = "String" Then
      strGroups = LCase(CurrentUser.MemberOf)
   Else
      strGroups = LCase(Join(CurrentUser.MemberOf))
End If


Const SPECIAL = "60 minute screen saver"

Const HKEY_CURRENT_USER = &H80000001

If InStr(strGroups, SPECIAL) Then

	strComputer = "."
	Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer   & "\root\default:StdRegProv")
      
      'strKeyPath = "Control Panel\Desktop"
      strKeyPath = "Software\Policies\Microsoft\Windows\Control Panel\Desktop"
      objReg.CreateKey HKEY_CURRENT_USER, strKeyPath
      ValueName = "ScreenSaveTimeout"
      strValue = "1800"
      objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue
      'Set objShell = CreateObject("WScript.Shell")
      'objShell.Run "RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters", 0, True
	
   End If

End If

Open in new window

0
 
RobSampsonCommented:
The If....Then....Else statement is working the way I had it, I just forgot to add
strComputer = "."
above the
Set objReg = .....
line. Add that, and the error you got with go away.

There is supposed to be an apostrophe on this line:
      'strKeyPath = "Control Panel\Desktop"

because we're not using that right now.  Does the registry change take effect?

Rob.
0
 
erwin_mirandaAuthor Commented:
no it does not, what about creating another script like the original one but with your regisrtry setting, can that be possible
0
 
erwin_mirandaAuthor Commented:
No Sir the registry does not take effect, sir.
0
 
RobSampsonCommented:
OK, it doesn't seem that it's possible to use RunDLL32.exe to force the screen saver timeout to update.  The best I can find is this:
http://support.microsoft.com/kb/97142
http://www.freevbcode.com/ShowCode.Asp?ID=504

which suggests you can use the SystemParametersInfo function of User32.dll to set the time out.  You could compile that code into an EXE, and make the EXE accept the command line argument for the timeout value in seconds, and you could use that.  If you had access to Visual Basic, you could do it with that.

Regards,

Rob.
0
 
erwin_mirandaAuthor Commented:
How can i do that?
0
 
RobSampsonCommented:
Do you have Visual Basic?  You can download the VB.NET Express Edition, and use something like this code:

' SetLastError:=True is required for error checking
<DllImport("user32", SetLastError:=True, CharSet:=CharSet.Auto)> _
Public Shared Function SystemParametersInfo( _
            ByVal intAction As Integer, _
            ByVal intParam As Integer, _
            ByVal strParam As String, _
            ByVal intWinIniFlag As Integer) As Integer
' returns non-zero value if function succeeds
End Function

Const SPI_SETSCREENSAVERACTIVE As Integer = 17
Const SPI_SETSCREENSAVERTIMEOUT As Integer = 15
Const SPIF_UPDATEINI As Integer = 1
Const SPIF_SENDWININICHANGE As Integer = 2

Const intTimeOut As Integer = 1800
intReturn = SystemParametersInfo(SPI_SETSCREENSAVERTIMEOUT, intTimeOut, Null, SPIF_UPDATEINI + SPIF_SENDWININICHANGE)


Compile that, and when you run the EXE it will hopefully set it (although I haven't tested it).

Regards,

Rob.
0
 
RobSampsonCommented:
If you can't do it, I'll try to make the program tomorrow.
0
 
erwin_mirandaAuthor Commented:
Thank you Mr. Sampson..I will try and do it..you know..but as they say in order for you to walk you must first learn to crawl...and thank you form the bottom of my heart..
0
 
erwin_mirandaAuthor Commented:
and if i try and fail ...i will ask for help sir..
0
 
RobSampsonCommented:
Hi, I have written the very small VB.NET application called SetScreenSaverTimeout.exe.  You can use it in one of two ways:

1) at a command prompt (scriptable), use
    setscreensavertimeout <timeinseconds>

2) double-click it, and it will prompt you for the timeout in seconds

Regards.

Rob
SetScreenSaverTimeout.txt
0
 
erwin_mirandaAuthor Commented:
thank you..how would i add this to the script that i first provided and how can i check it with the OU for 60 minute screen saver, if possible to do..

I just dont want to give everybody this option..just a certain amount of people.
0
 
erwin_mirandaAuthor Commented:
SIr, how can i make it work in a 3.5 net framework...
0
 
RobSampsonCommented:
I tested it in a .NET 3.5 Framework...what error message did you get?

Rob.
0
 
erwin_mirandaAuthor Commented:
this is the error
To run this application, you must first install one of the following versions of .Net Framework:
v4.0.30319
0
 
RobSampsonCommented:
That now requires .NET Framework 3.5 or greater.
0
 
erwin_mirandaAuthor Commented:
Sorry sir it did not execute
0
 
RobSampsonCommented:
It works for me.  On Windows XP with SP3, when I go to a command prompt and run
C:\Temp\SetScreenSaverTimeout.exe 600

my screensaver timeout is set to 10 minutes.  Have you tried running it manually?

Rob.
0
 
erwin_mirandaAuthor Commented:
I put the exe on a share than map to it...using your code for my script and it did not execute....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.