Solved

Powershell

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article will help you understand what HashTables are and how to use them in PowerShell.
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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