Trying to get random line, store it, then delete it

Welcome Forums General PowerShell Q&A Trying to get random line, store it, then delete it

Viewing 4 reply threads
  • Author
    Posts
    • #244316
      Participant
      Topics: 5
      Replies: 10
      Points: 87
      Rank: Member

      Title loosely explains my goal…

       

      Have a document with random information inside. I want to grab it, store it in a variable, then remove it from the document. I’ve seen on this forum and other posts most people mention it’s best to just re-save the document without the information that was grabbed. I’ve attempt to get it to work for my purpose, but it’s not really coming through the right way.

       

       

      For example, I run it. I get 3. Then it re-saves the document. The next available options within the document are 1, 2, and 4. Rinse and repeat as long as I want.

      It seems to work if I make it a new file name, but the point is to keep the list intact as it is.

       

      Thanks in advance.

    • #244325
      Participant
      Topics: 15
      Replies: 1765
      Points: 3,178
      Helping Hand
      Rank: Community Hero

      You are doing everything in the same pipeline and it’s keeping the file open and then you are trying to write to it while Get-Content has it open.

      Back in VBSCript days when you opened a file, you had to specify a mode to open the file or it would open in Read-Only locked mode. Just move the SET of the content out of the pipeline:

    • #244337
      Participant
      Topics: 5
      Replies: 10
      Points: 87
      Rank: Member

       Just move the SET of the content out of the pipeline:

      Thanks!

    • #244520
      Participant
      Topics: 12
      Replies: 38
      Points: 282
      Helping Hand
      Rank: Contributor

      It should be possible to do it in the pipeline as well with a slight tweak.

      This grabs the content of the file and unloads it – that’s the reason for the parenthesis, the Foreach-Object then iterates over the content of the file and saves it to the same file name as before, all in the pipeline.

    • #244526
      Participant
      Topics: 15
      Replies: 1765
      Points: 3,178
      Helping Hand
      Rank: Community Hero

      Thanks for sharing KLaage. Thought it was interesting that it even kept the file open when you couldn’t use Set-Content or something to write back to it, but when in doubt Please Excuse My Dear Aunt Sally (PEMDAS) 🙂

Viewing 4 reply threads
  • You must be logged in to reply to this topic.