Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBScript Windows 7 / Windows Vista

35 views
Skip to first unread message

Anthony Humphreys

unread,
Jul 22, 2009, 9:29:41 PM7/22/09
to
I am trying to write a script that will copy files to the Windows
Directory (Company Background Images)
This is to be deployed and run via group policy as a machine (Startup)
Script.

However, I believe this is the reason, the files / folders are not
being created as required because Vista / Windows 7 protects the C:
\Windows Directory (and Program Files Directory as well)

Has anyone here have a work around for this?
THe script works fine if I run it as Administrator after I have logged
in.
The Script is copying files from the SBS Server via full FQDN UNC
Path.

Any help in narrowing this down would be greatly appreciated.

Pegasus [MVP]

unread,
Jul 23, 2009, 3:18:53 AM7/23/09
to

"Anthony Humphreys" <ahumphr...@gmail.com> wrote in message
news:2a03ee6f-ba3a-4003...@t11g2000prh.googlegroups.com...

If your script works fine while you're logged on as Administrator then
you're dealing with a permissions issue, not a scripting issue. Your first
step should be to capture the error messages, either from within your script
(which you did not post) or by including a humble "copy" command in your
group policy startup batch file like so:

xcopy /s /d /c "%SystemRoot%\Some Folder\*.jpg"
\\SomeServer\SomeShare\SomeFolder\ 1>>c:\test.txt 2>>&1

Once you know what the security issue is you can repost your question in an
SBS newsgroup. Remember that no amount of scripting will get you around
security issues.


Anthony Humphreys

unread,
Jul 23, 2009, 3:25:51 AM7/23/09
to
Dim objFSO
Dim objShell
Dim objSourceFolder
Dim intSourceFileCount
Dim intFileCount

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

objITXLogoDirectory = objShell.ExpandEnvironmentStrings("%WinDir%") &
"\system32\oobe\info\backgrounds"
objITXLogoSource = objShell.ExpandEnvironmentStrings("%LogonServer%")
& "\sysvol\<domain name>\scripts\ITX_Backgrounds"

'<domain name> is an actual value, has been replaced with a variable.

Set objSourceFolder = objFSO.GetFolder(objITXLogoSource)

For Each objFile In objSourceFolder.Files
intSourceFileCount = intSourceFileCount + 1
Next

With objFSO
If .FolderExists(objITXLogoDirectory) = True Then
'Folder Exists - Let's check if the files Exist
Set objDestinationFolder = objFSO.GetFolder(objITXLogoDirectory)
For Each objFile In objDestinationFolder.Files
intFileCount = intFileCount + 1
Next
If intFileCount <> intSourceFileCount Then
'Remove the Files from the Directory
For Each objFile In objDestinationFolder.Files
objFile.Delete
Next

