Solved

I would like a Powershell equivalent of the following script

Posted on 2014-09-23
20
88 Views
Last Modified: 2014-10-09
I would like to convert the following script in Powershell.


Set fso = CreateObject("Scripting.FileSystemObject")

data = Split(fso.OpenTextFile("output.ed").ReadAll, "AIS*00*")
For i = 1 To UBound(data)
  fso.OpenTextFile("new" & i & ".txt", 2, True).Write "AIS*00*" & data(i)
Next
0
Comment
Question by:100questions
  • 10
  • 6
  • 3
  • +1
20 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40340764
$i = 1
$path = "X:\your\path"
(gc "$path\output.ed") -split "AIS*00*" | %{ 
    New-Item -ItemType file -Path "$path\new$i.txt" -Value "AIS*00*$_" -Force
    $i++
}

Open in new window

HTH,
Dan
0
 
LVL 16

Expert Comment

by:Learnctx
ID: 40341198
Being that split uses a regular expression the above won't work as it is expecting 0 or more repetitions of 'S' and '0', not 'S*' and '0*'. You will want to make sure you escape the asterisk/star.

(gc "$path\output.ed") -split "AIS\*00\*" | %{

Open in new window

0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40341278
Did not look at the search string :)

I can generalize the script so it does not matter if there are special characters in it:

###REPLACE WITH YOUR OWN DATA###
$path = "X:\your\path"
$searchString = "AIS*00*"
$i = 1

###DO NOT MODIFY BELOW###

$searchString = [regex]([regex]::Escape($searchString))
(gc "$path\output.ed") -split $searchString | %{ 
    New-Item -ItemType file -Path "$path\new$i.txt" -Value "$searchString$_" -Force
    $i++
}

Open in new window

0
 

Author Comment

by:100questions
ID: 40342004
Thanks Dan.  The script does not seem to work well, it created many .txt files when in fact the data I was using had only 2 sets of data.  Also all the txt files seem to be one liners etc..
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40342010
OK. Please attach a sample file, so I can test on your data.
0
 

Author Comment

by:100questions
ID: 40342021
Thanks Learnctx.  
This script does not work, it produces this issue:



Missing closing '}' in statement block.
At C:.....1:1 char:47
+ (gc "$path\output.ed") -split "AIS\*00\*" | %{ <<<<
    + CategoryInfo          : ParserError: (CloseBraceToken:TokenId) [], Parse
   Exception
    + FullyQualifiedErrorId : MissingEndCurlyBrace
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40342300
There was a missing the parameter: raw. Without it, get-content gets one line at a time
###REPLACE WITH YOUR OWN DATA###
$path = "X:\your\path"
$searchString = "AIS*00*"
$i = 1

###DO NOT MODIFY BELOW###

$searchString = [regex]([regex]::Escape($searchString))
(gc "$path\output.ed" -Raw) -split $searchString | %{ 
    New-Item -ItemType file -Path "$path\new$i.txt" -Value "$searchString$_" -Force
    $i++
}

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40344382
There seems to be an error in the get-content parameter names evaluation, at least for me (PS 4). I had to put -Raw as first switch / parameter to get it work:
 (gc -raw "$path\output.ed") -split $searchString | %{

I cannot prove http:#a40342021 - getting that error is probably due to changes you made we do not see.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40344394
Weird. I'm on PS4 also (win 8.1) and it worked with -raw at the end.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40344401
@Qlemo: I don't think the OP did any changes. He simply used that line (
(gc "$path\output.ed") -split "AIS\*00\*" | %{
) and the parser complained it found an opening curly brace without a matching closing one.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 68

Expert Comment

by:Qlemo
ID: 40344428
This is valid syntax. The parser knows there has to be more on following lines. It won't complain (yet) about a curly brace mismatch.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40344440
It will, if you paste the line in the PS console:
Missing closing '}' in statement block.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40344499
No. All you get is a continuation prompt:PS2-please-continue.png
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40344532
Not in PS ISE on my computer:
Windows-PowerShell-ISE.png
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40344572
Ahh, using ISE. Important point to know. Yes, the ISE prompt will require "complete" code, because it runs line by line, not allowing for splitting code over several lines.

Note: Don't try multi-line code in the ISE's PS Console, because it is not the same as in the script window or the non-ISE PowerShell prompt.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40344583
You just can't write multi-line code in ISE's PS Console. But pasting multiple lines of code works just fine.

Then again, in ISE you have a window above where to write the code...
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40344608
That's the point - there is no point in pasting it into the Console part :D
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40344619
You have to save the code to be able to run it from the script pane.

For a quick throwaway script it's faster (for me) to write it in the script pane and then just paste it in the console and run it.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40344667
Using PowerGUI, so not having those issues. It runs unsaved scripts with temporary names, and I don't have to bother about anything :p
0
 

Author Closing Comment

by:100questions
ID: 40371602
Unsure if this works, I might try it.   I need to ask more questions therefore I need to move on.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

9 Experts available now in Live!

Get 1:1 Help Now