Scripting
PowerShell Import-Csv Error
by Warlock on Apr.17, 2009, under .NET, Scripting
I ran across the following error while working with a PowerShell script I was developing:
Import-Csv : Cannot process argument because the value of argument "name" is invalid. Change the value of the "name" argument and run the operation again.
It had me stumped for a while. The Impor-Csv command was failing, but the file name was good (I could cat the files contents to standard out).
Long story short, the error came from having trailing blank columns in my CSV. Import-Csv uses the first row in the CSV as names for the columns (unless you specify otherwise) and when you have blank columns (or at least multiple blank columns) it causes this error as it doesn’t have a valid name for them. The underlying culprit for the error was actually Microsoft Excel. I was exporting a spreadsheet to a CSV and the spreadsheet had previously had some trailing columns. I values to the columns, but apparently Excel decided they should still show up in the export. Fully deleting the columns so they didn’t show up in the CSV resolved the problem.
Customizing Your Powershell Prompt
by Warlock on Mar.30, 2009, under .NET, Scripting
Powershell is a great CLI for Windows and I appreciate it’s design more and more every time I use it. As I interactive with for more and more tasks, wanted to customize it a bit so that it looked prettier, and so I could visually distinguish what was going on more easily.
One thing I’ve always done in the *NIX world was add color to my prompt so that it would be easy for me to tell where each command/output sequence started and ended. To do this you need to edit (or create) the file <My Documents>\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
To determine how to render the prompt, Powershell calls a function, prompt. To change the appearance, just implement this function in your profile. Mine is as shown below:
Write-Host -nonewline "Welcome to PowerShell "
Write-Host -nonewline -foregroundcolor Magenta $env:Username
Write-Host "!"
Write-Host " "
function prompt {
Write-Host -nonewline -foregroundcolor Magenta "PS "
Write-Host -nonewline -foregroundcolor Green $(get-location)
Write-Host -nonewline -foregroundcolor Magenta " >"
" "
}
Note that the output uses the -foregroundcolor flag with Write-host to change the output color. Also note that built-in variables are used to determine context information, such as the current location.
This blog post worked a quick reference for me.
Alternative to Regedit
by Warlock on Sep.22, 2008, under Scripting
Anyone who spends time on Windows quickly becomes familiar with regedit. It’s the fastest way to to get at a lot of configuration information and set specific application/OS flags.
When working from the command line, however, the reg command can be a better alternative for use in .BAT scripts. The reg command is specifically designed for scripting, and can do things like compare registry keys, retrieve key values, import/export .reg files, and much more. The help output of the command is listed below.
C:\Documents and Settings\Foo>reg /?
Console Registry Tool for Windows - version 3.0
Copyright (C) Microsoft Corp. 1981-2001. All rights reserved
REG Operation [Parameter List]
Operation [ QUERY | ADD | DELETE | COPY |
SAVE | LOAD | UNLOAD | RESTORE |
COMPARE | EXPORT | IMPORT ]
Return Code: (Except of REG COMPARE)
0 - Succussful
1 - Failed
For help on a specific operation type:
REG Operation /?
Examples:
REG QUERY /?
REG ADD /?
REG DELETE /?
REG COPY /?
REG SAVE /?
REG RESTORE /?
REG LOAD /?
REG UNLOAD /?
REG COMPARE /?
REG EXPORT /?
REG IMPORT /?
