Solved

Powershell

Posted on 2011-09-07
3
541 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 70

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 70

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell- Registry HIVE 6 63
PowerCli import module 2 50
PowerShell:  foreach where object notmatch? 17 76
Using powershell to move email to subfolder within outlook 3 37
Utilizing an array to gracefully append to a list of EmailAddresses
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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