CJSmith dot me

I dump stuff I find useful here

Category: Windows Server 2012

Remotely remove a broken user profile from another computers registry when the user folder is deleted

Published / by Chris Smith / Leave a Comment

I recently had an issue where someone had deleted my admin user profile from a remote server by deleting its folder.
The logon then failed as Windows still expected the folder to be there, as obviously the registry entries were not removed.
Rather than manually checking HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList for the SID and then removing it WMIC can be used. A user called Tommylotmanagement posted an answer and I found it worked if the user folder profile has been deleted as well.

wmic /node:<Server> path win32_UserProfile where LocalPath="c:\\users\\<ADUsername>" Delete 2>>c:\windows\temp\wmic.err

It should then show:
Deleting instance \\<Server>\root\cimv2:Win32_UserProfile.SID=”<SID>”
Instance deletion successful

If it fails check the file c:\windows\temp\wmic.err in a text editor.

Credit: Tommylotmanagement

I have also found this has worked to correct issues where accounts keep generating TEMP or TEMP. folders.

Display advanced properties of scheduled tasks in command line

Published / by Chris Smith / Leave a Comment

The following command requests a detailed display of the tasks on the local computer. It uses the /v parameter to request a detailed (verbose) display and the /fo LIST parameter to format the display as a list for easy reading. You can use this command to verify that a task you created has the intended recurrence pattern.
Before running increase the command prompts screen buffer (Right click the Window, go to properties, go to Layout and set the Screen Buffer Height to 9999). If this is not done you will find due to the amount of Windows scheduled tasks, that the tasks you want are cleared from thje screen buffer.

schtasks /query /fo LIST /v

SchTasks.exe displays a detailed property list for all tasks. I normally will then copy everything into the clipboard (Ctrl + A) and pout it into a text editor, I then delete anything after “Folder: \Microsoft” so I then only have my list of scheduled tasks.

Credit: Technet

Updating a network location in Windows 2012

Published / by Chris Smith / Leave a Comment

I had an issue where a server which was joined to a domain had incorrectly set its network as Public.
A TechNet article and TenForum article provided the answer (As it states Windows 10 it also applies to Server 2012) so I have placed credits above the steps I followed.
Following a re-occurrence I have found my steps below didn’t work for me a second time, so this post now has a second way of performing this with PowerShell thanks to a blog post from Clint Boessen.

MS Technet

Press [Windows Key] + [R] together at the same time, the Windows Run prompt should appear. If it hasn’t appeared press Windows Key and type Run, a Run desktop program should appear in the list
Type “gpedit.msc” and press enter
If this also fails try and launch it via the Microsoft Management Console
Local Group Policy Editor should appear
Expand Computer Configuration –> Windows Settings –> Security Setting –> Network List Manager Policies
Right click “Network List Manager Policies” and click Show All Networks
Choose your affected Network name from the right pane (If you are unsure check its name in Control Panel –> Network and Sharing Centre)
Go to Network Location tab and change the Location type from Public to Private
Close Local Group Policy Editor
Restart the computer or server for it to take effect.

Clint Boessen’s method actually worked much better for me and also didn’t need a machine reboot.

Open up Command Prompt as Administrator
Type “PowerShell” and press enter
Get a list of interfaces by using the command:
You will want the InterfaceIndex as I prefer to specify exactly the interface to be modified (Required when you have more than one network cable attached)
To modify the interface you require use the command in Powershell, however swap out and choose whether you want Private or Public set:
“Get-NetConnectionProfile ¦ Set-NetConnectionProfile -InterfaceIndex -NetworkCategory

Filtering in Event Viewer Windows Server 2008 onwards

Published / by Chris Smith / Leave a Comment

The Event Viewer from Server 2008 onwards is XML based.
Filters based on XML syntax can be used such as:

  <Query Id="0" Path="System">
    <Select Path="System"> 
                 *[EventData[Data and (Data='<Search Parameter>')]] 
  <Query Id="0" Path="Application">
    <Select Path="Application">
                 *[EventData[Data and (Data='<Search Parameter>')]] 

You can search for anything which appears in the data section such as service names, error messages etc..

- <EventData>
  <Data Name="param1">Service Display Name</Data> 
  <Data Name="param2">Details</Data> 
  <Data Name="param3">Details</Data> 
  <Data Name="param4">Service Name</Data> 

MS Technet

Within a batch file run a program for a certain length of time before killing it

Published / by Chris Smith / Leave a Comment

I needed to run a scheduled batch job and kill it off after a certain amount of time, this was because the program would run what it needed and then run as normal (If it exists then there is no reason to use start or timeout). As it actually ran as a service I didn’t want to leave it running in this way.
As I use Taskkill on the programs exe name, obviously don’t use it if it will be run on a server where it is running other executables with the same program name.
I replaced timeout with ping as the process launched fully utilises the CPU cores.

ECHO Running the program with start allows the batch script to move on to the next line
start "DBCapture" "d:\myprogram.exe" -runparameter1 -runparameter2
ECHO Waiting 120 seconds before killing off the task
ECHO Credit For lower CPU usage, instead of using WAIT use Ping as suggested on http://ss64.com/nt/timeout.html
PING -n 121>nul
ECHO Kill the program
Taskkill /IM "myprogram.exe" /F

Credits: I replaced my use of timeout based on a suggestion on SS64.com

