Click to Rate and Give Feedback
Tips
You can use Ocsetup.exe to install or remove roles, role services, and features on Server Core. Note that the Ocsetup.exe syntax is case sensitive so you must type package names exactly as shown in this list. ...

Read more!

To make replication go smoothly, you need to do a bit of planning, which involves selecting a specific replication model. Here's an overview of the common replication models you will likely choose from. ...

Read more!

Memory is often the source of performance problems, and you should always rule out memory problems before examining other areas of the system. Here’s an overview of counters that you’ll want to track to uncover memory, caching, and virtual memory (paging) bottlenecks. ...

Read more!

You should run maintenance routines against databases on a daily basis. By default, Exchange Server runs maintenance tasks daily from 1:00 A.M. to 5:00 A.M. but you can customized the time if you so desire. ...

Read more!

SQL Server PowerShell includes new cmdlets designed specially for working with SQL Server 2008. Here's a look at what's new, and what you can do. ...

Read more!

Related Articles

Following up on the November installment of his Windows PowerShell column, Don Jones demonstrates an even better inventorying tool and demonstrates a sound process for building your own custom Windows PowerShell functions.

Don Jones

TechNet Magazine December 2008

...

Read more!

In the past few installments of the Windows PowerShell column, Don Jones has been demonstrating how to develop a Windows PowerShell-based inventory tool. In this installment, Don brings it all together into a very powerful, full-featured tool, and offers some guidance on customizing the tool for your own needs.

Don Jones

TechNet Magazine February 2009

...

Read more!

The Scripting Guys discuss Socrates and revisit the topic of querying an XML file . This time, however, the XML file is structured so that rather than using child nodes, additional property values are configured as attributes.

The Microsoft Scripting Guys

TechNet Magazine October 2008

...

Read more!

Automating data entry is more important than ever, and today’s automation language is Windows PowerShell. This article uses Windows PowerShell to collect some data about the local computer and write it to an Office Access database.

The Microsoft Scripting Guys

TechNet Magazine May 2009

...

Read more!

Despite its object oriented nature, Windows PowerShell is also able to parse complicated strings. Don Jones demonstrates how you can do this in Windows PowerShell using Select-String.

Don Jones

TechNet Magazine September 2008

...

Read more!

Also by this Author

For your users with attention span issues—and even those without—providing status feedback in your scripts is a good idea. Here's all you need to know to provide status in Windows PowerShell.

Don Jones

TechNet Magazine March 2008

...

Read more!

Ever wish Windows PowerShell would launch with a work environment tailored to your needs? Don Jones demonstrates how you can use profiles to customize the Windows PowerShell shell.

Don Jones

TechNet Magazine October 2008

...

Read more!

Despite its object oriented nature, Windows PowerShell is also able to parse complicated strings. Don Jones demonstrates how you can do this in Windows PowerShell using Select-String.

Don Jones

TechNet Magazine September 2008

...

Read more!

Permissions in Windows are, unfortunately, innately complicated. But Windows PowerShell can help. Get a quick overview of how permissions in Windows work and find out how you can simplify the task of working with them.

Don Jones

TechNet Magazine February 2008

...

Read more!

When designing Windows PowerShell, a lot of thought was put into security. But, as with any software, Windows PowerShell can still be exposed to malicious code. Here are some recommendations for choosing an Execution Policy that will keep your systems more secure.

Don Jones

TechNet Magazine January 2008

...

Read more!

Popular Articles

Raymond Chen explains why it is often the developers who have their acts together who end up checking in the final bug fixes to products.

Raymond Chen

TechNet Magazine January 2009

...

Read more!

Security principals underlie so much of Windows security that it is essential for any administrator to have at least a basic understanding of how the various types of Security principals work and how they are used. Here's what you need to know.

Jesper M. Johansson

TechNet Magazine January 2009

...

Read more!

Without too much effort, you can deploy a terminal server to host the applications you need in your environment. But there are some important decisions you’ll need to make to ensure your implementation meets user expectations. Greg Shields discusses the various options you have and explains how they will affect you.

