Archive for the ‘powershell’ Tag

DNS check for powershell

Saturday, October 11th, 2014

I had a need to do a quick DNS lookup for computers in a powershell script which going through a list of computers. There are two ways to a system will not have a DNS entry: 1) System retired. 2) System uses DHCP and is not on the network.

A quick way to perform a look would be to use:


It does give output directed to the console.  If you want to suppress it, use the [void] option.



Trimming white space

Friday, October 10th, 2014

Working with a CSV file; I had a situation where the command line would give the output I wanted but the script would get the object information.  I found I could get what I needed by converting the output to a script but it introduced white space.  A computer name would turn into:

“Computer                                       “

What was interesting was the system commands would treat the white space as part of the computer name which of course cause the commands to fail.

Look around the Net; I found a command to trim white space.

$server = $server.trim()

The system name returned to proper size and what is great about this command is that it will trim white space from both sides.

Replace a character in a string

Friday, October 10th, 2014

I had a script where I needed to change a character in a string.

Simple task as I found on the Net.

$str = $str.replace(‘\’,’_’)

Scriptblock doesn’t work with variables.

Friday, October 10th, 2014

Powershell is a fun experience which I wish I had started awhile ago.  It’s features seem endless and it greatly eases system management.

The recent scare over Shellshock caused us to evaluate our Cygwin installations.  We didn’t know the exact number of installations and the version of Bash in use.

The question of the number of Cygwin installations was easy to solve. A script to review the organization units and scan each computer for Cygwin.

After a list was generated; a script was written and three results files were created: Found, System Down, and Not Found.

Cygwin due to it’s open source nature does not follow Microsoft design standards.  It has it’s own way of doing things. Good for the user but not always good for system management purposes.

I didn’t find a “Quick and Dirty” way to get the version number of the Bash executable.   Nothing in the registry and file information lacked details.

Redhat (we use them for support) advised using the “–version” option to identify the version.  A rather tedious task if done through remote desktop or nagging the users to send the information.

What would be nice if there was way to run a command remote and get the information.

Powershell has a nice option called “invoke-command.” It’s a one use option for such things.

The command is rather simply to use:

Invoke-Command -scriptblock { c:\cygwin\bin\bash –version }  -computername <hostname>

This gave the following output:

GNU bash, version 4.1.15(1)-release (i686-pc-cygwin)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

A little too much detail for a version number.  This was quickly addressed by using Select-String command.

Invoke-Command -scriptblock { c:\cygwin\bin\bash –version }  -computername <hostname> | Select-String -pattern “GNU bash, version”

This in turn gave:

GNU bash, version 4.1.15(1)-release (i686-pc-cygwin)

This was usable.  It also helped with simple errors.  If you don’t get the expected result, add an error message. This became evident when trying to run the command on Windows 2003 which lacked Powershell 2.0 and systems which did not have remote management configured.

Invoke-Command is great.  It gave me the information I needed without visiting each system.

I wrote a script and quickly found two problems:

  1. There are flavors of Cygwin and unless there was a conscious effort during install; you could end up with different directories (ie: C:\cygwin, C:\cygwin64, C:\rhcygwin).
  2. Running the command for several systems.  Invoke-Command can take up to five systems.  Great for a small number but not so great when you have hundreads or more.

Both of these issues required the use of variables and it was quickly discovered that -scriptblock option couldn’t use variables. For example:  $cygwinPath\bash.exe –version resulted as \bash.exe –version

The dynamic nature of scanning several computers was not something the command could do.  Rather then use another approach; I looked around the Net and found there was a way to use dynamic scriptblocks by using the NewScriptBlock command.

A quick change:

$scriptblock = $ExecutionContent.InvokeCommand.NewScriptBlock( “$cygwinPath\bash.exe –version”)
$versionResult = Invoke-Command -scriptblock $scriptblock -computername $server | Select-String -pattern “GNU bash, version”

This gave the needed results and we had an understanding of the work needed to be done.