Compress and move existing Windows 2012 and Microsoft SQL Server 2008 Express backups

Published / by Chris Smith / Leave a Comment

The following has been used to backup a server with Cisco Cubac on. The software runs on Windows 2012 machine and uses SQL Server 2008 Express.
As its not on a managed server I had to find a way to backup the server and the database, plus keep the backup as small as possible.
I get the Windows backup tool to backup once a day and save it to D:\WindowsImageBackup\, I then get the script below to run which then backs up SQL Server 2008 Express separately. I then get the backups compressed with 7zip and move them to a network storage device.
The destination NAS required a username and password, I had to put in a username and password (Not recommended although I believe SAMBA may not have been configured correctly).
If the server was on the same domain as the backup destination then get rid of “/user:username Password” from the net use line, normally having an account with the same username and password should work.

This batch requires RoboCopy (I believe is installed with the OS), 7Zip, a destination device and lots of disc space to fit the backups.

This has been used on a server with Windows 2012 and Microsoft SQL Server 2008 Express to backup a Cisco Cubac install.
The script will call SqlBackup.sql to tell SQL to backup the databases (As it installed with SQL Server 2008 Express).
Everything surrounded by <> needs to be changed, anything calling a directory needs to be checked.
%hostname% is not used for ServerHostName variable, its used for the SQL instance name (Which normally is the hostname).

echo Script runs a manual backup of SQL before moving it to the NAS.
echo Additional files such as backups by Windows are also copied across, but these must be generated by Windows Server Backup.
echo Setting up Variables
Set FileDate=%date:~-10,2%%date:~-7,2%%date:~-4,4%
rem change to SQL instance name
Set ServerHostName=
rem change backup destination
Set NasPath=\\BackupDestination\Cisco_Cubac\
Set Nasoldfiles=%NasPath%\OldFiles\
rem Set where the script runs from
Set BackupScript=C:\Wherethescriptrunsfrom\
Set Sqllocal=
rem Change username and password as required
net use %NasPath% /user:username Password /persistent:no > %BackupScript%logs\%filedate%-MapDrive.log
mkdir %BackupScript%logs
mkdir %BackupScript%7ztemp
echo Run SQL Backup and put output into log file and append with date %FileDate%
sqlcmd -S %ServerHostName% -i %BackupScript%SqlBackup.sql > %BackupScript%logs\%filedate%-SqlBackup.log
echo Compress and move the latest SQL backup to the NAS
"C:\Program Files\7-Zip\7z.exe" a -t7z %BackupScript%7ztemp\%filedate%-FileSystem.7z %Sqllocal%*.bak > %BackupScript%logs\%filedate%-SqlBackupCompress.log
Robocopy %BackupScript%7ztemp\ %NasPath%Sqlbackup *.7z /E /MOVE /V /R:3 /W:5 > %BackupScript%logs\%filedate%-MoveSQLBackup.log
del %Sqllocal%*.* /Q > %BackupScript%logs\%filedate%-DeleteSQLBackup.log
echo Compress and move Windows backup from drive to NAS and remove old files
mkdir D:\7ztemp
rem Change "D:\WindowsImageBackup\" to where the Windows backup is located
"C:\Program Files\7-Zip\7z.exe" a -t7z D:\7ztemp\%filedate%-ServerBackup.7z "D:\WindowsImageBackup\" > %BackupScript%logs\%filedate%-WinBackupCompress.log
Robocopy D:\7ztemp %NasPath%Winbackup\%filedate%\ *.* /E /MOVE /V /R:3 /W:5 > %BackupScript%logs\%filedate%-MoveWinBackup.log
rmdir "D:\WindowsImageBackup\" /S /Q >> %BackupScript%logs\%filedate%-DeleteWinBackup.log
mkdir "D:\WindowsImageBackup\" >> %BackupScript%logs\%filedate%-DeleteWinBackup.log
echo Remove old Files on NAS by using Robocopy to copy files over a certain age into a specific folder
mkdir %Nasoldfiles%
ROBOCOPY %NasPath%Sqlbackup %Nasoldfiles% /move /minage:7 >> %BackupScript%logs\%filedate%-NasRemoveOldFiles.log
ROBOCOPY %NasPath%Winbackup %Nasoldfiles% /E /move /minage:7 >> %BackupScript%logs\%filedate%-NasRemoveOldFiles.log
ROBOCOPY %NasPath%Backuplogs %Nasoldfiles% /move /minage:7 >> %BackupScript%logs\%filedate%-NasRemoveOldFiles.log
del %Nasoldfiles%*.* /Q > %BackupScript%logs\%filedate%-NasDeleteOldFiles.log
echo Move Backupscript logs to NAS to complete job
Robocopy %BackupScript%logs\ %NasPath%Backuplogs\ *.* /E /MOVE /V /R:3 /W:5

This second file (SQL Backup File for Microsoft SQL Server 2008 Express) is called separately, above I have called it SqlBackup.sql in the folder specified in variable BackupScript.

BACKUP DATABASE [master] TO  [FileSystem] WITH NOFORMAT, NOINIT,  NAME = N'master-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
BACKUP DATABASE [model] TO  [FileSystem] WITH NOFORMAT, NOINIT,  NAME = N'model-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
BACKUP DATABASE [msdb] TO  [FileSystem] WITH NOFORMAT, NOINIT,  NAME = N'msdb-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10