Solved

Powershell

Posted on 2011-09-07
3
535 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 68

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 68

Expert Comment

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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

895 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

18 Experts available now in Live!

Get 1:1 Help Now