Mar 29, 2015

Embedding Expressions in Select-Objects

I had my first taste of using Select-Objects in a way more than just modifying values on output or to specifically pick a set of attributes to list.

When someone asks for a list of users and their managers, meh, no big deal. When they ask for the user, their manager, and their manager’s email address -- well, no big deal but not as much of a no big deal as the first one.

I found it a bit annoying that I had to write a script to do this every time I wanted to get this type of information so I did a bit of exploring. Turned out a little while ago, while experimenting in optimizing speed in a script, I had tried a method of using Select-Object to create a custom object.

 

Using Select-Object to Pull Manager Detail on the Fly
$myData | select 
    @{n='UserId';e={$_.samaccountname}}, 
    @{n='Created';e={$_.lastlogon}}, 
    @{n='Name';e={$_.name}}, 
    @{n='Manager';e={$_.manager}},
    @{n='Manager Email';e={ 
        (get-aduser $_.manager -properties mail).mail 
        }
    }

Hopefully this makes sense. I broke it out so it’s clearer to read. For my example, I already had a dataset with specific information in it. I just needed to pipe it out and get the manager email. I piped this to export-csv to create a file to look at.

The meaningful part here is that you can embed things in the expression -- like the Get-AdUser call.

 

Slightly More Challenging
$myData | select 
    @{n='UserId';e={$_.samaccountname}}, 
    @{n='Logon';e={$_.lastlogondate}}, 
    @{n='Name';e={$_.name}}, 
    @{n='Manager';e={ 
        (get-aduser $_.samaccountname -Properties manager).manager 
        }
    }, 
    @{n='Manager Email';e={ 
        (get-aduser $(get-aduser $_.samaccountname -Properties manager).manager -properties mail).mail 
        }
    }`

And again, I broke this out but in reality ran it on a single line. In this case, I didn’t have the manager value already so I had to run a command in both expressions -- manager and manager email.

This process isn’t going to scale well with a lot of data elements. This is just to show you something that might save you a little time if you’re just tooting around. :-)

Mar 27, 2015

DHCP Scope Information

Thought I’d squeeze in this post before Windows Server 2003 drifts off into the sunset. (Don’t pretend like you don’t have these servers floating around.)

Okay, so, I was recently asked to validate that some DHCP scope work was performed correctly. Validation in this case was to pull all the scope options. It would have been immensely helpful to use PowerShell to do this. However, I made do without it using Netsh.

 

Retrieving Scope Options for a Single Scope

netsh dhcp server <servername> scope <scopeaddr> show optionvalue

That’s easy. How about for every scope on my server? That’s easy, too, as it turns out.

 

Retrieving Scope Options for All Scopes
for /f %a in ('netsh dhcp server <servername> show scope ^| find /i "."' ) do @netsh dhcp server <servername> scope %a show optionvalue

Mar 26, 2015

PowerShell: Updating Terminal Services Profile Information

If you’ve done any dabbling in the AD cmdlets and attempted to update terminal services information, you’ll hit a wall with the traditional cmdlets. Why? Well, simply, what you see in AD Users and Computers is not the way the values are actually stored, as Ed explains.

Well, luckily, it turns out it’s not that hard. I was asked to come up with a process to update the profile path. This is a sample of what I ended up with:

$PathValue = <myUserPath>
$myUser = "myUserName" $User = [ADSI]LDAP://$((Get-AdUser $myUser).distinguishedname) $User.psbase.invokeset("TerminalServicesProfilePath",$PathValue) $User.setinfo()

 

Back to the Scripting Guys’ script, here is a function that shows the possible values that can be modified:

function SetTSProperties()
{
 $ou = [adsi]"LDAP://ou=mytestou,dc=nwtraders,dc=com"
 $user = $ou.psbase.get_children().find($userDN)
 $user.psbase.invokeSet("allowLogon",1)
 $user.psbase.invokeSet("TerminalServicesHomeDirectory",$hDirValue)
 $user.psbase.invokeSet("TerminalServicesProfilePath",$ppValue)
 $user.psbase.invokeSet("TerminalServicesHomeDrive",$hdValue)
 $user.setinfo() 
} #end SetTSProperties

Feb 9, 2015

PowerShell: Static Methods

Thanks PowerShell.com for the “Useful Static .NET Methods” PowerTip of the Day. Read the article.

 

Find all static methods.

[net.dns] | gm -MemberType *method -static

 

Find all signatures (overload definitions).

[net.dns]::GetHostByAddress

 

One last thing, if you’re looking for a good reference list of static methods that are useful, pick up a copy of Windows PowerShell Cookbook, 3rd Edition, by Lee Holmes.

Feb 3, 2015

Importing a RSA Token on Windows Phone

I recently (this morning) had the good fortune of having to recover my Windows Phone. I run a Lumia 920. It’s been rock solid but recently have had stability issues. It kept locking up and rebooting itself randomly. This morning, it locked up. I rebooted it, and it locked up again. Finally, I rebooted it once more and got the light blue screen of death with the frowny face.

While thinking about all of the apps I would have to go in and configure, the one I dreaded most was getting my RSA token reconfigured. Why? I didn’t bother to write down the steps the last time I went through it. Now, I will remedy that problem.

Here we go.

  • Get the RSA SecurID app from the Windows Phone Store HERE.
  • The next thing you need to do is to install the RSA SecurID Software Token Converter. (All you really need is the TokenConverter.jar file.)
  • Next, however you do it, request a token.
  • Once you got the token, put the token (usually ends in .stdid) and the TokenConverter.jar file into the same directory.
  • Next, open a command prompt and navigate to the directory you put the files in.
  • Type the following command:

java.exe -jar TokenConverter.jar .\myToken.sdtid -winphone -o .\myFile.txt

  • If this fails, your administrator might have established a password on the token. Add the password using the -p switch and run the command again.
  • Upon success, the myFile.txt will contain the path you need for your RSA SecurID app. The file contents will look something like this:

com.rsa.securid://ctf?ctfData=longstringofnumbers

Grab that path. You’re going to need it for your app. All you have to do now is open the app and put the path in there.

Hit the checkbox. You’re good to go.