Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy

Jelmylicious posted:

My turn for a question:

I will be giving my companies internal Introduction to Powershell course. It will be a two day course for anyone ranging from "never touched powershell" to seasoned sysadmins that know VB by heart, but want to get to know powershell better. (oh god, what am I getting myself into? :ohdear:)
The current iteration of the course is a bit to dry and doesn't allow for that much input from the students about what they want to learn. I am going to make this course more hands-on and practical. My question to you is:
Is there anything that you ever ran into, while using powershell, where you thought to yourself: "If only I had known this when I started out, this would have saved so much time!"
Help me make this course awesome, so my newly made minions can help answer questions in this thread.

One thing that may appeal to your audience is the much cleaner (compared to VBScript) Active Directory integration, at least in my experience probably 3/4 of the Windows scripting I do involves AD in some way. You could mention either the .NET AccountManagement namespace or the wealth of cmdlets available in 2008 R2.

Adbot
ADBOT LOVES YOU

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy

Wicaeed posted:

How would one discover if a file was created on the first weekday in a month?

I can find a bunch of examples from years ago that use vbscript, but none do 100% what I need

code:
$info = Get-Item butts.txt
if($info.CreationTime.DayOfWeek -ne "Saturday" -and $info.CreationTime.DayOfWeek -ne "Sunday" -and $info.CreationTime.Day -lt 4)
{
    # it was the first weekday of the month
}
First we need to check if the creation time is not on a weekend, then make sure it's prior to the fourth day of the month (for the case where first day of the month is a Friday). Every other case should be covered but I haven't actually tested it.

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy

Mario posted:

What if the 1st is on a Monday? This would match Tuesday and Wednesday as well.

How about (again, not tested):
code:
$info = Get-Item butts.txt
if(($info.CreationTime.DayOfWeek -ne "Saturday" -and $info.CreationTime.DayOfWeek -ne "Sunday" -and $info.CreationTime.Day -eq 1)
-or
($info.CreationTime.DayOfWeek -eq "Monday" -and $info.CreationTime.Day -lt 4))
{
    # it was the first weekday of the month
}
Good catch, that sounds right.

Quebec Bagnet
Apr 28, 2009

mess with the honk
you get the bonk
Lipstick Apathy

Wicaeed posted:

Awesome, I was able to make that work with 99% accuracy. Actually all of that Saturday/Sunday stuff I don't even need since the folders are only created on Weekdays.

If I wanted to return a list of folders, but exclude any created in the previous month, how could I do that? I know I can filter my results with something like

code:
Where-Object {$_.CreationTime -lt (get-date).adddays(-31)}
But that would exclude the previous 31 days of results, which if it's halfway through the month, will start pruning records I don't want pruned.

Any ideas?

code:
Where-Object {$_.CreationTime.Month -ne ([System.DateTime]::Now.AddMonths(-1).Month -and $_.PsIsContainer }
Will list all objects that are directories (PS calls those containers for various reasons) whose creation time month is not equal to the current date's month-1. That also wraps around January->December correctly. Remember that Where-Object needs to be a filter, e.g. Get-ChildItem | Where-Object {...} | Format-List

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply