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,862 other subscribers

Archive for the ‘PowerShell’ 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 »

PowerShell conditional and logical operators (via: PowerShell Pro!)

Posted by jpluimers on 2014/04/03

Switching between many different languages, I tend to forget the exact names and symbols of the PowerShell operators.

Most of the ones I use have to do with comparison and logic, o here they are:

Most used comparison operators

Operator Description
-eq Equal to
-lt Less than
-gt Greater than
-ge Greater than or Eqaul to
-le Less than or equal to
-ne Not equal to

Logical operators

Operator Description
-not Not
! Not
-and And
-or Or

I find it funny that you have ! and -not, but not -!. Oh well (:

Talking about logicals: PowerShell can coerce a couple of values to $false, but I’m ambivalent to use that: it does shorten code, but is very PowerShell specific.

Before I forget: an operator that is undervalued, is the -f operator that does formatting.

It uses the standard .NET formatting strings, so that is an easy way to put your .NET knowledge to use. Some further reading on the -f operator:

–jeroen

via: Conditional Logic | PowerShell Pro! :: PowerShell Pro!.

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Tagged: , , | Leave a Comment »

Escaping in PowerShell: the back-tick ` is the escape character.

Posted by jpluimers on 2014/04/03

Somehow I always forget this:

The PowerShell escape character is the backtick “`” character.

Use it for escaping quotes within quotes, or inserting special characters.

Don’t abuse it (see the great debate: do not use it as a line continuation).

Often you can avoid line continuation characters by using splatting, which is one of the “best kept secrets” in PowerShell.

–jeroen

via: Escaping in PowerShell.

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

Getting Microsoft Product Keys back from the registry (via StackOverflow and various other sources)

Posted by jpluimers on 2014/04/02

Every once in a while, someone hoses their computer far enough that it has to be reinstalled, but the original Microsoft product keys are misplaced, and some creepy anti-virus tool disallows the running of standard product key recovery tools like nirsoft’s.

Well, there is enough sourcecode that does recover it, just look for any of these strings:

Some hits:

The below full executables can trigger a virus warning (ordered from less often to most often):

–jeroen

 

Posted in .NET, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 2.0, C# 3.0, C# 4.0, C# 5.0, CommandLine, Delphi, Development, PowerShell, Software Development | Leave a Comment »

.NET/C#/PowerShell: building .NET DiscUtils library for virtual disk images

Posted by jpluimers on 2014/03/11

Edit 20250107: added various [Wayback/Archive] archived links (dated as close to the blog post as possible) because of link-rot; marked some keywords as code; found the current .NET DiscUtils repository at [Wayback/Archive] GitHub – DiscUtils/DiscUtils: Utility libraries to interact with discs, filesystem formats and more where you also can download it.

 

.NET DiscUtils is an interesting open source .NET library for accessing and manipulating virtual disk images. Since it is entirely written in C# (without the need for P/Invoke), you should even be able to run this on non-Windows machines using mono. Later on, you will see the 0.11.0 build fails this, but it gives good hope it eventually will.

Virtual disk formats supported are DMG, ISO, RAW (IMG/IMA/VFD/FLP/BIF), VDI, VHD, VHDX, VMDK, and [Wayback/Archive] XVA, regular disks like Physical, iSCSI and NFS.

There are two ways of getting the .NET DiscUtils tools to run:

  1. [Wayback/Archive] download pre-build binaries (at the time of writing: version 0.10) from via [Wayback/Archive] .NET DiscUtils – Home, or
  2. from the [Wayback/Archive] latest source page, click the download button, then build the binaries from the source package. At the time of writing, that version is 0.11.

This post describes the second way, and requires PowerShell to be installed on your system (which probably is, as Windows 7 and Windows Server 2008 R2 include it). Read the rest of this entry »

Posted in .NET, .NET 4.0, .NET 4.5, C#, C# 4.0, C# 5.0, CommandLine, Development, PowerShell, Software Development | 1 Comment »

PowerShell: Do not take a shortcut while testing NULL values.

Posted by jpluimers on 2014/03/05

A while ago I found a blog post explaining how to shortcut testing NULL values with PowerShell.

Do not do that!

I agree with the quote on the blog:

One thing you may not forget is that Powershell is a lot more friendly for NULL values than C#, so don’t forget to check your objects for NULL values. In Powershell this is very clean and easy to do.

But it is also easy to get wrong:

To see if a variable is null, simply check:

If (!$Variable) {some action}

Conversely, to verify if the variable has any value:

If ($Variable) {some action}

Just a few examples. Now quess the outcome for all of them.

$a=$null;  if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a=$false; if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a=0;      if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a='';     if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a="";     if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a=1;      if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}

Now take an educated guess on the outcome. Read the rest of this entry »

Posted in .NET, CommandLine, Development, PowerShell, Software Development | Leave a Comment »

One of the niceses PowerShell 3.0 new features: Simpler Where Filter Syntax

Posted by jpluimers on 2014/03/04

You can check out which PowerShell you have by executing the $Host.Version or $PSVersionTable.PSVersion on a line. You can even switch versions by applying the PowerShell -version switch on the command-line and they will both change.

One of the great features of the new PowerShell 3.0 features (besides New and Improved PowerShell 3.0 Cmdlets) is a simplified Where Filter Syntax.

So: this is how I get the PowerShell version information the easy way from a command prompt:

PowerShell $PSVersionTable.PSVersion

–jeroen

via:

Posted in .NET, CommandLine, Development, PowerShell, Software Development | Leave a Comment »

PowerShell aliases (via: Top Ten PowerShell Aliases for DOS Commands)

Posted by jpluimers on 2014/02/26

In most programming environments, I tend to avoid abbreviations, especially since command-completion and parameter-completion makes it easier to write readable code.

Same fore PowerShell: the PowerShell ISE has great completion features.

Other people tend to use abbreviations, especially since many PowerShell aliases make it easier for people coming from a cmd or bash background.

Two Get-Alias commands I use quite often for researching aliases:

  • Get-Alias -Definition <name>
  • Get-Alias | Sort-Object Definition

The former gives you the CmdLet for an alias.
The latter all defines alises sorted by the CmdLet definition.

Two aliases that I tend to avoid are these:

While you are at it, there are also parameter aliases. Read Weekend Scripter: Discovering PowerShell Cmdlet Parameter Aliases on TechNet Blogs to learn more about these.

–jeroen

via: Top Ten PowerShell Aliases for DOS Commands.

Posted in .NET, CommandLine, Development, PowerShell, Software Development | Leave a Comment »

Ye Olde Automated 16TB Windows Azure Virtual Machine demo – Team Individualism – Site Home – MSDN Blogs

Posted by jpluimers on 2013/07/02

For my link archive: PowerShell Scripts to create VMs with large disks.

Ye Olde Automated 16TB Windows Azure Virtual Machine demo – Team Individualism – Site Home – MSDN Blogs.

–jeroen

Posted in Cloud Development, CommandLine, Development, Power User, PowerShell, Software Development, Windows Azure | Leave a Comment »

PowerShell: fixing script signing errors even after you had “Set-ExecutionPolicy RemoteSigned”

Posted by jpluimers on 2013/06/27

Once every while PowerShelll users get an error like this:

PS C:\bin> . .\DownloadedScript.ps1
. : File C:\bin\DownloadedScript.ps1 cannot be loaded.
The file C:\bin\DownloadedScript.ps1 is not digitally signed.
The script will not execute on the system. For more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ . .\DownloadedScript.ps1
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
PS C:\bin>

I recently had it too, but was surprised this happened as I took the steps in my previous blog posts on this topic: Read the rest of this entry »

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »