Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need to check if script has ran, Version of client and then run command

Posted on 2011-03-10
3
Medium Priority
?
366 Views
Last Modified: 2012-08-14
Hi Guy's,

I would be very gratefull if someone could assistme with a script I'm trying to create. First let me give you a little back ground, we have an app that is used through out the company, this app's settings etc is controlled via settings in HKLM > Software > Policies >AppName, the company has decided it wants to update these settings by pushing out some new reg settings. After some testing it was found this was best done by deleting the old settings 1st of all and then apllying the new, to make matters more complicated we have a mix of Win XP and Win 7.

The script that I have hobbled together tries to do the following:

Check for the presence of a certain regkey to see if the script should be run (Regkey found at HKCU > Software >AppName. If the Reg key does not exists it should carry on,  if it does the entire script should stop.
If the script is to continue check to see if client is XP or Win 7 by checking for the folder C:\Users and then run the appropriate commands.
Add the Regkey mentioned above so that the script does not run again.

 
on error resume next
Dim WSHShell, RNetKey, SetRNetRanValue, wshEnvironment, Folder, AppName1, AppName2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("WScript.Shell")
Set wshEnvironment = wshShell.Environment("System")
Set oWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOSInfo = oWMIService.ExecQuery("Select * from Win32_OperatingSystem")
set shell = WScript.CreateObject("WScript.Shell")
RNetKey = "HKCU\Software\AppName\"
SetRNetRanValue = WSHShell.RegRead(RNetKey & "SetRNetRan")
AppName1 = "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\AppName\"
AppName2 = "HKEY_CURRENT_USER\SOFTWARE\Policies\AppName\"

Set oshell = CreateObject("WScript.Shell")
Err.Clear
strRegKey = oshell.RegRead ("HKCU\Software\AppName\")

If Err.Number <> 0 Then      
 If objFSO.FolderExists("C:\Users") Then 
  Set objShell = CreateObject("Shell.Application")
  'Pass a bogus argument with leading blank space, say [ uac]
    objShell.ShellExecute "wscript.exe", Chr(34) & _
  	WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
on error resume next
Set WSHShell = WScript.CreateObject("WScript.Shell")
  	WSHShell.RegDelete AppName1
  	WSHShell.RegDelete AppName2
  	WSHShell.Run "regedit /s \\servername\install\AppName.reg"
  	WScript.Quit
Else
  	WSHShell.RegDelete AppName1
  	WSHShell.RegDelete AppName2
  	WshShell.Run "regedit /s \\servername\install\AppName.reg"
  	WScript.Quit
End If
        
 if not SetRNetRanValue = "1" Then
		RunSetRNetLocation RNetKey
			Else
'WScript.echo "SetRnetRan set"
	End If

Function RunSetRNetLocation( RNetKey)
'wscript.echo "SetRNetRan not set"
	WshShell.RegWrite RNetKey, 0 ,"REG_SZ"
	WSHShell.RegWrite RNetKey & "\" & "SetRNetRan", "1"
	WScript.Quit
   End Function
   
      End

Open in new window

0
Comment
Question by:arrols
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
connectex earned 2000 total points
ID: 35097046
So exactly where are you stumped?

BTW, Use this code for OS checking. Much more versatile:

 
' Get OS name
Dim objWMIService, colItems, objItem

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objItem in colItems
  strOSName = objItem.Caption
Next

'Test for desired versions
If InStr(1, strOSName, "windows 7", vbTextCompare) Then
If InStr(1, strOSName, "2008", vbTextCompare) Then 
If InStr(1, strOSName, "vista", vbTextCompare) Then
If InStr(1, strOSName, "xp", vbTextCompare) Then
If InStr(1, strOSName, "2003", vbTextCompare) Then

Open in new window

0
 

Author Closing Comment

by:arrols
ID: 35097764
Great, that gave me exactly what I need and my script is now working.

Cheers
0
 
LVL 13

Expert Comment

by:connectex
ID: 35098644
Glad I could help. One minor thing it would probably be wise to include a fall through if you don't detect one of the desired OSes. One of my scripts cleans up all the local temp folder for all profiles on the system. I wrote it prior to Windows 7 being release it didn't have a test for it. So when it first run on Windows 7 it didn't just delete my temp folder, it cleanup my desktop, favorites and other folders under my profile. Ouch! So I changed the code to fall through and say "Unsupported OS" if it finds something I didn't expect at the time of coding.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

598 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question