-Contains Help

by Upinit at 2013-01-22 18:12:31

I come from the Perl world and am getting started with Powershell. I’m trying to use the -contains function and I can’t seem to get it working. My script is taking an input file in the form of:

distinguishedname LastLogonDate
----------------- -------------
CN=Vader, Darth (PMD),OU=PMD,OU=WC,OU=Consultants,DC=acme,DC=com
CN=Skywalker, Luke (MSC),OU=PMD,OU=WC,OU=Consultants,DC=acme,DC=com
CN=Solo, Han (PMD),OU=PMD,OU=WC,OU=Consultants,DC=acme,DC=com 6/16/2011 7:11:09 AM
CN=Bauer, Jack (PMD),OU=PMD,OU=WC,OU=Consultants,DC=acme,DC=com 9/18/2012 2:59:09 PM

I am parsing through it and splitting each line to grab just the date and time fields which I am then using to get the number of days since they have logged on. I have it working except when I try to add some extra logic into it.

My issue is as you can see in the input file not everyone has a date and time field associated with them. I want my script to not to perform the splitting and date comparison function for those users. I am using -contains but it doesn’t appear to work for me as depending whether or not I use -contains or -notcontains its doing an all or nothing function.

Here is what I have, note I have some added code for my own debugging purposes. When I run it as is I am getting the flag as "no" for all line entries even though 2 of them have the : in them as they have date and time associated to them. Can someone direct me to what I am doing wrong?

$a = get-content c:\scripts\activedirectory\expired_users.txt
$date1 = Get-Date
foreach ($line in $a) {
$new = $null
write-host "====================="
write-host "$line"
if ($line -contains ":") {
$split = $line -split "=com"
$new = $split[1].Trim()
echo xxx $new xxx
$flag = "Yes"
$date2 = Get-Date $new
$num = ($date1 - $date2).Days
echo $num
} else {
$flag = "No"
}
echo $flag
}
by Klaas at 2013-01-23 04:30:14
since "$line" is a string, you can use $line -like ":" or $line -match ":".
‘-contains’ is used to check for an element in a collection.

DonJ wrote this:
http://www.windowsitpro.com/blog/powershell-with-a-purpose-blog-36/windows-powershell/powershell-141421