|
|
To my Son and respected Islamic Brothers & Sisters, Assalamu Alaykum (السلام عليكم) Alhamdulillah I’m fine with kind blessings of Almighty ALLAH Subhanahu Wa Ta’ala ( SWT ) and I hope that you and yours family will also be healthy, happy and fine. A most sacred, pious and spirtual month (Ramdhan Mubarak) is going to start after short time, I’ve written a Powershell script that shutdowns a remote computer. This Powershell script use WMI to shutdown the remote system. 1. Open notepad 2. Copy and paste below text to notepad 3. Save the file with .ps1 extension. # ---------- SCRIPT STARTS HERE-------------- #remote shutdown script #usage remoteshutdown.ps1 computername $objsystem = get-wmiobject -computername $args[0] -query "select * from win32_operatingsystem" $result = $objsystem.shutdown() if ($result.returnvalue -match "0") { write-host "$args[0] - shutdown command completed" -foregroundcolor green } else { write-host "$args[0] - unable to send shutdown command" -foregroundcolor red } # ---------- SCRIPT ENDSS HERE-------------- Looking for a free software inventory solution? This VBscript pulls HOST NAME, OS, Edition, SP, Vendor, Model, Service Tag, Total Memory, Number of Processors, PROCESSOR, PROCESSOR TYPE, HD0 Size,HD0 free space. It also outputs the data into a Excel spreadsheet. 1. Open notepad 2. Copy and paste below text to notepad 3. Save the file with .vbs extension. 4. To run the script, Example: cscript queryRemoteServers.vbs servername username password 5. Once you run the script, you will find the output data in output.csv of the script directory,
'------------ SCRIPT STARTS HERE-------------- '--------------------------------------------------------- 'script to query remote computers 'for device inventory purposes '--------------------------------------------------------- set args = Wscript.Arguments If args.Count <> 3 then Wscript.Echo "Usage: cscript queryRemoteServers.vbs server_name username password" Wscript.Quit 1 END IF strComputer = args(0) strUser = args(1) strPass = args(2) Set objFS = CreateObject("Scripting.FileSystemObject") Set objNewFile = objFS.openTextFile("output.csv",8,True) 'objNewFile.WriteLine "HOST NAME,OS,Edition,SP,Vendor,Model,Service Tag,Total Memory,Number of Processors, PROCESSOR,PROCESSOR TYPE,HD0 Size,HD0 freespace,HD1 Size,HD1 freespace" Set objLocator = CreateObject("WbemScripting.SwbemLocator") Set objSvc = objLocator.ConnectServer(strComputer, "root\cimv2", strUser, strPass) objNewFile.write strcomputer & "," Set colSWbemObjectSet = objSvc.InstancesOf("win32_operatingsystem") For Each objSWbemObject In colSWbemObjectSet objNewFile.Write objSWbemObject.caption & "," objNewFile.Write objSWbemObject.csdversion & "," Next Set colSWbemObjectSet = objSvc.InstancesOf("win32_computersystemproduct") For Each objSWbemObject In colSWbemObjectSet objNewFile.Write objSWbemObject.vendor & "," objNewFile.Write objSWbemObject.name & "," objNewFile.Write objSWbemObject.identifyingnumber & "," Next Set colSWbemObjectSet = objSvc.InstancesOf("Win32_computersystem") For Each objSWbemObject In colSWbemObjectSet objNewFile.Write objSWbemObject.totalphysicalmemory & "," objNewFile.Write objSWbemObject.numberofprocessors & "," Next Set colSWbemObjectSet = objSvc.ExecQuery("select * from Win32_processor where deviceID='CPU0'") For Each objSWbemObject In colSWbemObjectSet objNewFile.Write objSWbemObject.Name & "," objNewFile.Write objSWbemObject.caption & "," Next Set colSWbemObjectSet = objSvc.ExecQuery("select * from win32_logicaldisk where drivetype='3'") For Each objSWbemObject In colSWbemObjectSet objNewFile.Write objSWbemObject.size & "," objNewFile.Write objSWbemObject.freespace & "," Next objNewFile.WriteLine "" @echo off :: variables set drive=G:\Backup set backupcmd=xcopy /s /c /d /e /h /i /r /y echo ### Backing up My Documents... %backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents" echo ### Backing up Favorites... %backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites" echo ### Backing up email and address book (Outlook Express)... %backupcmd% "%USERPROFILE%\Application Data\Microsoft\Address Book" "%drive%\Address Book" %backupcmd% "%USERPROFILE%\Local Settings\Application Data\Identities" "%drive%\Outlook Express" echo ### Backing up email and contacts (MS Outlook)... %backupcmd% "%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook" "%drive%\Outlook" echo ### Backing up the Registry... if not exist "%drive%\Registry" mkdir "%drive%\Registry" if exist "%drive%\Registry\regbackup.reg" del "%drive%\Registry\regbackup.reg" regedit /e "%drive%\Registry\regbackup.reg" :: use below syntax to backup other directories... :: %backupcmd% "...source directory..." "%drive%\...destination dir..." echo Backup Complete! @pause The above example backs up "My Documents", Favorites, Outlook Express email/address book, (all for the current user) and the Windows Registry. It copies the files to the directory defined in the %drive% variable, or "g:\Backup". If the script is ran multiple times, it will only rewrite if the source files are newer. It will create subdirectories as necessary, and it will retain file attributes. It can copy system and hidden files. In the above file, all lines that begin with "::" are comments. The "set drive=" and "set backupcmd=" near the top define two variables (referenced by %drive% and %backupcmd%), used a number of times throughout the file; the first being the location of the top directory where we want to backup, and the second the actual copy command with all necessary switches. All the "echo " lines in the file simpy output the line of text to the screen, and the lines beginning with %backupcmd% are the actual commands to execute. Note that most of the folders in the above backup example are subdirectories of the %USERPROFILE%... It is possible to simply backup the entire user profile with My Documents, Favorites, Outlook Express, Outlook, etc. by backing up this one folder. Here is an example (it assumes the above "drive" and "backupcmd" variables are set): %backupcmd% "%USERPROFILE%" "%drive%\%UserName% - profile" Backing up Other Directories and networked PCs You can backup other directories by simply creating more alike lines: %backupcmd% "...source dir..." "%drive%\...destination dir..." For example, if you'd like to backup "C:\Program Files\Microsoft Office" to our destination "G:\Backup\MS Office" (and retain the directory structure) you'd need to add the following line to the batch file: %backupcmd% "C:\Program Files\Microsoft Office" "%drive%\MS Office" Here is another example, backing up the Administrator Profile on a machine on the LAN with computer name "Lianli": %backupcmd% "\\Lianli\c\Documents and Settings\Administrator" "%drive%\Lianli - admin profile" Remember, you have to save the batch file with either .bat or .cmd extension, then just double-click to execute it. Using the Current Date Sometimes it is useful to create folders with the date incorporated in the folder name. Here is how to set the variable folder to the current date (assuming US system date format): set folder=%date:~10,4%_%date:~4,2%_%date:~7,2% %backupcmd% "...source dir..." "%drive%\%folder%\...destination dir..." It is also possible to use the current time in the folder name. The following example with incorporate both the current date and time to the minute, separated by underscores. There is an extra step that cleans up possible spaces in single-digit hours in the system time: set hour=%time:~0,2% if "%hour:~0,1%"==" " set hour=0%time:~1,1% set folder=%date:~10,4%_%date:~4,2%_%date:~7,2%_%hour%_%time:~3,2% %backupcmd% "...source dir..." "%drive%\%folder%\...destination dir..." Example - dated directories In the example below, we first set 3 variables: drive, folder, and backupcmd. The "drive" variable defines the root directory of our backups. The "folder" takes the 2 digit day value from the current date (US date format, taking 2 digits from the date command output, starting at the 7th character), which we will use as a subdirectory. The third variable, "backupcmd" defines our backup command with the appropriate command line switches we want to use. @echo off :: variables set drive=D:\Backup set folder=%date:~7,2% set backupcmd=xcopy /s /c /d /e /h /i /r /k /y echo ### Backing up directory... %backupcmd% "C:\Program Files\somedirectory" "%drive%\%folder%" echo Backup Complete! @pause This example will backup the "C:\Program Files\somedirectory" folder to "D:\Backup\[dd]" where [dd] is the current day of the month. After a month, we will have 30ish daily copies of the backup... And, because of the xcopy command line switches chosen, following backups will only overwrite files that are newer, speeding up subsequent backups. Alternatively you can add a line to delete the %folder% directory prior to executing the %backupcmd% if you prefer to start clean (and take longer). Cleaning up It is usually a good idea to clean up temporary files, cookies, and history from the destination backup, as applicable. It is especially useful if you're backing up full, multiple user profiles and overwriting them periodically. Since temporary files and cookies change, your backed up directories will keep increasing with unnecessary files. To remedy this, the following code can be added to the backup script, or to a separate batch file. It will automatically search all subdirectories for "cookies", "temp" and "history", and then remove those directories: :: change to the destination drive first G: :: your parent backup directory drive=G:\Backup echo ### Searching for files to clean up... cd %drive% dir /s/b/ad \cookies > %drive%\cleanup.txt dir /s/b/ad \temp > %drive%\cleanup.txt dir /s/b/ad \history > %drive%\cleanup.txt echo ### Deleting cookies, temp files and history from backup dir for /f "delims=" %%J in (%drive%\cleanup.txt) do rd "%%J" /Q/S echo Cleanup complete @pause Note that you need to change to the destination drive, and the main backup directory before searching for files to delete. Any sub-folders that contain "cookies", "temp", or "history" will be deleted automatically. You can test to see what will be deleted by commenting out the "for /f ....." line (just add :: to the beginning of the line, or delete it from the batch file and add it again later). If that line is not present, the file will only list all files to be deleted in the cleaup.txt file, located in the destination directory (G:\Backup\cleanup.txt in the above example). If you add the cleanup portion to the end of your backup batch file, you may want to remove the "@pause" line at the end of the backup portion, so everything can execute without user interacion. Alternatively, there is a simpler one-line method of deleting specific subdirectories after backing up. The disadvantage of using this method is that you'd need another line for each separate directory to be removed... In other words, it doesn't work well when removing a large number of directories by similar names. Still, here is an example: rmdir /s /q "%drive%\%UserName%\Local Settings\Temporary Internet Files" A Microsoft Windows user profile describes the Windows configuration for a specific user, including the user's environment and preference settings. The user profile contains those settings and configuration options specific to the user, such as installed applications, desktop icons, and color options. This profile is built in part from System Policy information (for example, those things that a user has access to and those things that the user can and cannot change) and in part from permitted, saved changes that a user makes to customize the desktop. If you have administrative privileges, you can copy one user profile to another. To do this, follow the steps below for your operating system. Windows XP Note: The Windows XP default desktop view and Start menu are different from the Windows Classic View (e.g., in Windows 2000). Therefore, navigating to certain items can be different. In the interest of broad applicability, most Knowledge Base instructions assume you are using Classic View. For information about switching your Windows XP default view to Classic View, see In Windows XP, how do I switch to the Windows Classic View, Classic theme, or Classic Control Panel? - From the
Start menu, select Settings, then Control Panel. Double-click System. - Click the
Advanced tab, and then, under "UserProfile", click Settings. - Click the profile you want to copy and then click
Copy to. - In the
Copy To dialog box, click Browse to select the directory to which you want to copy the profile. This will usually be C:\winnt\profiles\username or C:\Documents and Settings\username, where username is the username of the profile to which you are copying. When you've selected the directory, click OK. - Under "Permitted to Use", click
Change. - In the field labeled "Enter the object name to select:", enter the username of the user who needs to have rights to view this profile. Click
Check Names to make sure that the user is found. If the user is not found, you may need to click Locations... to select the correct domain (or, if it is a local account, to select the computer name), and then click OK. - Click
OK twice. - If you are prompted to continue, click
Yes. Allow a minute for the system to copy the profile. - In the
User Profiles window, click OK, and then click OK again in the System Properties window. Windows NT and 2000 - From the
Start menu, select Settings, then Control Panel. Double-click System. - Select
User Profiles, and then click the profile you want to copy. Click Copy to. - In the
Copy To dialog box, click Browse to select the directory to which you want to copy the profile. This will usually be C:\winnt\profiles\username or C:\Documents and Settings\username, where username is the username of the profile to which you are copying. When you've selected the directory, click OK. - Click
Change... and select the user who will have permission to use the profile. For example, DOM1\janedoe will allow user janedoe on domain DOM1 to have access to the profile. You can search Microsoft's knowledge base at: www.http://support.microsoft.com/default.aspx - Click
OK three times. Windows 95 and 98 Make sure that you are not logged in under the profile that you want to copy to. - Double-click
My Computer. Go to the Windows\Profiles directory (usually at C:\Windows\Profiles). - Double-click the folder of the profile you want to copy.
- Select all of the folders and files in that profile by pressing
Ctrl-a . From the Edit menu, select Copy. - Go back to the
Profiles directory, and double-click the folder of the profile you want to copy to. - From the
Edit menu, select Paste. - Click
Yes to All to overwrite the old profile.
|
|
|
|