header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Help with Powershell syntax
Last Post 18 Oct 2007 08:28 PM by bsonposh. 8 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SpanosUser is Offline
New Member
New Member
Posts:5
Avatar

--
18 Oct 2007 05:51 PM  
I have  a file that I'm parsing.  I want to split each line on a regular expression match and then add those to a powershell object.  Basically, I'm turning text into a powershell object.

So my problem is that I can't get the syntax right to make it happen.

Here's my one liner:

gc $ResultsFile |% {$_ |? {$_ -match ': '}|% {$r = New-Object object}{$r | Add-Member -MemberType NoteProperty -Name ([regex]::Split($_,':\s{3,}'))[0] -Value ([regex]::Split($_,':\s{3,}'))[1]}}{$r}| Export-Csv $resultsFile1 -NoTypeInformation

The problem is that no new members or values are added. I know the regular expression is right because when I run:

gc $ResultsFile |% {$_ |? {$_ -match ': '}|% {[regex]::Split($_, ':\s{3,}')[0]}}

It returns the correct results. I've seen syntax that uses $_.split(':') to fill the name and value property, but the problem with that is that it doesn't give me enough flexibility with processing the file. I really need to split on a : followed by at least 3 spaces. Any suggestions?
bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
18 Oct 2007 06:01 PM  
Perhaps this will help http://bsonposh.com/modules/wordpress/?p=25

Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
SpanosUser is Offline
New Member
New Member
Posts:5
Avatar

--
18 Oct 2007 06:36 PM  
Here is where I get lost with your post:

I can create a new object no problem.  I'm trying to assign the name and the value from the split of a line in the file.   So, for example, the file contains data like this:

HostName:     SERVER1
TaskName:     1265
NextRunTime:     07:30:00, 10/19/2007

I want to make the name property HostName, TaskName, NextRunTime and the values for each corresponding item.  I want to split on the regex because a split on just a colon causes issues for other things.

So I need to find a way to make the line

$newobj | add-member -membertype noteproperty -Name (first value of a regular expression split) -Value (second value of a regular expression split)

How do I make that work syntax wise?
SpanosUser is Offline
New Member
New Member
Posts:5
Avatar

--
18 Oct 2007 06:54 PM  
I looked at the answer on the newsgroups as well.  Thanks for the help.  I'll just answer here from now on as the other site takes too long to load in my browser.

My file is a csv file.  Each line is parsed individually.  The data is like I described above.

I'm getting closer.  I started dividing things out to see if I can get it to work.  Here's a better example:

$working = gc $ResultsFile

foreach ($row in $working)

{

if ($row -match ': ')

{

$name = [regex]::Split($row, ':\s{3,}')[0]

$value = [regex]::Split($row, ':\s{3,}')[1]

$myobj = new-object System.Object

$myobj | Add-Member -MemberType noteproperty -Name $name -Value $value

$myobj |gm

}

}

The problem is that get member looks totally jacked up.  Probably created object in wrong place.  but names and values are right now.

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
18 Oct 2007 06:56 PM  
You mean something like


gc $file | foreach{
    ($_.Split(":"))[0].Trim()
    $r = "" | Select HostName,TaskName,NextRunTime
    $r.HostName = <your split here>
    $r.TaskName = <your split here>
    $r.NextRunTime = <your split here>
    $r
}
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
SpanosUser is Offline
New Member
New Member
Posts:5
Avatar

--
18 Oct 2007 07:21 PM  
($_.Split(":")) won't work for me because it will split on the time (07:00:01) and some of the names also contain mulitple colons with a space between them.

So my text file is literally:

HostName:                             SHHKRSP1
TaskName:                             1263
Next Run Time:                        07:30:00, 10/19/2007
Status:                              
Last Run Time:                        07:30:00, 10/15/2007
Last Result:                          0
Creator:                              kronosSQLagent
Schedule:                             At 7:30 AM every Mon, Fri of every 2 weeks, starting 10/3/2005
Task To Run:                          D:\Kronos\wfc\bin\TaskRunner.exe -e 1263
Start In:                             D:/Kronos/wfc\data
Comment:                              Event - Event Name: Time Detail - Pham

                                      Time Detail
Scheduled Task State:                 Enabled
Scheduled Type:                       Weekly
Start Time:                           07:30:00
Start Date:                           10/3/2005
End Date:                             N/A
Days:                                 MONDAY,FRIDAY
Months:                               N/A
Run As User:                          tmc\kronossqlagent
Delete Task If Not Rescheduled:       Disabled
Stop Task If Runs X Hours and X Mins: 72:0
Repeat: Every:                        Disabled
Repeat: Until: Time:                  Disabled
Repeat: Until: Duration:              Disabled
Repeat: Stop If Still Running:        Disabled
Idle Time:                            Disabled
Power Management:                     No Start On Batteries, Stop On Battery Mode

The code I wrote above seems to solve my problem.  I had to break it down.  Now I have to work on getting this to a CSV file.

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
18 Oct 2007 07:29 PM  
How does this work?
$objCol = @() ### Add this for storing the object
$working = gc $ResultsFile
foreach ($row in $working)
{
    if ($row -match ': ')
    {
        $name = [regex]::Split($row, ':\s{3,}')?]
        $value = [regex]::Split($row, ':\s{3,}')?]
        $myobj = new-object System.Object
        $myobj | Add-Member -MemberType noteproperty -Name $name -Value $value
    }
    $objCol += $myobj 
}
$objCol | export-Csv $exportfile -NoType


Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
SpanosUser is Offline
New Member
New Member
Posts:5
Avatar

--
18 Oct 2007 08:21 PM  

Export-csv : Cannot bind argument to parameter 'InputObject' because it is null. $objCol | export-Csv <<<< $exportfile -NoType

I added the object collection line inside the If statement and got very strange output.  Not sure it's useful for troublehooting.

I posted another clairification on the newsgroup of what I'm trying to do.

Thanks once again.  Your object collection idea jives with what I was thinking the problem is.  If I stick a write-host line in the if loop to display the name and value pairs, it definately works.  I just can't figure out the object part yet.

bsonposhUser is Offline
Basic Member
Basic Member
Posts:392
Avatar

--
18 Oct 2007 08:28 PM  
alrighty... we can continue there then :)
Brandon Shell
----------------
Microsoft Powershell MVP
https://mvp.support.microsoft.com/profile/Brandon
Blog: http://www.bsonposh.com
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer