creating vbscript to change defualt wallpaper on win7home machines - vbscript

Need help making this script true. i beleave the first part is done file, but registry needs to be reflected from information 4-8 thank you.
1 'this vbscript should be named DefualtWallpaper.vbs
2 'copy this file to a folder.
3 'cat.jpg image should be in same folder as vbscript.
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("%cd%\cat.jpg%") Then
filesys.CopyFile "%cd%\cat.jpg%","%windir%\web\wallpaper\windows"
4.'change registry to new file
Set WShellObj=createobject("WScriptShell")
WShellObject.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\DesktopBackground","%WINDIR%\WINDOWS\Web\Wallpaper\Windows\cat.bmp","REG_EXPAND_SZ"
Set WShellObj=nothing
i get a error at line 18 char 54 error expected 'end' code 800A03f6 from microsft VBScript combilation error. on a win 7 home 64bit,
Key name: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes
keytype: REG_EXPAND_SZ
key: DesktopBackground
Data: %WINDIR%\WINDOWS\Web\Wallpaper\Windows\cat.bmp

Try this one from spicework forum:
reg.exe load HKU\DefUser "C:\Documents and Settings\Default User\ntuser.dat"
reg.exe add "HKU\DefUser\Control Panel\Desktop" /v Wallpaper /d "c:\windows\wallpaper\desktop.bmp" /f
reg.exe unload HKU\DefUser
The link is here:
http://community.spiceworks.com/scripts/show/327-batch-file-to-set-the-default-wallpaper

This VBScript for:
1-Run script as administrator
2-Copy my image to the directory where the background image saved
3-Change the current desk background image to my image From registry
4-Force Log off the current user to apply the change
'run the script as Administrator
If WScript.Arguments.Length=0 Then
CreateObject("Shell.Application").ShellExecute "wscript.exe",Chr(34)&WScript.ScriptFullName&Chr(34)&" Admin",Null,"runas",1
WScript.Quit
End If
'copy the image from to
Dim WShellObj : Set WShellObj=createobject("WScript.Shell")
Dim filesys : set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists(WShellObj.ExpandEnvironmentStrings("%cd%")&"\cat.jpg") Then
WShellObj.Run "cmd.exe /c copy ""%cd%\cat.jpg"" %windir%\web\wallpaper\windows",0,False
End If
'change desktop wallpaper through registry
WShellObj.RegWrite "HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper","%WINDIR%\Web\Wallpaper\Windows\cat.jpg","REG_SZ"
'force log off to apply the change of wallpaper
Dim oSystem
For Each oSystem In GetObject ("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem")
oSystem.Win32Shutdown 4
Next

Related

How to use CMD to backup files to .zip for archive?

