The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,861 other subscribers

Archive for the ‘Batch-Files’ Category

Easiest way to grant/query “Log on as a service” to a Windows user from the command-line? (my question on Super User)

Posted by jpluimers on 2014/04/28

I want to script an install where a service needs to be run as a user. I want to be able to specify the user.

Creating the user is easy through the [Wayback/Archive] NET USER /ADD command.

Specifying the user for the service can also be done: the [Wayback/ArchiveSC CONFIG command [Wayback/Archiveallows this (thanks [Wayback/Archive] wmz and [Wayback/Archive] ofiris).

Now the missing link: granting the user the [Wayback/Archive] “Log on as a service” privilege as a [Wayback/Archivelogon right (SeServiceLogonRight). Is there a command for this? Or a simple script for PowerShell?

(I know only Local Service can do this out of the box, and [Wayback/Archiveno other accounts by default are, but I want to have control over the account and what other privileges that account has).

Edit: solved. Thanks [Wayback/Archive] Mathias R. Jessen.

Here is the solution, including a few comments.

The easiest way to do this from a command line is definitely using NTRights.exe from the Windows Server 2003 Resource Toolkit.

ntrights +r SeServiceLogonRight -u jeroen -m \%COMPUTERNAME%

I changed the command-line a bit:

ntrights +r SeServiceLogonRight -u %USERNAME% -m \%COMPUTERNAME%

Note that

whoami /all

doesn’t show any change (not even after a reboot, it does not matter if you run it with or without UAC token).

secpol.msc

does show the change however, and does not require UAC (follow the tree to “Security Settings -> Local Policies -> User Rights Management -> Log on as a service” to see the users having the permission).

–jeroen

via: [Wayback/Archive] Easiest way to grant “Log on as a service” to a Windows user from the command-line? – Super User.

PS: Later I found out it is way easier to query the right:

accesschk.exe /accepteula -q -a SeServiceLogonRight

It will list the users having that right, for instance:

        IIS APPPOOLClassic .NET AppPool
        NT SERVICEALL SERVICES
        VCS-CIContinuaCI

There are similar rights one might want to query:

SeBatchLogonRight
SeDenyBatchLogonRight
SeInteractiveLogonRight
SeDenyInteractiveLogonRight
SeServiceLogonRight
SeDenyServiceLogonRight
SeNetworkLogonRight
SeDenyNetworkLogonRight

Thanks [Wayback/Archivetwasbrillig for explaining that at as answer to [Wayback/Archivepowershell – How to view user privileges using windows cmd? – Stack Overflow

Posted in Batch-Files, CommandLine, Development, Power User, PowerShell, Scripting, Software Development, Windows, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Vista | Leave a Comment »

Windows: setx sets environment variables in a persistent way (from values on cmd-line, registry or text files)

Posted by jpluimers on 2014/04/02

Wow, I totally missed the introduction of SETX.

From TechNet:

SETX:

Creates or modifies environment variables in the user or system environment, without requiring programming or scripting. The Setx command also retrieves the values of registry keys and writes them to text files.

Even better, is that it allows you take values from these sources so it is easy to get those into environment variables:

  • Command-line parameter
  • Registry key
  • Text file (with some filtering/search options)

From a bit of searching around, I think it got introduced in a Windows Resource Kit, and got included by default starting Windows Vista.

Excellent addition to my toolset (:

–jeroen

via Setx.

Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Vista | Leave a Comment »

Windows CSC reset: How to re-initialize the offline files cache and database (via MS Support)

Posted by jpluimers on 2014/03/27

Every once in a while, the local synchronization of offline files mismatches the actual files.

Time for a CSC reset.

Note: you need to be Local Administrator on the machin in order to reset the CSC cache.

Use Reg.exe

You can also automate the process of setting this registry value by using the Reg.exe command line editor. To do this, type the following command in the Reg.exe window:

REG.EXE. REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\NetCache" /v FormatDatabase /t REG_DWORD /d 1 /f

–jeroen

via: How to re-initialize the offline files cache and database.

Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows, Windows 7, Windows 8 | 2 Comments »

When GExperts for Delphi 2007 does not install itself in Delphi on Windows Vista/7/8

Posted by jpluimers on 2014/03/06

Like When DelphiSpeedup cannot register itself in Delphi on Windows Vista/7/8, GExperts also had problems installing on Windows Vista and up when Delphi runs in a non-elevated account.

It said it installed fine, but it didn’t get included when Delphi was started. The reason was that it got installed in the wrong root key.

What happened is that GExperts installed itself into the HKLM root:

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Borland\BDS\5.0\Experts]
GExperts=C:\Program Files (x86)\GExperts for Delphi 2007\GExpertsDelphi2007.dll

But it should have installed itself in the HKCU root: Read the rest of this entry »

Posted in Batch-Files, Delphi, Delphi 2007, Development, Scripting, Software Development | Leave a Comment »

Windows “FIND” console app: how to *not* have the dashed line with the filename in the output

Posted by jpluimers on 2014/03/06

When using the Windows FIND console application, it will insert a dashed line

C:\Users\Developer>echo Foo > %temp%\foo.txt
C:\Users\Developer>find "Foo" %temp%\foo.txt
---------- C:\USERS\DEVELOPER\APPDATA\LOCAL\TEMP\FOO.TXT
Foo

This is really annoying when using FIND to parse files, and redirect the output fur further processing.

But there is a way around it, as find will not emit the dashed line when it the input is not a file, but stdin: Read the rest of this entry »

Posted in Batch-Files, Development, Scripting, Software Development | 1 Comment »

Downloading https urls from the commandline through cURL for Windows

Posted by jpluimers on 2014/01/27

Lately I moved more and more away from wget, mainly because out of the box, wget (and also aria2, which I like for the bittorrent support) handle https downloads so badly: you need to manually setup your CA store on each and every installed system.

Not so with cURL, especially not on Windows any more, as “recently” (that is: since the last time I examined it, which is over a year ago now), there is a new kid in town: cURL for Windows: a Windows Installer for the Web Transfer Tool.

You don’t even need to download the installer. Grabbing the stuff from the bin directory in the zip download is enough: it contains a prepackaged CA certificate set that works splendid.

So now downloading https://dl.google.com/update2/installers/ChromeStandaloneSetup.exe to the current directory is as simple as Read the rest of this entry »

Posted in *nix, Batch-Files, Development, Power User, Scripting, Software Development, wget, Windows | Tagged: , | Leave a Comment »

Different ways of sleeping/waiting in batch files

Posted by jpluimers on 2013/12/13

About a year and a half ago, I wrote about a Batch file to “Keep Alive” a CMAK generated VPN connection in Windows 7.

It uses ping to wait a certain amount of time, but it has the drawbacks of

  • requiring TCP/IP to be installed (which some headless systems don’t).
  • using N+1 as the number of seconds

Since then, I learned that since Windows Vista and up has timeout command that just waits:

timeout /t 600 /nobreak

Two parameters are used:

  • /nobreak
    does not stop waiting when you press a key
  • /t #
    waits # seconds

(the example is 10 minutes, I use it to regularly run FlushFileCache.exe or FlushMem.exe to empty the Windows file chache and release memory – often more than a gigabyte – back to Windows)

There is also sleep.exe, but that requires the Windows Resource Kit or Windows Server 2000/2003.

–jeroen

via:

Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows | Tagged: | Leave a Comment »

Windows batch files: How to set a variable with the result of a command (via: Stack Overflow)

Posted by jpluimers on 2013/12/11

One of the easy things in *nix is to set the value of an environment with the output of a command.

Something like this is possible in Windows too, but you have to instruct Windows to keep an empty set of delimiters to capture the full first line.

There is also a small but important difference between Windows and *nix upon command failure: *nix will always return an empty value, but in Windows you must make sure to empty the value first.

Thanks Jesse Dearing for this summary: Read the rest of this entry »

Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows | Leave a Comment »

shorter version of batch file: getting directory and parent directory

Posted by jpluimers on 2013/08/17

Two years ago, I posted a batch file that gets the parent directory of the batch file that runs.

It was convoluted, using substrings and intermediate environment variables so you needed setlocal.

This one does not any more: for both the directory of the batch file, and the parent directory of that directory, one-liners suffice:


echo batchfile=%0
echo full=%~f0
setlocal
for %%d in (%~dp0.) do set Directory=%%~fd
echo Directory=%Directory%
for %%d in (%~dp0..) do set ParentDirectory=%%~fd
echo ParentDirectory=%ParentDirectory%
endlocal

–jeroen

via: batch files: getting directory and parent directory « The Wiert Corner – irregular stream of stuff.

Posted in Batch-Files, Development, Scripting, Software Development | Tagged: , , , | 1 Comment »

command line – create empty text file from a batch file (via: Stack Overflow)

Posted by jpluimers on 2013/08/15

StackOverflow has [WayBack] quite a [WayBackfew questions [WayBackon creating [WayBackempty text files from a batch file.

Most answers involve the [WayBack] NUL special file (which is not available on one special version of Windows [WayBackWindows Fundamentals for Legacy PCs, but can easily be retrofitted).

[WayBack] This is the solution I use most often:

type NUL > EmptyFile.txt

–jeroen

via: [WayBackcommand line – How to create empty text file from a batch file? – Stack Overflow.

Posted in Batch-Files, Development, Scripting, Software Development | 2 Comments »