Grow Your PowerShell

We’ve spent some time looking at PowerShell and every time we get better at it.

Some confusion arises from the use of custom functions but we have on method that’s shown to work.

First, we’re working off a set of external sources from some of our new best friends.

We found a discussion at ServerFault.com, here, that provides us with the code to support a specific use case that’s always driving us nuts, proving that a user ID and password is good or bad. This is remarkably handy when working with service accounts that are supported by a bunch of neanderthals in a cage labeled “Windows Support.”

Then we have another discussion, here, that helps us with some PoSh function management stuff.

And, finally, our new best friend, Don, explains a bit here about executing a number of commands on a list on inputs piped in one at a time.

So, here goes…

You can create a function interactively from your PoSh prompt one line at a time.

PS C:\Windows\system32> Function WriteSomethingOnScreen {
>> Write-Host "Something"
>> }
>>
PS C:\Windows\system32>

In that code, note the following:

  • The function amounts to the word “function,” the function name and some code inside {curly braces}.
  • When you start building a function, PoSh figures it out and turns your <path>> prompt into an interactive, “>>” prompt.
  • When you feed the >> prompts a blank line, it jumps out of interactive mode and back into your standard, <path>> prompt.

Now we have a function in memory and we can call it by name at the <path>> prompt:

PS C:\Windows\system32> Function WriteSomethingOnScreen {
>> Write-Host "Something"
>> }
>>
PS C:\Windows\system32>

If we kill our PoSh session, the function dies with it.

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\Windows\system32> WriteSomethingOnScreen
The term 'WriteSomethingOnScreen' is not recognized as the name of a cmdlet, fu
{Blah,blah, blah}
 + CategoryInfo : ObjectNotFound: (WriteSomethingOnScreen:String)
 [], CommandNotFoundException
 + FullyQualifiedErrorId : CommandNotFoundException
PS C:\Windows\system32>

Yes, we have to create our function all over again from scratch. Since that sucks, we’ll just write our function into a script in NotePad and save it, no problem.

 

01_NotePad

 

Save the script and run it in PoSh:

First thing you’ll note is that, by default, PoSh will block scripts from running:

PS C:\Windows\system32> writesomethingonscreen.ps1
File C:\Windows\SYSTEM32\WriteSomethingOnScreen.ps1 c
e execution of scripts is disabled on this system. Pl
{Blah, Blah, Blah}
 + FullyQualifiedErrorId : RuntimeException
PS C:\Windows\system32>

So run:

PS C:\Windows\system32> Set-ExecutionPolicy Unrestricted

Execution Policy Change
The execution policy helps protect you from scripts that you
{Blah, Blah, Blah}
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
PS C:\Windows\system32>

Now PoSh will run your script.

PS C:\Windows\system32> WriteSomethingOnScreen.ps1
PS C:\Windows\system32>

Note also that the input is NOT case sensitive.

PS C:\Windows\system32> writesomethingonscreen.ps1
PS C:\Windows\system32>

So our script created our function and we should be able to run it now, right? Wrong. We actually have to call the function from the script to make it work by adding:

WriteSomethingOnScreen

..to the bottom of the script and then running it.

PS C:\Windows\system32> writesomethingonscreen
Something
PS C:\Windows\system32>

We’ll look at the list of inputs next time.

This is where we’re headed:

Function Test-Credentials {
 Param($context, $username, $password, $domain)
 Add-Type -AssemblyName System.DirectoryServices.AccountManagement
 $ct = [System.DirectoryServices.AccountManagement.ContextType]::$context
 $pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ct, $domain)
 New-Object PSObject -Property @{
 UserName = $username;
 IsValid = $pc.ValidateCredentials($username, $password).ToString()
 }
} 

Then, your command looks like this:

test-credentials <{"Machine" | "Domain"}> 
                 <User ID> 
                 <Password> 
                 <{Macine Name | Domain Name}>

Of course, replace the new lines with simple blank spaces.

This does a lot of cool stuff. Once it’s written into the PoSh memory you can run it like a CMDLT and pass it, with no commas, a context {machine or domain} a UID, a password and a domain or machine name and it will tell you if the UID and PW are correct.

We’ll look at it more closely next time.

hth

-robot

 

 

 


Tags:

 
 
 

Comments are closed.