If you would like to read more about Invoke-Command, Ed Wilson did a nice little writeup on his blog.

How to skip the first line with import-csv

Wednesday, September 3rd, 2014

I had a “quick and dirty” script to examine a list of hosts from a CSV.

There was a header with the data and I didn’t want the error that would pop up at the end of the foreach loop.

To get around the first line; I used a pipe and the select statement:

Import-Csv $file -header(“Hostname”, “OldIP”, “NewIP”) | select -Skip 1 | foreach {

Simple and it avoids a comparison check in the code.

The only thing to be concerned is if there are times there will be files without a header.

Powershell editor

Wednesday, January 1st, 2014

I sometimes get asked for suggestions for a Powershell editor.   Probably the biggest and best  is Sapiens Primalscript.  But with all the abilities and power; this means a bigger price tag. There is also an expectation of upgrades.  I let mine go too long and was told I had to purchase new.  😦

I also find many people are looking for a free editor.  Microsoft has a page of suggested editors on Technet.

As mentioned I have used Primalscript and recommend it if you have the money to invest.

I have used the one that comes with Powershell and it’s ok if you are starting out.

I have used the GUI version and found it’s ok for most things.

I am now starting to “play around” with Admin Script Editor.  The page shows it’s a purchased package but this is no longer the case.  The company closed shop but they made the last version free to people who use the itninja site.




Admin Script Editor is no more

Wednesday, January 1st, 2014

I was looking at upgrading my editor for powershell.

Normally, I use Sapien but I had not upgraded it for several years and they said I basically had to purchase it new.   I thought maybe with the New Years Eve deal but it appears they stopped doing that.  😦

I had a look at the old ASE and found the company had closed its doors last September but they made the last version free to people who join the itninja site.

It’s the enterprise version and so far it runs on 8.1.

Time will tell.



Powershell: Direct output to a variable.

Thursday, July 25th, 2013

I was asked if there was a way to capture output of a specific program to a variable.

Basically, the person wanted to get a version number from the program.

Played around and had a way to accomplish this  but I found a simpler approach.

$scriptOutput = & "c:\path\program.exe" -V 2&1

Obviously, the -V works with the program in question.

I am liking powershell

The account used is a computer account. Use your global….

Thursday, July 25th, 2013

While scripting a report; I noticed a bogus error about a file version being wrong.

I examined the server in question and found the server service was not running.

I tried to start it but it stopped right away with the following message in the system event log.

The Server service terminated with the following error:
The account used is a computer account. Use your global user account or local user account to access this server.

Another server with the same problem would give this message:

Windows could not start the Server service on Local Computer Error: 1808: The account used is a computer account. Use your Global user account or local user account to access this server.

The service was configured correctly.

An odd problem and it’s cause was the fact a couple users placed URLs in the system Path variable (ie \\server\mount\dir).

Removing the entries and a reboot of the server corrected the issue.

set-executionpolicy unrestricted is denied.

Sunday, February 3rd, 2013

One of the steps we have for setting up a new LSF server is to run a powershell script which does a survey of the machine. The first step is to un-restrict the execution policy via this command:

set-executionpolicy unrestricted

The command was entered and it returned this nice little error:

Execution Policy Change
The execution policy helps protect you from scripts that you do not 
trust. Changing the execution policy might expose you to the security 
risks described in the about_Execution_Policies help topic. Do you 
want to change the execution policy?

[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\
SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft .PowerShell' is 

At line:1 char:20
+ set-executionpolicy <<<<  unrestricted
    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], 
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,

Rather odd since all security was correct.

Two obvious ideas are to upgrade powershell or reinstall it. Neither was an option is this situation.

I did a quick look around Microsoft and found this.

I decided to try the registry option and added the login.

* Run: regedt32
* Right-Click > Permissions
* Select, and add your account, grant it “Full Control” privileges
I re-entered the set-executionpolicy command and this time it worked.