powershell

Powershell related posts.
17
Aug

PowerShell searching scheduled tasks

Someone recently complained about the number of tasks listed in Windows 8.1 Scheduled Tasks and searching them for example, Windows Defender. Quick trip into PowerShell land arrived at

Get-ScheduledTask

hmmm
PS C:\Support> Get-ScheduledTask *defender*
 
TaskPath                                       TaskName                          State
--------                                       --------                          -----
\Microsoft\Windows\Windows Defender\           Windows Defender Cache Mainten... Ready
\Microsoft\Windows\Windows Defender\           Windows Defender Cleanup          Ready
\Microsoft\Windows\Windows Defender\           Windows Defender Scheduled Scan   Ready
\Microsoft\Windows\Windows Defender\           Windows Defender Signature Update Ready
\Microsoft\Windows\Windows Defender\           Windows Defender Verification     Ready

Yay!

Then I was asked.. How to find the exe that belonged to a Scheduled Task, MpCmdRun.exe (Windows Defender)... huh what? errr...

9
Aug

Lab Test Users

Setting up a test domain often requires some users. Since I am seeing a need to set up a test environment a few different times I figured I'd spend the time to automate it. I looked around for some names lists and figured I would just raid character names from the works of Jane Austin, it gives me some variety.

I just set the user password to the same thing and this script is set to read from user.txt in the same directory and enable the accounts.

$users = Import-Csv .\users.txt -Delimiter ','
$password = Read-Host "Enter password for all test users: " -AsSecureString
 
foreach($user in $users){
    $name = "$($user.First) $($user.Last)"
    New-ADUser -Name "$($user.First) $($user.Last)" -DisplayName $name -GivenName $($user.First) -Surname $($user.Last) -SamAccountName "$($user.First)$($user.Last)" -AccountPassword $Password -PasswordNeverExpires $true -Enabled $true -Description "User Test Account"
     }

1
Aug

PowerShell neat function tricks for free

Finished going through a recent NA TechEd2012 video Turn PowerShell Commands into Reusable CLI and GUI Tools by Don Jones.

Some of the advice I sort of knew and and followed.

  • I knew how to make a PowerShell script module.
  • I knew about the .SYNOPSIS, .EXAMPLE stuff.
  • I sort of knew about using the param/paremeters though I didn't really understand it, just used it in a limited framework copy/paste sort of way. After the video I have a much better handle on how to leverage it.

What I really got out of it though was some new and better ways to leverage built in switches more effectively which I had no real clue about.

  • Documenting not though #comments but using Write-Verbose so that the -verbose switch works and you can see what a script is doing without pulling it up in an editor.
  • useing write-debug to leverage the -debug switch for your functions
  • Using try/catch in your scripts so if a first part fails, then it skips the next parts on that one server.
  • A clear example on how to use Write-Object with a PSObject which I just used without understanding it.
  • Using $errorLogPath to create an error log

Below is the script he built for the presentation with the various examples. Save the below code as MyTools.psm1 and load it up. Then run it against multiple systems without switches and using the -debug and -verbose switches. Better yet, go watch the video.

Tags:
28
Jun

TechEd 2012 Videos are out

Microsoft provides some of the best free technical resources out there for self training and keeping aware of changes in their technology.

TechEd sounds like a great event which I have never gotten to go to, but through the magic of the Internet and Microsofts Channel 9, you have access to the presentations.

If you are new to PowerShell I recommend viewing them with Ed Wilson's video series.

TechEd North America 2012

1. Windows PowerShell Crash Course
2. 5 part set of PowerShell video's from Ed Wilson as well
3. Turn PowerShell Commands into Reusable CLI and GUI Tools

Then check out these as well.
PowerShell Remoting in Depth
Advanced Automation Using Windows PowerShell 3.0

This year is an insane year for change. The changes in Microsoft's technology is vast and looks to be seriously fun.

1
May

Using PowerShell to add numbers in a text file

Recently had a conversation where someone needed help adding up the last column in a tab separated text file. It looked like a calling plan report. In channel tons of people brought up all sorts of *nix based tools which I thought odd since it was a Windows based IRC channel.

I've occasionally had a need for something like this anyway so poked at it for a few minutes and came up with the below one liner which I will leave here so my bad memory can find it again.

import-csv .\test.txt -Header "date","something","type","cost" -delimiter "`t" | Measure-Object cost -sum
 
Count    : 12
Average  :
Sum      : 2.38
Maximum  :
Minimum  :
Property : cost

Since the text file doesn't have any header rows, you have to 'add them' through the import command.

Here is that test.txt file example.

14-Dec-11 	0-234 	Long Distance 	0.32
14-Dec-11 	0-960 	Long Distance 	0.04
09-Dec-11 	1-237 	Directory Assistance Call Comp 	1.25
09-Dec-11 	1-960 	Directory Assistance Call Comp 	0.15
22-Nov-11 	0-234 	Long Distance 	0.12
22-Nov-11 	0-960 	Long Distance 	0.01
16-Nov-11 	0-234 	Long Distance 	0.16
16-Nov-11 	0-960 	Long Distance 	0.02
12-Nov-11 	0-234 	Long Distance 	0.20
12-Nov-11 	0-960 	Long Distance 	0.02
11-Nov-11 	0-234 	Long Distance 	0.08
11-Nov-11 	0-960 	Long Distance 	0.01

Update:
A user (spade) in #PowerShell contributed this approach as well.

gc data2.txt | % {$sum=0} { $sum += ($_ -split "`t")[-1] } { $sum }

Tags:
23
Jan

PowerShell-Windows.Forms RDP Launcher

In a given environment there can be many short term solutions that over time grow into long term headaches. We have a situation where we need to provide access to RDP sessions through Citrix. The original solution by previous admins was to create a custom RDP icon configured to the server in question and publish it in Citrix. This resulted in a rather 'busy' screen in Citrix and intermittant update issue.

A co-worker asked about PowerShell doing this and I said something off handed about Windows Forms to generate a menu selection should be do-able. He found a nice Windows.Forms example and then did a hard coded elseif menu (which seemed as bad as all the links to me). I came up with an improvement where we pulled the server/ip list from a file and used Select and Eric Woodford added a nicer $choice option with the added bonus of turning the line for launching RDP into a variable. This is good because we have a similar issue with putty links. This should wipe out 80 some odd custom published Citirx apps.

Syndicate content