'Replace the Files in the directory
For Each objFile In objSourceFolder.Files
WScript.Echo objFile & " to " & objITXLogoDirectory & "\" &
objFile.Name
objFSO.CopyFile objFile, objITXLogoDirectory & "\" & objFile.Name
Next
End IF
Else
'Folder doesn't exist, So Create Folder, then Copy Images
'Need to check each level of the directory Structure
If .FolderExists(objShell.ExpandEnvironmentStrings("%Windir%") &
"\system32\oobe") = False Then objFSO.CreateFolder
(objShell.ExpandEnvironmentStrings("%Windir%") & "\system32\oobe\")
If .FolderExists(objShell.ExpandEnvironmentStrings("%Windir%") &
"\system32\oobe\info") = False Then objFSO.CreateFolder
(objShell.ExpandEnvironmentStrings("%Windir%") & "\system32\oobe\info
\")
If .FolderExists(objShell.ExpandEnvironmentStrings("%Windir%") &
"\system32\oobe\info\backgrounds") = False Then objFSO.CreateFolder
(objShell.ExpandEnvironmentStrings("%Windir%") & "\system32\oobe\info
\backgrounds\")
Set objSourceFolder = objFSO.GetFolder(objITXLogoSource)
For Each objFile In objSourceFolder.Files
Wscript.echo objFile
objFSO.CopyFile objITXLogoSource & "\" & objFile,
objITXLogoDirectory & "\" & objFile.Name
Next
End If
End With

With objShell
.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion
\Authentication\LogonUI\Background", 1, "REG_DWORD"
End With

'Set the correct Registry Value if it hasn't been set by the Login
Script yet
'HKLM\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI
\Background

Pegasus [MVP]

unread,
Jul 23, 2009, 4:32:49 AM7/23/09
to

"Anthony Humphreys" <ahumphr...@gmail.com> wrote in message
news:dbe16a43-54a4-495c...@b25g2000prb.googlegroups.com...

Here are a few points about your issue:

- If there is a possibility of your script encountering a problem then you
must include code to trap the problem, e.g. like so:
On Error Resume Next
Err.Clear
oFSO.CopyFile oFile, "e:\system volume information\test.txt"
if err.number > 0 then WScript.Echo Err.Description
On Error Goto 0

- It looks like you have a spelling problem in your code. The line
%Windir%\system32\oobe\info \
should probably read
%Windir%\system32\oobe\info\
(extra space after "info")

- You can increase the readability of your VB Script a lot by indenting your
code so that its structures are clearly visible.

- What you're trying to do is really very simple: Copy some files from here
to there if the file counts of two folders do not agree. This task can be
performed with a few lines of batch file code because batch files are made
precisely for this purpose. In VB Script this gets very chatty. The batch
file below (untested) performs the same task as your VB Script file, with
error testing included, yet is less than a third of the size of the VB
Script and a lot more readable. As an example: To copy all files from one
folder to another you need to write this in VB Script:

Set objSourceFolder = objFSO.GetFolder(objITXLogoSource)
For Each objFile In objSourceFolder.Files

objFSO.CopyFile objITXLogoSource & "\" & objFile, objITXLogoDirectory & "\"
& objFile.Name
Next

In a batch file this code becomes:

xcopy /y "ITXLogoSource\*.*" "ITXLogoDirectory"

[01] @echo off
[02] set objITXLogoDirectory=%WinDir%\system32\oobe\info\backgrounds
[03] set
objITXLogoSource=%LogonServer%\sysvol\MyDomain\scripts\ITX_Backgrounds
[04] set IntFileCount=0
[05] for %%a in ('dir /a-d "%ObjITXLogoSource%" ^| find /c ":"') do set
SourceFileCount=%%a
[06] if exist %objITXLogoDirectory% for %%a in ('dir /a-d
"%objITXLogoDirectory%" ^| find /c ":"') do set IntFileCount=%%a
[07] if not %SourceFileCount% EQU %IntFileCount% (
[08] del /q "%objITXLogoDirectory%\*.*"
[09] ) else (
[10] If not exist %Windir%\system32\oobe\info\backgrounds md
%Windir%\system32\oobe\info\backgrounds
[11] )
[12] xcopy /d /c "%objITXLogoSource%" "%objITXLogoDirectory%" 1>>c:\test.txt
2>>&1
[13] echo > "%Temp%\temp.reg" REGEDIT4
[14] echo >>"%Temp%\temp.reg"
[HKey_Local_Machine\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI]
[15] echo >>"%Temp%\temp.reg" "Background"=dword:1
[16] regedit /s "%Temp%\temp.reg"


Anthony Humphreys

unread,
Jul 23, 2009, 5:18:02 AM7/23/09
to
Thanks for the tip,

I will try the Bat file now,

in my VBScript the files where indented for readability, they just
didn't come across very well. (either that or I had word wrapping
turned on when I copied them)
re: The spelling mistake, I checked the source and this spelling
mistake did not exist.

I ended up reducing the script to just the following

'==========================================================================

Dim objFSO
Dim objShell
Dim objSourceFolder
Dim intSourceFileCount
Dim intFileCount

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

objITXLogoDirectory = objShell.ExpandEnvironmentStrings("%WinDir%") &
"\system32\oobe\info\backgrounds"
objITXLogoSource = objShell.ExpandEnvironmentStrings("%LogonServer%")

& "\netlogon\ITX_Backgrounds"
Set objSourceFolder = objFSO.GetFolder(objITXLogoSource)

For Each objFile In objSourceFolder.Files
intSourceFileCount = intSourceFileCount + 1
Next

With objFSO
If .FolderExists(objITXLogoDirectory) = False Then


'Folder doesn't exist, So Create Folder, then Copy Images
'Need to check each level of the directory Structure
If .FolderExists(objShell.ExpandEnvironmentStrings("%Windir%") &
"\system32\oobe") = False Then objFSO.CreateFolder
(objShell.ExpandEnvironmentStrings("%Windir%") & "\system32\oobe\")
If .FolderExists(objShell.ExpandEnvironmentStrings("%Windir%") &
"\system32\oobe\info") = False Then objFSO.CreateFolder
(objShell.ExpandEnvironmentStrings("%Windir%") & "\system32\oobe\info
\")
If .FolderExists(objShell.ExpandEnvironmentStrings("%Windir%") &
"\system32\oobe\info\backgrounds") = False Then objFSO.CreateFolder
(objShell.ExpandEnvironmentStrings("%Windir%") & "\system32\oobe\info
\backgrounds\")
Set objSourceFolder = objFSO.GetFolder(objITXLogoSource)

End If
End With

strCommandtoRun = "xcopy " & objSourceFolder & "\*.* " &
objITXLogoDirectory & "\"
objShell.Run strCommandToRun
'Wscript.echo strCommandToRun

With objShell
.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion
\Authentication\LogonUI\Background", 1, "REG_DWORD"
End With

'Set the correct Registry Value if it hasn't been set by the Login
Script yet
'HKLM\Software\Microsoft\Windows\CurrentVersion\Authentication\LogonUI
\Background

'==========================================================================


and noticed that neither the folders where created, nor where the
registry entry made.
This was set as a startup script in Windows 2008 on a Windows 7
desktop.
- So if rules of GPO are true, the script should have access to HKLM
to make the registry modifications.
- I confirmed that the Domain Computers have read access to the
netlogon directory
- No Files are created in C:\Windows\System32\oobe\info nor in C:
\Windows\SysWOW64\oobe\ as I thought maybe WOW might be causing the
problem.
- Because it is a startup script, It is difficult for me to put any
debugging code in there (at least I think it is)


I did make some modifications to your BAT file, the contents are
below.

#
==========================================================================

@echo off
set objITXLogoDirectory=%WinDir%\system32\oobe\info\backgrounds\
set objITXLogoSource=%LogonServer%\NetLogon\ITX_Backgrounds\

If not exist %Windir%\system32\oobe\info\backgrounds md %Windir%
\system32\oobe\info\backgrounds

echo xcopy /d /c "%objITXLogoSource%\*.jpg" "%objITXLogoDirectory%" >>
c:\test.txt
xcopy /d /c "%objITXLogoSource%*.jpg" "%objITXLogoDirectory%" 1>>c:
\test.txt 2>>&1

echo > "%Temp%\temp.reg" REGEDIT4


echo >>"%Temp%\temp.reg" [HKey_Local_Machine\Software\Microsoft\Windows

\CurrentVersion\Authenticatio­n\LogonUI]


echo >>"%Temp%\temp.reg" "Background"=dword:1

regedit /s "%Temp%\temp.reg"

del "%temp%\temp.reg" /y

#
==========================================================================
The bat file seemed to work fine, however, as This is just a pre-
cursor to some of the things I plan to do via Login Script, I would
like to know why the VBScript failed.
Time to break it down into managable chunks I think :)

- What's the best way to output a Startup Script (VBScript) to a text
file?

Pegasus [MVP]

unread,
Jul 23, 2009, 5:26:57 AM7/23/09
to

"Anthony Humphreys" <ahumphr...@gmail.com> wrote in message
news:d44b271c-395e-4efe...@x25g2000prf.googlegroups.com...
Thanks for the tip,

<snip>

The bat file seemed to work fine, however, as This is just a pre-
cursor to some of the things I plan to do via Login Script, I would
like to know why the VBScript failed.
Time to break it down into managable chunks I think :)

- What's the best way to output a Startup Script (VBScript) to a text file?

I would emply two methods:

1. Redirection:
cscript //nologo c:\MyScript.vbs 1>>c:\test.txt 2>>&1

2. Setting milestones. Include a number of lines of the following form in
your code in strategic places:
wscript.echo Label 1
If appropriate then you could add some variables on the same line, e.g.
the current user's logon name.


Anthony Humphreys

unread,
Jul 23, 2009, 5:37:38 AM7/23/09
to
I understand you are busy, however, the script works when logged in as
a user, but not when deployed as a Startup Script.
Everything but the file copy takes place.

Anthony Humphreys

unread,
Jul 23, 2009, 5:45:06 AM7/23/09
to
What I meant to say, I understand you are busy and I appreciate your
time in helping me resolve this issue.

It is definantly a permissions issue to the source files that is
preventing this from working.
I tried to copy the files by name, and recieved file not found.
I checked the permissions on the folder \\server\netlogon
\ITX_Backgrounds and set it to everyone for sh*ts and giggles and I
still recieve the file not found error

I have two options now...
I could - Copy the file to the LocalSystem as part of Group Policy
Preferences, or work on this further.
The inability for me to access these files Authenticated as the System
to the Netlogon Directory will probably cause issues moving forward
when the same thing happens with other scripts (Files need to be on
the local system)

What are your thoughts on this? How do you think I should proceed
here?

Once again, I thank you for your time and effort in helping people
like me get to the bottom of these issues.

Anthony Humphreys

unread,
Jul 23, 2009, 11:48:59 AM7/23/09
to
So the root cause of the problem was that I was trying to use
%logonserver% in a startup script.
the environmental variable %logonserver% is only active once a user
logs in and not in a startup (Computer) context

I instead used \\ite.local\netlogon\ and this worked correctly.

Thank you for your assistance with this, it's had me stumped for to
long now.

Pegasus [MVP]

unread,
Jul 23, 2009, 12:39:18 PM7/23/09
to
Thanks for the feedback - glad you got it to work!

"Anthony Humphreys" <ahumphr...@gmail.com> wrote in message

news:cf69245f-5d30-4b96...@2g2000prl.googlegroups.com...

0 new messages