Jump to content

Outlook profile setup logon script help


Recommended Posts

Posted
The only message is what was in my previous reply unfortunately although with the suggested change I now have the numbers (23, 15)!

 


If objFSO.FileExists(strFile) Then

 

Only thing I can think of is maybe there is a space in the path which is throwing it out so wondering if we use the Trim function ie

 


strFile = "C:\Users\" & Trim(strUserName) & "\AppData\Local\Microsoft\Outlook\outlook.pst"

If objFSO.FileExists(Trim(strFile)) Then


Posted
On Error GoTo HandleError is VB only, not VBS.

 

Steve

 

In that case delete the on error goto lines and also the HandleError: line as well ( when I googled it , it showed that you could use this on vbs ) obviously said site was wrong.

Posted
Still no joy unfortunately. Same as before. If I new a little vbs myself I wouldn't feel so useless. Maybe I should set some time aside!!
Posted
Still no joy unfortunately. Same as before. If I new a little vbs myself I wouldn't feel so useless. Maybe I should set some time aside!!

 

 

Dim objNetwork, strUserName, strDirectory, objFSO
Dim objShell
Dim Version
' Create required objects ie Shell, FileSystemObject and Network
set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = WScript.CreateObject("WScript.Network")

'Get user's name
strUserName = objNetwork.UserName 

'See if the user needs Outlook configured
'strDirectory = "C:\Users\" & strusername & "AppData\Local\Microsoft\Outlook"