Greg Shields

TechNet Magazine January 2009

...

Read more!

Why do attachment sizes increase when sending and receiving e-mail messages? How can you ensure databases on a passive node in a CCR cluster are defragmented during online maintenance? Can you use an external trust between forests? We answer these questions and more.

Henrik Walther

TechNet Magazine January 2009

...

Read more!

Raymond Chen looks at the skewed relationship bugs have to errors, and explains why it's important that programmers suffer as well as give results.

Raymond Chen

TechNet Magazine October 2008

...

Read more!

Our Blog

NAP monitors the health of specified computers when they attempt to connect to a network and includes a number of mechanisms to enforce health requirements. In this article, Geek of All Trades Greg Shields gives readers an overview of these enforcement mechanisms and, as an example, takes a closer look at setting ...

Read more!

Use Windows PowerShell to Manage Virtual Machines Here are a few examples of how you can use Windows PowerShell scripts to manage virtual machines running on a Server Core installation. Note that these scripts are presented as samples and may need to be customized to work in your environment.

Create a New ...

Read more!

Disabling an Unused Part of Group Policy Objects One way to disable a policy is to disable an unused part of the GPO. By disabling part of a policy that isn’t used, the application of GPOs and security will be faster.

Administer Windows Server 2008 Server Core from the Command Prompt ...

Read more!

In the August 2008 issue of TechNet Magazine, Paul Randal wrote an article Top Tips for Effective Database Maintenance.  It was geared toward "involuntary  DBAs" (IT pros who inadvertently wind up responsible for a SQL Server instance).  The article had a great response from our readers so Paul has written another ...

Read more!

Microsoft Forefront is designed to deliver an integrated security solution that makes it much easier to deploy and manage security across an organization’s IT infrastructure. In this, our annual security issue, we feature two articles that describe how Forefront Security protects instant messaging and e-mail.

Protect ...

Read more!

Windows PowerShell Sign Here, Please
Don Jones


In the January 2008 Windows PowerShell column, I stressed the importance of digitally signing your Windows PowerShell scripts. And I described some scenarios where any execution policy other than AllSigned could create significant security vulnerabilities in your environment.
One thing I didn't cover in much detail is how to actually sign your scripts. You can take a look at that column at technetmagazine.com/issues/2008/01/PowerShell. Guess what I'm covering this month?

More than Just a Signature
The word signature is the correct technical term for what I'm talking about, though the word itself isn't really a good description of what is actually happening here. Signing a script doesn't mean you're approving or authorizing it, as you might do with a contract or a credit card slip. In the world of digital security, signing is the process of affixing your identity to something and ensuring that the state or condition of that "something" has not been modified in any way. It's all based on cryptography, and cryptography (at least in this case) starts with a pair of encryption keys.
These keys are referred to as asymmetric keys because they are different from one another. The pair consists of a private key, which is accessible only to you, and a public key, which is accessible to anyone. I'm simplifying this a bit, but basically anything encrypted with the private key can only be decrypted with the matching public key.
Here's how it works. Again, I'm using some simplification just to keep the discussion moving along. I have a Windows PowerShell® script that I want to sign and I have a certificate containing my private key that I want to use to sign the script. Windows PowerShell itself has a cmdlet, which I'll discuss in a moment, that can take the script and the certificate and perform the actual signing. When it does this, Windows PowerShell takes the script and encrypts it using my private key. That encrypted copy appears as gibberish text that is added to the bottom of the script as a series of comments (see Figure 1). My identity is also encoded, but not encrypted, into those comments—the identity itself can be read without resorting to cryptography.
Figure 1 Signature block stored at the bottom of your script (Click the image for a larger view)
Later, when Windows PowerShell looks at the signature, it decodes my identity and uses it to obtain my public key. Since only my public key can decrypt the rest of the signature, Windows PowerShell knows that if it's able to decrypt the signature, then I must have signed it. If someone else had signed it, my public key wouldn't be able to decrypt the signature. Once the signature is decrypted, Windows PowerShell compares the script to the copy that had been encrypted into the signature. If they match, then the signature is considered intact. If the two scripts differ, the signature is broken and considered invalid.
This is how a signature protects a clear-text script from being modified without someone noticing. Remember, while the script itself can be easily changed, the signature can't be changed to match the modified script without using my private key—and only I have my private key.
This is definitely a simplification of the underlying technical process. In reality, signatures can and do contain more information, such as a copy of my public key, information about the certificate authority (CA) that issued the keys, and so on. But my description here certainly outlines the important parts of the process and highlights the fact that a signature actually does protect your script from unauthorized modification.
A critical security factor is the absolute protection of your private key—you don't want this falling into someone else's hands. When you install your key in Windows, you should immediately password-protect it so that a malicious process can't use your key without your knowledge. Smart cards provide even better security since they contain your private key. With a smart card, your private key never leaves the card. Instead, the data that you want to encrypt is transmitted to the card through its reader hardware, and the circuitry of the card itself performs the encryption and sends the result back.

