Solved

Powershell

Posted on 2011-09-07
3
537 Views
Last Modified: 2012-05-12
I have a document.txt file that has over 100 lines in it. The following information is an example of how the information is setup in the file.
ComputerName=Person
LastLogin=Date
ComputerName=Person
LastLogin=Date

What I'm trying to achive is the following..a comma delimitied file but with the following output

Person,Date
Person,Date
Person,Date

I'm in the process of trying to write this code in powershell.

Currently I'm able to output a file that is similar to the example below, but I'm trying to matchh up the Person and the date and remove ComputerName and LastLogin information.  Below is an example of what I can currently output:

ComputerName,Person
LastLogin,Date
0
Comment
Question by:wnbtechnology
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 125 total points
ID: 36499520
Get-Content YourFile.txt | % {
  $value = $_.split("=")[1]
  if ($_.StartsWith("ComputerName=")) {
    $computer = $value
  } else {
    Write-Output ($computer + ', ' + $value)
  }
}

Open in new window

You can redirect the output to a file, of course.
0
 
LVL 10

Expert Comment

by:wls3
ID: 36502205
This can solve the issue of getting paired lines:
for($i = 0; $i -lt $file.Length; $i++) {
  "Lines: $i - $($i+1): " + $file[$i,$i+2]; 
  $i++
}

Open in new window

Using a file that contains this text:
param(
        $valuepassedasargument
)

# Display the argument
Write-Output "Param: $valuepassedasargument";

# Display the value by accessing the $args collection
Write-Output "`$args: $args";

# Display something just as a sanity check
Write-Output "Sanity check: Something";

# Wait for input to pause processing
read-host;

Open in new window

I get this output:
Lines: 0 - 1: param( param( )
Lines: 2 - 3: ) ) )
Lines: 4 - 5: # Display the argument # Display the argument )
Lines: 6 - 7:   )
Lines: 8 - 9: Write-Output "`$args: $args"; Write-Output "`$args: $args"; )
Lines: 10 - 11: # Display something just as a sanity check # Display something just as a sanity check )
Lines: 12 - 13:   )
Lines: 14 - 15: read-host; read-host; )

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 36502305
@wls:  Wrong thread???
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now