So this one's a multi-parter (Windows 7).
I have a folder structure as so:
C:
SyncFolder
Backups
[User]
FolderA
Subfolder1
Subfolder2
FolderB
Subfolder3
My aim is to use the folder 'SyncFolder' as a backup system for certain files (Sync folder is actually a folder placed there by a file syncing service).
At present, I have the following within the Sync folder:
a .bat file containing a ROBOCOPY command (which copies 1 file)
a .vbs file which is used to call the .bat file (to avoid the CMD window appearing). This VBS file is being called once per hour by Windows Task Scheduler
the file which is being copied
So here are my questions:
I'm looking for a code (preferably an edit to the existing .bat, as I'm not overly-technical) which can:
Copy Subfolder1 in its entirety into a .zip file, which is named YYYYMMDDHHMM_SubFolder1_Backup (where the date & time is automatically populated)
Move the newly-created .zip file to SyncFolder\Backups (or create it there in the first place?)
Deletes (or overwrites?) the previous backup
Repeats for each Subfolder specified (perhaps as an additional command line?) -- I'm not expecting the commands to identify the folders. I would specify the folders myself
Logs the details of the backup to a .log file located in SyncFolder (i.e. Name of .zip created, Date&Time of backup, size of .zip created)
I'm aware this might be a bit ambitious for CMD or a .bat, but I'm open to any suggestions, but please do bear in mind that I'm not highly technical, so any walk-throughs would be immensely appreciated!
Edit:
Here is my attempt with the .bat code:
#echo off
robocopy "C:\[USER]\[FolderA]\[SubFolder1]" "C:[SyncFolder]\Backups\BackupTest1.zip" *.* /dcopy:T /r:5 /w:5 /log+:"C:\[SyncFolder]\CopyLog.log" /bytes /ts /np
... I've tried this, without luck. This command works, but only creates a folder named BackupTest1.zip (which isn't actually a .zip).
Create a file using notepad, let's save it as ZipCMD.vbs then add the following code.
Set objArgs = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
InputFolder = FS.GetAbsolutePathName(objArgs(0))
ZipFile = FS.GetAbsolutePathName(objArgs(1))
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere(source)
wScript.Sleep 3000
Now you can run from cmdline
ZipCMD.vbs "C:\Frank\Folder A\Sub Folder 1" "C:\SyncFolder\Backups\BackupTest1.zip"
if you want to manually zip anything from cmd line.
If you want to schedule it, you can either just add the VBS file, or write a mybackup.cmd file and put the full string below in it and schedule that instead.
mybackup.cmd
ZipCMD.vbs "C:\Frank\Folder A\Sub Folder 1" "C:\SyncFolder\Backups\BackupTest1.zip"
Okay, so building on Gerhard's answer, I've got this:
In C:\SyncFolder, create ZipCMD.vbs using the following code in Notepad
Set objArgs = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
InputFolder = FS.GetAbsolutePathName(objArgs(0))
ZipFile = FS.GetAbsolutePathName(objArgs(1))
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere(source)
wScript.Sleep 3000
I've also created a Backups.bat in C:\Syncfolder using the following code in Notepad
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%%MM%%DD%%HH%%Min%
rem parameters set for use later
Del "C:\SyncFolder\*_BackupTest1.zip"
rem This line removes the old backup first (Not ideal. Open to suggestions)
ZipCMD.vbs "C:\Users\[USER]\[FolderA]\[SubFolder1]" "C:\[SyncFolder]\BackupTest1.zip"
rem This line copies files and folders from the Source to the Destination into a named .zip, without a date.
copy "C:\[SyncFolder]\BackupTest1.zip" "C:\[SyncFolder]\%stamp%_BackupTest1.zip"
rem This line creates a copy of the un-dated file, but appends a Date and Time from the parameters defined at the start of the file.
Del "C:\[SyncFolder]\BackupTest1.zip"
rem This line deletes the un-dated file, leaving only the current, dated backup.
Set File="*_BackupTest1.zip"
FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA
rem These lines define a new parameter defined by the .zip's size
Echo %stamp%_BackupTest1.zip %Size% >>BackupLog.log
rem This last line appends a line to a log file containing the name of the .zip created (which contains its' date) and its' size.
The second section of the above .bat (after defining initial parameters) can be repeated for multiple folders.
Lastly, I create a RunBackups.vbs in C:\SyncFolder using the following code in Notepad
Set WshShell = CreateObject("WScript.Shell" )
WshShell.Run chr(34) & "C:\[SyncFolder]\Backups.bat" & Chr(34), 0
Set WshShell = Nothing
This runs the .bat without having the black CMD window appear (i.e. running in 'silent' mode), and is the file I call upon using Task Scheduler to automate the backup process.
Unfortunately, there are still some issues with this:
In the ZipCMD.vbs file, the wScript.Sleep 3000 line causes issues on any .zip files which would take longer than 3 seconds to create. This could be extended, but having it hard-coded may prove troublesome. Open to suggestions as to how to get around this?
In the Backups.bat file, the old backup is deleted prior to the creation of the new backup. This is because I use *_TestBackup1.zip to identify the old backup, which, if used after the backup's creation, would delete both new and old backups. Perhaps moving the first Del command to immediately after the first ZipCMD.vbs command would help, as a backup would always exist?

Disable/Enable Taskmanager with VBS

Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.Run "calc" ,,true
msgbox "Calc finished"
WSHShell.Run "userinit.exe"
I need Taskmanager disabled from the beginning of the script until userinit.exe gets executed. I'm using Win 7 and i don't know how to do it. (I searched a lot but nothing worked)
You can set a registry value, using the registry editor cmdline equivalent: reg.exe. Here's an example:
WSHShell.Run("Reg.exe add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /t REG_DWORD /d 1 /f", 0, 1)
In order to enable it, reset the same registry value: /d 0.
Details can be found in [TweakAndTrick]: Enable Task Manager disabled by Administrator or Virus in Windows.
Note that depending on your user, some registry key permissions adjustments might be necessary.

Is is possible to have RUN AS prompt for vbscript?

I have this vbscript which changes the registry values and i want it to be able to run as another account(with admin rights) in a standard windows user account. Is it possible to code it such that when u double click on the vbscript, it will ask for yr windows account name and password then u be able to run the script with that account rights?
Thanks!
In the old days you could add a runas key to the respective file type in the registry:
reg add "HKCR\VBSFile\Shell\runas\Command" /ve /t REG_EXPAND_SZ ^
/d "\"%"SystemRoot"%\system32\wscript.exe\" \"%1\" %*" /f
which would add a Run as… entry to the context menu that would prompt you for credentials.
Unfortunately, Microsoft changed the "runas" behavior when they introduced UAC. Now the registry key adds a Run as Administrator entry to the context menu, that will work only with UAC enabled.
Sysinternals to the rescue (as always): you can re-enable the context menu entry for running as a different user with ShellRunas. Download the archive, unzip the executable to a directory in your %PATH% and run ShellRunas.exe /reg to register the program. That will add a Run as different user… context menu entry for executables only, though. To add this entry for VBScript files as well you need to add the relevant registry keys/values yourself, e.g. like this:
reg add "HKCR\VBSFile\Shell\runasuser" /ve /t REG_SZ /d "#shell32.dll,-50944" /f
reg add "HKCR\VBSFile\Shell\runasuser\command" /v DelegateExecute /t REG_SZ ^
/d "{ea72d00e-4960-42fa-ba92-7792a7944c1d}" /f
or by merging a .reg file like this:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\VBSFile\Shell\runasuser]
#="#shell32.dll,-50944"
[HKEY_CLASSES_ROOT\VBSFile\Shell\runasuser\command]
"DelegateExecute"="{ea72d00e-4960-42fa-ba92-7792a7944c1d}"
There is no elegant way for incorporating this in a VBScript, though. If your system has UAC enabled, you could check if your user already has admin privileges (verification method adopted from here) and otherwise re-launch the script using the ShellExecute method with the "runas" verb:
Set reg = GetObject("winmgmts://./root/default:StdRegProv")
rc = reg.GetStringValue(&h80000003, "S-1-5-19\Environment", "TEMP", val)
If rc = 5 Then
'return code 5 == access denied
're-launch script only when it was run without arguments, so we don't go
'in circles when admin privileges can't be acquired
If WScript.Arguments.Count = 0 Then
're-launch as administrator; the additional argument is a guard to make
'sure the script is re-launched only once
CreateObject("Shell.Application").ShellExecute "wscript.exe" _
, Chr(34) & WScript.ScriptFullName & Chr(34) & " relaunch", "", "runas", 1
WScript.Quit 0
Else
WScript.Echo "Cannot acquire admin privileges."
WScript.Quit 1
End If
Else
'your code here
End If
With UAC disabled, you'd need to prompt for credentials via InputBox (bad) or with a custom password dialog (better). Either way, you'd need to re-launch the script via runas.exe
Set sh = CreateObject("WScript.Shell")
sh.Run "runas /user:" & username & " cscript """ & WScript.ScriptFullName & """"
and type in the password via SendKeys (which is a bad idea in its own right).

unhandled exception of type 'System.Security.SecurityException'

First time poster long time reader. I want to say thank you guys for all of your work. I have been working on a VB project, currently we have some tools written in dos however its dated and currently just doesn't work for &%^$. What I have in batch is this.
if exist %userprofile%\Local GOTO W7
RD /s /q "%userprofile%\Local Settings\Application Data\Our Company Folder"
RD /s /q "%userprofile%\AppData\Local\Our Company Folder"
What this does is delete our company config folder when corruption has occurred.
Since we have a support tool created in VB I want to change this command to VB.
This is what I have now is
Dim fso
Dim wshshell As Object
Dim USRPROFILE
fso = CreateObject("scripting.filesystemobject")
wshshell = CreateObject("wscript.shell")
USRPROFILE = wshshell.expandenvironmentstrings("%APPDATA%")
If fso.FolderExists(USRPROFILE & "\Our Company Folder") Then
fso.DeleteFolder(USRPROFILE & "\Our Company Folder")
End If
USRPROFILE = wshshell.expandenvironmentstrings("%HOMEPATH%")
If fso.FolderExists(USRPROFILE & "\Local Settings\Application Data\Our Company Folder") Then
fso.DeleteFolder(USRPROFILE & "\Local Settings\Application Data\Our Company Folder")
End If
End Sub
However I am getting this error message.
An unhandled exception of type 'System.Security.SecurityException' occurred in Microsoft.VisualBasic.dll
Additional information: Exception from HRESULT: 0x800A0046 (CTL_E_PERMISSIONDENIED)
What I would read that message as is that the user running the VB code doesn't have permission to delete the folder.
This is that whomever you are executing the program as doesn't have correct permissions, so you have a couple of options.
Change the folder permissions
Log on as someone with permissions
Or run the process as someone else as suggested in the link below:
VB.Net - execute vbscript using process with 'run as administrator'

VBScript saying "No Application is associated with the specified file for this operation"?

I'm writing a simple VBScript to write to a custom windows event log using eventcreate.
FOR I = 0 to 5
Set WshShell = WScript.CreateObject("WScript.Shell")
strCommand = "eventcreate /l Application /t Information /so Test-Log /id 66 /d TEST"
WshShell.Run strCommand
Next
However, whenever I try to run it through the command prompt, I get the following message:
C:\testlog.vbs(6, 5) (null): No application is associated with the specified file for this operation.
From what I can tell, I'm doing exactly what the online examples are telling me to do, I just can't seem to replicate it. What am I doing wrong?
I ran your script and it worked as expected on a win 7 laptop. Ensure you are running the script with admin rights. I changed the script a little, I moved the Set statement out for the For...Next loop. There is no need to continue to set the WshShell object on each loop, setting it once for the entire script is fine in this instance.
Dim WshShell, strCommand
Set WshShell = WScript.CreateObject("WScript.Shell")
For I = 0 to 5
strCommand = "eventcreate /l Application /t Information /so Test-Log /id 66 /d TEST"
WshShell.Run strCommand
Next

Resources