Getting a Certificate
Cmdlet of the Month: Export-CSV
The folks in the forums at PowerShellCommunity.org often ask if Windows PowerShell can export to Microsoft Excel®. Of course it can! The Export-CSV cmdlet does the trick, since Excel can natively open, edit, and save CSV files. For example, if you want to export a list of services, just run Get-Service | Export-CSV MyServices.csv. In fact, you can tack Export-CSV onto the end of any pipeline and all the objects in that pipeline will wind up in the CSV file you specify. By default, Export-CSV adds a header line to the top of the file, which it uses to specify the type of object that was exported. This line is commented so it won't prevent Excel from opening the file. If you don't want that type information exported, you can just add the -notype parameter to Export-CSV. In addition, you can add the -force parameter to force overwriting an existing CSV file of the same name without prompting (the default is to prompt), or you can add the aptly named -noClobber parameter to prevent overwriting an existing file.

To sign scripts, you will need a specific kind of certificate—a Class III Authenticode Code-Signing Certificate—and there are three main ways to get one. The first is to use your organization's internal Public Key Infrastructure, or PKI, if it has one. A well-implemented PKI will have its root CA trusted by all of your organization's computers—this is necessary for the certificates issued by the CA to be usable in the organization.
Windows Server® has shipped with its own Certificate Server software since Windows® 2000, and you can use that software to create your own PKI. A full-fledged PKI implementation requires a lot of planning, but if you just need a PKI for the sole purpose of issuing code-signing certificates, you don't need to do a ton of work. You can simply use Group Policy to push your CA's root certificate out to your computers so that they'll trust it.
A second option is to use a commercial CA. One benefit of using a commercial CA is that, if you select one of the major CAs, your organization's computers are probably already configured to trust certificates from that CA. (Note that Windows XP trusts a large number by default, while Windows Vista® trusts a far smaller number by default). One popular commercial CA is VeriSign (verisign.com). There are others worth investigating, also, such as CyberTrust (cybertrust.com) and Thawte (thawte.com).
The third option for obtaining a code-signing certificate is to make your own self-signed certificate using a tool like Makecert.exe. It ships with the Windows Platform SDK, it installs in some editions of Microsoft® Office, and it can be found in many other places. The upside of a self-signed certificate is that it's free and doesn't require any infrastructure. The downside, however, is that it's really only usable on your computer. But if you just need to enable script execution on your computer—for testing or general experimentation with code signing—Makecert.exe is a great option. Documentation about this tool is at go.microsoft.com/fwlink/?LinkId=108538. Just be aware that many versions of this tool have existed over the years, so it's possible that your computer is running an older version that doesn't work exactly as the documentation describes.
Once you have Makecert.exe, you can then create a self-signed certificate by running something like the following from the Windows PowerShell command line (and don't let me catch anyone using cmd.exe when you're reading this column):
makecert -n "CN=MyRoot" -a sha1 –eku
1.3.6.1.5.5.7.3.3 -r -sv root.pvk root.cer 
–ss Root -sr localMachine
And then run the following:
makecert -pe -n "CN=MyCertificate" -ss MY 
–a sh1 -eku 1.3.6.1.5.5.7.3.3 -iv root.pvk 
–c root.cer
Makecert.exe will prompt you for a password to protect the key, and then it will create the certificate. You can verify the installation by running this code in Windows PowerShell:
gci cert:\CurrentUser\My -codesigning
This will display a list of all the items in the CERT: drive (that's your Windows certificate store) that are code-signing certificates. By the way, all of this is also documented in Windows PowerShell itself. To find that documentation, just run help About_Signing and read down a few screens.

Signing a Script
Now that you've got a certificate and you've got a script (for testing purposes, you can just make one up quickly, if you need to), you're ready to sign the script. It's shockingly easy with Windows PowerShell—just enter:
$cert = @(gci cert:\currentuser\my
-codesigning)[0]
Set-AuthenticodeSignature myscript.ps1 $cert
The first part retrieves the first installed code signing certificate (if you have multiple certificates installed and want to use one other than the first, just change the "0" to the appropriate number). The second part signs the file (of course, you'll need to change the filename to match yours). It's that simple! To be totally honest, though, I find the Set-AuthenticodeSignature cmdlet name to be a bit longer than necessary, so that's why I created an alias called Sign. Now, I can just run:
Sign myscript.ps1 $cert
Now open that script and you'll see the signature block inserted at the bottom. Try running the script with your execution policy set to AllSigned (Set-ExecutionPolicy AllSigned) and it should work fine. Now you'll want to modify the script and save it, but make sure you don't sign it again. Windows PowerShell should now simply refuse to run the modified version because the signature has been broken.

Worth the Effort
Isn't this really more hassle than it's worth? Is it asking too much to be able to script a few administrative tasks without having to drop $500 on a certificate or implement a whole new infrastructure element? Look, I'm an admin, too and I understand your concerns. But the simple answer is that this is definitely worth every bit of the effort it takes.
The fact is security almost always involves some level of hassle. Antivirus software can be a pain, but you aren't going to run without it. Firewall software is definitely a hassle, but as you know, it isn't safe to run without a firewall. And Windows Vista User Account Control (UAC) can also be a hassle, but it makes your computer safer. And isn't it this improved security that we're all trying to achieve?
Windows PowerShell is a powerful tool, and like any other tool, there is always the potential for it to be twisted into a security vulnerability by a malicious user. That is why it is important for you to take steps to head off these malicious users.
Code signing is the best step you can take, and it doesn't take much involvement. For example, I use a graphical script editor that automatically signs my scripts every time I press Save, so code signing is pretty much transparent for me.
You can make it more transparent even without a fancy editor. For example, you can create a Windows PowerShell profile for yourself (create the file Microsoft.PowerShell_profile.ps1 in a folder named WindowsPowerShell, located under your documents folder), and add this function to it:
function sign ($filename) {
  $cert = @(gci cert:\currentuser\my 
-codesigning)[0]
Set-AuthenticodeSignature $filename $cert
}
Now you can sign your script by simply entering:
Sign c:\scripts\myscript.ps1
Of course, that profile script should be the first one you sign! The idea is to take a few steps to make script signing as convenient as possible, so that it's not a hassle.
Deploying a one-server PKI for the sole purpose of issuing a code-signing certificate isn't much work. (Keep in mind, though, that you really do need to research and plan your PKI, including protecting the root CA and providing disaster recovery, especially if it will be used for anything else.) Makecert.exe is always available if you're the only one who will need to run scripts in your environment. Note that the About_Signing Windows PowerShell help topic also includes information on securing the certificate produced by Makecert.exe.

Don Jones is the author of Windows PowerShell: TFM and VBScript, WMI, and ADSI Unleashed. You can contact him through the PowerShellCommunity.org Web site.
© 2008 Microsoft Corporation and CMP Media, LLC. All rights reserved; reproduction in part or in whole without permission is prohibited.
Page view tracker