'Import PRF
If objFSO.FileExists("C:\Users\" & strUserName & "\AppData\Local\Microsoft\Outlook\outlook.pst") Then
'Wscript.Echo "Skipped the registry edits. Directory exists
Else
'Wscript.Echo "Start performing registry edits. Directory does not exist


objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run"
objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun"
objShell.RegWrite "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\ImportPRF","\\MyServer\Outlook Profiles\CVHS IMAP Profile.prf","REG_SZ"
End IF

 

Runs without errors here (Just commented out the regdeeltes so I didn't kill my pc, but should be fine).

 

Does that work for you?

 

Steve

Posted

This script from the link I provided on the first page works fine and even gives a machanism to push out an update should the prf be modified but checks for whether the profile has already been setup rather than whether a file exists meaning that it won't run again when logging onto a different computer. I had hoped that it could be modified to do this but again am a little lost.

 

'This script determines if a specified mail profile already exists.
'If it doesn't, it will set the path to the prf-file containing
'the mail profile configuration settings.
'Script created by: Robert Sparnaaij
'For more information about this file see;
'http://www.howto-outlook.com/howto/deployprf.htm

'=====BEGIN EDITING=====
'Name of mail profile as in the prf-file
ProfileName = "CVHS IMAP Profile"
'Path to the prf-file
ProfilePath = "\\MyServer\Outlook Profiles\CVHS IMAP Profile.prf"
'Increase the ProfileVersion whenever you want to reapply the prf-file
ProfileVersion = 3
'======STOP EDITING UNLESS YOU KNOW WHAT YOU ARE DOING=====
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & _
   strComputer & "\root\default:StdRegProv")

strKeyProfilePath = _ 
"Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\" _ 
& ProfileName & "\9375CFF0413111d3B88A00104B2A6676"
strLastChangeVer = "LastChangeVer"
objRegistry.GetBinaryValue _
HKEY_CURRENT_USER,strKeyProfilePath,strLastChangeVer,strValueLastChangeVer
If ProfileVersion > 1 Then
   strKeyProfileVersionPath = "SOFTWARE\HowTo-Outlook\DeployPRF"
   strProfileVersionName = ProfileName
   objRegistry.GetDWORDValue _
    HKEY_CURRENT_USER,strKeyProfileVersionPath,strProfileVersionName,strValueProfileVersion
   If IsNull(strValueProfileVersion) OR ProfileVersion > strValueProfileVersion Then
ReapplyPrf = True
   End If
End If
If IsNull(strValueLastChangeVer) OR ReapplyPrf Then
   'The mail profile doesn't exist yet so we'll set the the ImportPRF key and remove the FirstRun keys
   'Determine path to outlook.exe
   strKeyOutlookAppPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"
   strOutlookPath = "Path"
   objRegistry.GetStringValue _
    HKEY_LOCAL_MACHINE,strKeyOutlookAppPath,strOutlookPath,strOutlookPathValue
   'Verify that the outlook.exe and the configured prf-file exist
   Set objFSO = CreateObject("Scripting.FileSystemObject") 
   If objFSO.FileExists(strOutlookPathValue & "outlook.exe") AND objFSO.FileExists(ProfilePath) Then
'Determine version of Outlook
strOutlookVersionNumber = objFSO.GetFileVersion(strOutlookPathValue & "outlook.exe")
strOutlookVersion = Left(strOutlookVersionNumber, inStr(strOutlookVersionNumber, ".0") + 1)
'Create the Setup key, set the ImportPRF value and delete the First-Run values.
strKeyOutlookSetupPath = "SOFTWARE\Microsoft\Office\" & strOutlookVersion & "\Outlook\Setup"
strImportPRFValueName = "ImportPRF"
strImportPRFValue = ProfilePath
objRegistry.CreateKey HKEY_CURRENT_USER,strKeyOutlookSetupPath
objRegistry.SetStringValue HKEY_CURRENT_USER,_
     strKeyOutlookSetupPath,strImportPRFValueName,strImportPRFValue
strFirstRunValueName = "FirstRun"
objRegistry.DeleteValue HKEY_CURRENT_USER,_
     strKeyOutlookSetupPath,strFirstRunValueName
strFirstRun2ValueName = "First-Run"
objRegistry.DeleteValue HKEY_CURRENT_USER,_
     strKeyOutlookSetupPath,strFirstRun2ValueName
'Save the applied ProfileVersion if larger than 1.
If ProfileVersion > 1 Then
    objRegistry.CreateKey HKEY_CURRENT_USER,strKeyProfileVersionPath
    objRegistry.SetDWORDValue HKEY_CURRENT_USER,_
 strKeyProfileVersionPath,strProfileVersionName,ProfileVersion
End If
   Else 
       Wscript.Echo "Crucial file in script could not be found." &vbNewLine & _
       "Please contact your system administrator." 
   End If
Else
   'The mail profile already exists so there is no need to launch Outlook with the profile switch.
   'Of course you are free to do something else here with the knowledge that the mail profile exists.
End If
'Cleaup
Set objRegistry = Nothing
Set objFSO = Nothing

Posted
Runs without errors here (Just commented out the regdeeltes so I didn't kill my pc, but should be fine).

 

Does that work for you?

 

Steve

 

It's definitely not liking line 23 character 15 which is the "On Error..." line

 

I take it I should still be calling this script through cscript?

 

Anyway, I've commented out the "HandleError" lines for the time being seeing as it's not happy with this and the script now moves on but stops at the first RegDelete line with the message "Unable to remove registry key "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run"

Posted
It's definitely not liking line 23 character 15 which is the "On Error..." line

 

I take it I should still be calling this script through cscript?

 

Anyway, I've commented out the "HandleError" lines for the time being seeing as it's not happy with this and the script now moves on but stops at the first RegDelete line with the message "Unable to remove registry key "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run"

 

The one I copied doesn't have those lines if you look at the code section on that comment. :)

 

I'd suggest the problem is that you have no trailing \ on it. "Generally" \ means delete all hierarchy too, if there is a key inside it, aka Default etc it won't delete without it.

 

Try this one:

 

Dim objNetwork, strUserName, strDirectory, objFSO
Dim objShell
Dim Version
' Create required objects ie Shell, FileSystemObject and Network
set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = WScript.CreateObject("WScript.Network")

'Get user's name
strUserName = objNetwork.UserName 

'See if the user needs Outlook configured
'strDirectory = "C:\Users\" & strusername & "AppData\Local\Microsoft\Outlook"

'Import PRF
If objFSO.FileExists("C:\Users\" & strUserName & "\AppData\Local\Microsoft\Outlook\outlook.pst") Then
'Wscript.Echo "Skipped the registry edits. Directory exists
Else
'Wscript.Echo "Start performing registry edits. Directory does not exist
objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run\"
objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun\"
objShell.RegWrite "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\ImportPRF","\\MyServer\Outlook Profiles\CVHS IMAP Profile.prf","REG_SZ"
End IF

 

Can't test it here I'm afraid :(

 

Steve

  • Thanks 1
Posted
The one I copied doesn't have those lines if you look at the code section on that comment. :)

 

I'd suggest the problem is that you have no trailing \ on it. "Generally" \ means delete all hierarchy too, if there is a key inside it, aka Default etc it won't delete without it.

 

Try this one:

 

Dim objNetwork, strUserName, strDirectory, objFSO
Dim objShell
Dim Version
' Create required objects ie Shell, FileSystemObject and Network
set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = WScript.CreateObject("WScript.Network")

'Get user's name
strUserName = objNetwork.UserName 

'See if the user needs Outlook configured
'strDirectory = "C:\Users\" & strusername & "AppData\Local\Microsoft\Outlook"

'Import PRF
If objFSO.FileExists("C:\Users\" & strUserName & "\AppData\Local\Microsoft\Outlook\outlook.pst") Then
'Wscript.Echo "Skipped the registry edits. Directory exists
Else
'Wscript.Echo "Start performing registry edits. Directory does not exist
objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run\"
objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun\"
objShell.RegWrite "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\ImportPRF","\\MyServer\Outlook Profiles\CVHS IMAP Profile.prf","REG_SZ"
End IF

 

Can't test it here I'm afraid :(

 

Steve

 

Thank's for letting me know ref vbs and no error handling with ref to on error go to etc. Learning all the time :)

 

Also the last if else statement, how would you make it so that if the file does not exist ie

 

If <> objFSO.FileExist("Path") Then

write reg

write reg

delete reg

End If

 

As he is not doing anything otherwise, just to neaten the code up, that or use a select case but then we would need to know how FileExist returns ie true or false or 1 or 0 or what exactly

Posted
Thank's for letting me know ref vbs and no error handling with ref to on error go to etc. Learning all the time :)

 

Also the last if else statement, how would you make it so that if the file does not exist ie

 

If <> objFSO.FileExist("Path") Then

write reg

write reg

delete reg

End If

 

As he is not doing anything otherwise, just to neaten the code up, that or use a select case but then we would need to know how FileExist returns ie true or false or 1 or 0 or what exactly

 

You mean doing it without the "if exists do nothing" bit?

 

If objFSO.FileExists("path") = False Then

 

Steve

Posted (edited)
You mean doing it without the "if exists do nothing" bit?

 

If objFSO.FileExists("path") = False Then

 

Steve

 

current code :


If objFSO.FileExists("C:\Users\" & strUserName & "\AppData\Local\Microsoft\Outlook\outlook.pst") Then 
'Wscript.Echo "Skipped the registry edits. Directory exists 
Else
'Wscript.Echo "Start performing registry edits. Directory does not exist 
objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run\" 
objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun\" 
objShell.RegWrite "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\ImportPRF","\\MyServer\Outlook Profiles\CVHS IMAP Profile.prf","REG_SZ" 
End IF

 

Which has a wscript.echo commented out and no other code to execute in the section if the file does exist, so yes, if it is false then write the reg entries otherwise carry on with the remainder code

Edited by mac_shinobi
Posted
current code :


If objFSO.FileExists("C:\Users\" & strUserName & "\AppData\Local\Microsoft\Outlook\outlook.pst") Then 'Wscript.Echo "Skipped the registry edits. Directory exists Else 'Wscript.Echo "Start performing registry edits. Directory does not exist objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run\" objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun\" objShell.RegWrite "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\ImportPRF","\\MyServer\Outlook Profiles\CVHS IMAP Profile.prf","REG_SZ" End IF

 

Which has a wscript.echo commented out and no other code to execute in the section if the file does exist, so yes, if it is false then write the reg entries otherwise carry on with the remainder code

 

Aye just do it as a:

 

If objFSO.FileExists("path") = False Then
"Do Delete and Stuff"
End if

  • Thanks 1
Posted
Ok, Ok, I'm using your one Steve21 and although the registry "First-Run..." is deleted, the script gets stuck here with the error "Unable to remove registry key "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run", Code: 80070002, Source WshShell.RegDelete. Because of this the RegWrite doesn't get added...
Posted (edited)
Ok, Ok, I'm using your one Steve21 and although the registry "First-Run..." is deleted, the script gets stuck here with the error "Unable to remove registry key "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run", Code: 80070002, Source WshShell.RegDelete. Because of this the RegWrite doesn't get added...

 

1. When executing the script you are using cscript as the interpreter ie so you are using a bat file to execute the vbs using cscript.exe

2. On the machine , does the user account you are using have access rights to delete or write to the registry ?

 

So if you manually navigate to the registry key if it exists, right click on key or sub key --> permissions and said user account has relevant access rights ( read / write / delete etc )

 

Also noticed, not sure if it was an accident but there is a space in the reg key

 

"HKCU\Software\Microsoft\Office\14.0\Outlook\Setup \First-Run"

 

Or was that you typing the key ??

 

Also I presume the path to the reg key you have gotten rid of the last back slashes so it is :

 

objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun"

 

Instead of

objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun[b]\[/b]"

Edited by mac_shinobi
Posted
This script from the link I provided on the first page works fine and even gives a machanism to push out an update should the prf be modified but checks for whether the profile has already been setup rather than whether a file exists meaning that it won't run again when logging onto a different computer. I had hoped that it could be modified to do this but again am a little lost.

 

'This script determines if a specified mail profile already exists.
'If it doesn't, it will set the path to the prf-file containing
'the mail profile configuration settings.
'Script created by: Robert Sparnaaij
'For more information about this file see;
'http://www.howto-outlook.com/howto/deployprf.htm

'=====BEGIN EDITING=====
'Name of mail profile as in the prf-file
ProfileName = "CVHS IMAP Profile"
'Path to the prf-file
ProfilePath = "\\MyServer\Outlook Profiles\CVHS IMAP Profile.prf"
'Increase the ProfileVersion whenever you want to reapply the prf-file
ProfileVersion = 3
'======STOP EDITING UNLESS YOU KNOW WHAT YOU ARE DOING=====
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & _
   strComputer & "\root\default:StdRegProv")

strKeyProfilePath = _ 
"Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\" _ 
& ProfileName & "\9375CFF0413111d3B88A00104B2A6676"
strLastChangeVer = "LastChangeVer"
objRegistry.GetBinaryValue _
HKEY_CURRENT_USER,strKeyProfilePath,strLastChangeVer,strValueLastChangeVer
If ProfileVersion > 1 Then
   strKeyProfileVersionPath = "SOFTWARE\HowTo-Outlook\DeployPRF"
   strProfileVersionName = ProfileName
   objRegistry.GetDWORDValue _
    HKEY_CURRENT_USER,strKeyProfileVersionPath,strProfileVersionName,strValueProfileVersion
   If IsNull(strValueProfileVersion) OR ProfileVersion > strValueProfileVersion Then
ReapplyPrf = True
   End If
End If
If IsNull(strValueLastChangeVer) OR ReapplyPrf Then
   'The mail profile doesn't exist yet so we'll set the the ImportPRF key and remove the FirstRun keys
   'Determine path to outlook.exe
   strKeyOutlookAppPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"
   strOutlookPath = "Path"
   objRegistry.GetStringValue _
    HKEY_LOCAL_MACHINE,strKeyOutlookAppPath,strOutlookPath,strOutlookPathValue
   'Verify that the outlook.exe and the configured prf-file exist
   Set objFSO = CreateObject("Scripting.FileSystemObject") 
   If objFSO.FileExists(strOutlookPathValue & "outlook.exe") AND objFSO.FileExists(ProfilePath) Then
'Determine version of Outlook
strOutlookVersionNumber = objFSO.GetFileVersion(strOutlookPathValue & "outlook.exe")
strOutlookVersion = Left(strOutlookVersionNumber, inStr(strOutlookVersionNumber, ".0") + 1)
'Create the Setup key, set the ImportPRF value and delete the First-Run values.
strKeyOutlookSetupPath = "SOFTWARE\Microsoft\Office\" & strOutlookVersion & "\Outlook\Setup"
strImportPRFValueName = "ImportPRF"
strImportPRFValue = ProfilePath
objRegistry.CreateKey HKEY_CURRENT_USER,strKeyOutlookSetupPath
objRegistry.SetStringValue HKEY_CURRENT_USER,_
     strKeyOutlookSetupPath,strImportPRFValueName,strImportPRFValue
strFirstRunValueName = "FirstRun"
objRegistry.DeleteValue HKEY_CURRENT_USER,_
     strKeyOutlookSetupPath,strFirstRunValueName
strFirstRun2ValueName = "First-Run"
objRegistry.DeleteValue HKEY_CURRENT_USER,_
     strKeyOutlookSetupPath,strFirstRun2ValueName
'Save the applied ProfileVersion if larger than 1.
If ProfileVersion > 1 Then
    objRegistry.CreateKey HKEY_CURRENT_USER,strKeyProfileVersionPath
    objRegistry.SetDWORDValue HKEY_CURRENT_USER,_
 strKeyProfileVersionPath,strProfileVersionName,ProfileVersion
End If
   Else 
       Wscript.Echo "Crucial file in script could not be found." &vbNewLine & _
       "Please contact your system administrator." 
   End If
Else
   'The mail profile already exists so there is no need to launch Outlook with the profile switch.
   'Of course you are free to do something else here with the knowledge that the mail profile exists.
End If
'Cleaup
Set objRegistry = Nothing
Set objFSO = Nothing

 

What do you need doing to this script or what is it doing or not doing that you need etc ?

Posted
Ok, Ok, I'm using your one Steve21 and although the registry "First-Run..." is deleted, the script gets stuck here with the error "Unable to remove registry key "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run", Code: 80070002, Source WshShell.RegDelete. Because of this the RegWrite doesn't get added...

 

Suggests the key/path doesn't exist, as you have no error checking to ensure if it exists before deleting it.

 

I'm assuming if you re-run the script now, it'll error on the first line too as that no longer exists? (Assuming it's not been re-created).

 

Really you want to ensure the keys exist before deleting them, as you're only checking the main path, not each key.

 

Steve

Posted

I'm using cscript as the interpreter through the batch file that calls the vbs

 

I'm running this as a domain adminstrator for testing purposes so have full control of the registry

 

I think the space must have been added by the site formatting as don't have a space in my script

 

There is no backslash at the end of the reg keys.

 

I probably shouldn't have posted the other script as am just adding more problems ;) This script checked for whether the profile has already been setup rather than whether a file exists so isn't so effective when using an IMAP setup that requires a personal data file on the local computer to work properly. This file (outlook.pst) is added through the prf file via logon script but obviously needs to run on each computer which won't happen due to the HKCU showing that the profile has already been setup and so not running again hence why I'm looking at a file checking method instead.

 

Suggests the key/path doesn't exist, as you have no error checking to ensure if it exists before deleting it.

 

I'm assuming if you re-run the script now, it'll error on the first line too as that no longer exists? (Assuming it's not been re-created).

 

Really you want to ensure the keys exist before deleting them, as you're only checking the main path, not each key.

 

I thought that might be the case but again am a little clueless on how to implement this although looking at the registry location, the "First-Run" value does exist so not sure why this can't be deleted

Posted
I'm using cscript as the interpreter through the batch file that calls the vbs

 

I'm running this as a domain adminstrator for testing purposes so have full control of the registry

 

I think the space must have been added by the site formatting as don't have a space in my script

 

There is no backslash at the end of the reg keys.

 

I probably shouldn't have posted the other script as am just adding more problems ;) This script checked for whether the profile has already been setup rather than whether a file exists so isn't so effective when using an IMAP setup that requires a personal data file on the local computer to work properly. This file (outlook.pst) is added through the prf file via logon script but obviously needs to run on each computer which won't happen due to the HKCU showing that the profile has already been setup and so not running again hence why I'm looking at a file checking method instead.

 

 

 

I thought that might be the case but again am a little clueless on how to implement this although looking at the registry location, the "First-Run" value does exist so not sure why this can't be deleted

 

If you could list what you want the script to do ie

 

Check registry keys exist for ones you want to create, if they do then don't re create them, if they don't then create them

Check registry keys for the ones you want deleting, if they don't exist, ignore, if they do then delete them.

 

Check for the existance of the prf file in said location, if it does not exist then copy the prf file from your server to the client, if it does then ignore and continue

 

What other things need to be done ??

Posted

Basically the same:

 

Check whether "C:\Users\%username%\AppData\Local\Microsoft\Outlook\outlook.pst" exists and if not then delete the First-Run/FirstRun values (if they exist) and load up the "ImportPRF" value

 

Going back to the previous script, how do I add in checking for whether the value exists before attempting to delete it like Steve21 suggests as this seems to be what's causing the script to halt!

Posted (edited)
Basically the same:

 

Check whether "C:\Users\%username%\AppData\Local\Microsoft\Outlook\outlook.pst" exists and if not then delete the First-Run/FirstRun values (if they exist) and load up the "ImportPRF" value

 

Going back to the previous script, how do I add in checking for whether the value exists before attempting to delete it like Steve21 suggests as this seems to be what's causing the script to halt!

 

I would use the 3rd chunk of code ( function ) as per here :

 

Checking if Registry Key's or Value's exist - VBScript FAQ - Tek-Tips

 

Taking into consideration that the last back slash of the registry path will indicate to the script if you are looking for a key or a value

 

'Requirements: The registry key/value you are looking for (RegistryItem)

'Note: RegistryItem MUST end in a backslash (\) if you are looking for a key

' RegistryItem's without a backslash (\) will assume you are looking for a value

Function RegistryItemExists (RegistryItem)

'If there isnt the item when we read it, it will return an error, so we need to resume

On Error Resume Next

 

2 Shorter ways of the 3rd function from the first site here :

 

http://yorch.org/2011/10/two-ways-to-check-if-a-registry-key-exists-using-vbscript/

Edited by mac_shinobi
Posted

That was the first article I found when searching and looked good but couldn't see what bit I needed. I've taken the part you suggested and added it to the script so now have

'Wscript.Echo "Start performing registry edits. Directory does not exist
Function RegistryItemExists ("HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run")
 'If there isnt the item when we read it, it will return an error, so we need to resume
 On Error Resume Next
objShell.RegDelete "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run"
Function RegistryItemExists ("HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun")
 'If there isnt the item when we read it, it will return an error, so we need to resume
 On Error Resume Next
objShell.RegDelete "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun"
objShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\Setup\ImportPRF","\\MyServer\Outlook Profiles\CVHS IMAP Profile.prf","REG_SZ"
End IF

 

However I know this is wrong because "On Error Resume Next" is going to simply run the next line which isn't what should happen so how do I implement this. Apologies for my ignorance once more.

Posted (edited)

I would look at the 2nd link I posted ( edited ) as that has a shorter version.

 

Depending on if you are checking for values or just the keys will determine which chunk of code you would use

 

http://yorch.org/2011/10/two-ways-to-check-if-a-registry-key-exists-using-vbscript/

 

If it doesn't work ( due to the resume next ) I will create a reg key on my machine and have a play around with it.

 


[color=#E56717][b]Function[/b][/color] RegKeyExists(Key)  [color=#151B8D][b]Dim[/b][/color] oShell, entry  [color=#151B8D][b]On[/b][/color] [color=#151B8D][b]Error[/b][/color] [color=#151B8D][b]Resume[/b][/color] [color=#8D38C9][b]Next[/b][/color]   [color=#151B8D][b]Set[/b][/color] oShell = [color=#E56717][b]CreateObject[/b][/color]([color=#800000]"WScript.Shell"[/color])  entry = oShell.RegRead(Key)  [color=#8D38C9][b]If[/b][/color] Err.Number <> 0 [color=#8D38C9][b]Then[/b][/color]    Err.Clear    RegKeyExists = [color=#00C2FF][b]False[/b][/color]  [color=#8D38C9][b]Else[/b][/color]    Err.Clear    RegKeyExists = [color=#00C2FF][b]True[/b][/color]  [color=#8D38C9][b]End[/b][/color] [color=#8D38C9][b]If[/b][/color][color=#8D38C9][b]End[/b][/color] [color=#E56717][b]Function

[/b][/color]The first one is using the method RegRead from WScript.Shell.If the given key is not found, it will rise an error, so we need to use an On Error Resume Next (which I don’t really like).

 

We would need to pass to the function a string like HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ (note the trailing \) if we are looking for a Registry Key (those that look like a folder). If we want to check if a value inside a key exists, we would remove the trailing \, like HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3\CurrentLevel.

Edited by mac_shinobi
  • Thanks 1
Posted (edited)

Can't really test it but this logic should work, not sure if I missed anything out but:

 

Dim objNetwork, strUserName, strDirectory, objFSO
Dim objShell
Dim Version

set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = WScript.CreateObject("WScript.Network")

strUserName = objNetwork.UserName 

If objFSO.FileExists("C:\Users\" & strUserName & "\AppData\Local\Microsoft\Outlook\outlook.pst") Then

Else


Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "Software\Microsoft\Office\14.0\Outlook\Setup"

strValueName = "First-Run"
oReg.GetBinaryValue  HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

If IsNull(dwValue) Then
   
Else
   objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run\"
End If


strValueName = "FirstRun"
oReg.GetBinaryValue  HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue2

If IsNull(dwValue2) Then
   
Else
   objShell.RegDelete "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\FirstRun\"
End If


objShell.RegWrite "HKCU\Software\Microsoft\Office\14.0\Outlook\Setup\ImportPRF","\\MyServer\Outlook Profiles\CVHS IMAP Profile.prf","REG_SZ"



End IF

 

*Edit - Did a quick check with echo's instead of deletes and seems to work for me. Any luck Randle?

 

Steve

Edited by Steve21
  • Thanks 1
Posted
I've populated it as follows with the key, which I'm taking it I need to do!?
Function RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\Setup\First-Run")
 Dim oShell, entry
 On Error Resume Next
 Set oShell = CreateObject("WScript.Shell")
 entry = oShell.RegRead(Key)
 If Err.Number <> 0 Then
   Err.Clear
   RegKeyExists = False
 Else
   Err.Clear
   RegKeyExists = True
 End If
End Function

but how/where do I implement this into the existing script?

Posted

That function will either return true ( for it exists ) or false for when it does not so you could do an if statement on the function , I would personally assign each registry key to a constant or a string ie

 

Const RegOne = "HKCU\Software\....."

Const RegTwo = "HKCU\Software\Microsoft\...."

Const RegThree etc

 

If RegKeyExists(RegOne) = "True" Then

'do what you need if the key exists

else

'do what you need if it does not exist

End If

 

Steve21 has posted a script above with ref to the registry side already

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...