Author Posts

April 24, 2014 at 11:05 am

So I'm writing a script to generate a list of random passwords. Once the list is generated they are output to a .txt file. They are in a very simple list like so:
pass1
pass2
pass3
pass4

nothing else.
What I need to happen is for a separate script to read this file, grab the first line and push the password to active directory. Once it's read the password and its all set it should delete what it read from the text file. So then it should read as such:

pass2
pass3
pass4

So this raises a few questions. First of all can I read and delete the line to begin with. Secondly if I delete that line, will it leave empty space? Third, If it leaves empty space can that space be deleted so that the second line then becomes the first line? and what commands/code would be used to accomplish this?

Any insight to this would be incredibly helpful. Note: This isn't anything major and I don't expect anyone to jump on it immediately as I have plenty of time to do this.

April 24, 2014 at 11:07 am

Get-Content source.txt | where { $_ -ne 'pass1' } | Out-File dest.txt

You're not "deleting" a line – you're reading in the lines, discarding the one you don't want, and writing out the remaining lines. So, no empty space.

April 24, 2014 at 11:08 am

I almost forgot to mention all the code necessary for changing the password in AD is already written. All I need is a snippet that will read a file in this location:
C:\Scripts\FuturePass.txt
grab the first line pass it into a variable called $SunPass to be set as the password in AD.

April 24, 2014 at 11:10 am

Ah I see. so here is my question. These two scripts will run perpetually can the "dest.txt" be the same file as the source?
In other words can I pull passwords from "FuturePass.txt" and then overwrite "FuturePass.txt" with the new version that excludes the password that was used?

April 24, 2014 at 11:18 am

You'd have to read the entire file into memory first.

$lines = Get-Content source.txt
$first = $lines[0]
$lines | where { $_ -ne $first } | out-file source.txt

Probably a hundred ways to do it. That's one.

April 24, 2014 at 11:27 am

Holy S****! If there is a god you are him! Thank you Thank you! This should work. Us jones' gotta help each other out, right?

Cheers

April 24, 2014 at 12:14 pm

One last question. Not that big of a deal, but I have no clue what "-ne" does...it kind of looks like it could be "Not Equals" or something but I have no clue. Just curious..my code is all done anyhow.

April 24, 2014 at 12:45 pm

Yes, it means "not equal". You can find a list of comparison operators by typing "help about_comparison_operators".

April 25, 2014 at 4:04 pm

Here's another variation ...

#1..100 | foreach { "pass$psItem" } >> source.txt
$pass = Get-Content .\source.txt -TotalCount 1
$newList = Get-Content .\source.txt | select -Skip 1
$newList | Out-File .\source.txt