-2

This is a sample of text including symbols that I am trying to overwrite a file with:

Home        http://domain.tld
IP|http://domain.tld/path/1
Navbar, Top|http://domain.tld/
Tips/Tricks|#some text
Accordion 2|*more text
Text|=More Text

substituting all meta-characters, e.g.,

s!\t!symTA!g
s!\#!symHA!g

This is my Perl code:

`$ps New-Item $dir -Name $file -ItemType "file" -Force -Value "$out"`

It works if $out is just one line without any spaces, though!

But it is about 100 lines!

It is giving a variety of errors similar to the following:

The term BlogsymPIhttpsymCOLsymFSsymFSbislinks.comsymFSblog is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:2 char:1 + BlogsymPIhttpsymCOLsymFSsymFSbislinks.comsymFSblog

The file is saved but with just the first line (with substitutions).

Borodin
  • 126,100
  • 9
  • 70
  • 144
bislinks
  • 84
  • 10
  • 2
    This is very unclear, and the "Perl code" that you show is certainly not Perl. Please expand on what you're actually doing. Where do `$ps` and `$out` come from (i.e. what is the code that populates them)? – Borodin Nov 09 '17 at 18:13
  • $ps New-Item $dir -Name $file -ItemType "file" -Force -Value @"\n$out\n"@; – Neossian Nov 09 '17 at 18:15
  • 1
    @Neossian: If you know what this means then please explain. – Borodin Nov 09 '17 at 18:20
  • @Neossian @"\n$out\n"@ did not work either. Just to clarify, $out includes multiple lines – bislinks Nov 09 '17 at 18:21
  • @Borodin, $ps is C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe and $out is/are the lines that I mentioned in the beginning of my qeustion – bislinks Nov 09 '17 at 18:22
  • @bislinks: You need to go into much more detail. What you think is Perl most certainly isn't, and I want to know where that line comes from: do you type it in, or is it the contents of a file, or part of a file? Which file? What do you do at the console to get this result? – Borodin Nov 09 '17 at 18:23
  • *"`$out` is/are the lines that I mentioned in the beginning of my qeustion"* Okay, so what put those lines into `$out`? – Borodin Nov 09 '17 at 18:23
  • Bislinks, try putting a line feed before and after the text in $out and then use $ps New-Item $dir -Name $file -ItemType "file" -Force -Value @"$out"@ – Neossian Nov 09 '17 at 18:25
  • @Neossian: None of this helps unless you specify what problem you're solving. – Borodin Nov 09 '17 at 18:26
  • @bislinks: For instance, your "Perl code" doesn't do the substitutions that you describe. – Borodin Nov 09 '17 at 18:29
  • 3
    Hi Borodin, bislinks is attempting to call Powershell from a perl script/interface. The command is being converted to straight OS execution. $Ps is the equivalent of powershell.exe so the question is really, how can we feed multiline string data into powershell.exe commandline call. It seems to be taking the the multiline data but executing every line in the in the multiline string. So surrounding it in block quotes should pass the string value. I used \n because I found that was the escape character for new line in perl. – Neossian Nov 09 '17 at 18:29
  • He could generate the full script he wants to run, convert it to B64 and pass it into powershell.exe using -EncodedCommand – Neossian Nov 09 '17 at 18:31
  • @Neossian: So you're saying that the line of "Perl code" is actually enclosed in `qx/.../`, or perhaps `system("...")`? I think we need to see the whole program to be certain, and it seems strange to omit those parts of a Perl statement. – Borodin Nov 09 '17 at 18:33
  • 1
    Yes. I had back ticks around the Perl code but I was unable to post question with the back ticks. – bislinks Nov 09 '17 at 18:37
  • @bislinks: Okay I've fixed your question. Is it correct now? Stack Overflow uses [**Markdown**](https://daringfireball.net/projects/markdown/syntax) and you should learn to use it before posting here. But there ***must*** be more to the Perl code than you show, as `$ps`, `$dir`, `$file` and `$out` need to be assigned. Please show the entire Perl file. – Borodin Nov 09 '17 at 18:42
  • You removed the word overwritten. I actually meant that it does overwrite the existing file which is what I want! That's the only problem with your correction. The rest is fine. – bislinks Nov 09 '17 at 18:47
  • Does this work? `$out = "@\"\n$out\n\"@"` ```$ps New-Item $dir -Name $file -ItemType "file" -Force -Value $out``` Sorry I don't have perl to test myself. – Neossian Nov 09 '17 at 18:51
  • Not sure if it can help in your situation, but maybe check [PowerShell](http://search.cpan.org/~ltheisen/PowerShell-1.00/lib/PowerShell.pm) on cpan. – jira Nov 09 '17 at 18:52
  • That did not help either. Days ago, I tried to install PowerShell on this windoes 10 machine, it failed. – bislinks Nov 09 '17 at 18:57
  • @bslinks: I removed "overwritten" because you use it at the top of your question, and because "saving" a file summarises what you're doing: it's irrelevant whether the file already exists. – Borodin Nov 09 '17 at 19:41
  • There's no way we can help you if you won't give us the information we ask for. It makes a *big* difference that there are really backticks around what you said was Perl code, and now that we've got past that I've asked twice how the other variables are populated and also to see the entire Perl code, and you haven't answered either. I'm not surprised it has taken you days to get this working. No one can help you if you don't answer requests for information. – Borodin Nov 09 '17 at 19:45
  • 1
    I have a very big problem believing that you're a web host designer and public speaker. – Borodin Nov 09 '17 at 19:45

2 Answers2

0

Please note that the maximum length of a command line string is around 8k characters, so this is a terrible general purpose solution. A better solution would be to write the entire command to a file and then invoke it as a script - I will assume this is not possible for some reason - otherwise you would probably just use perl to write the file - or use powershell to do the work.

I tested this in strawberry perl 5.26.1.1

use MIME::Base64 ();
use Encode qw/encode decode/;
use warnings;

$ps = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
$file = "c:\\strawberry\\test.txt";
$out = "Home        http://domain.tld
IP|http://domain.tld/path/1
Navbar, Top|http://domain.tld/
Tips/Tricks|#some text
Accordion 2|*more text
Text|=More Text";

$Command = "New-Item -path \"$file\" -ItemType file -Value @'\n$out\n'@";
$bytes = encode("UTF-16LE",$Command);
$encoded = MIME::Base64::encode($bytes);
$encoded =~ s/\R//g;
print "$ps -EncodedCommand $encoded";
$output = `$ps -EncodedCommand $encoded`;
print $output;

Good Luck!

Neossian
  • 695
  • 4
  • 14
  • But yesterday, I reverted to Perl ( which is soooooooo easy for me) and gave up on using Power Shell for this particular task. Nevertheless, will give it a try! Appreciate your hard work and input – bislinks Nov 10 '17 at 18:52
-1

Why are you using perl instead of straight PowerShell?

ex.txt

Home        http://domain.tld
IP|http://domain.tld/path/1
Navbar, Top|http://domain.tld/
Tips/Tricks|#some text
Accordion 2|*more text
Text|=More Text

ex.ps1

(Get-Content -Path '.\ex.txt') -replace 'regex','desired value' |
    Set-Content -Path '.\ex.txt'

This will parse each line in your document and attempt to match/replace your regex with the desired. If you don't want to pollute your original file you can do the following:

$Value = (Get-Content -Path '.\ex.txt') -replace 'regex','desired value'
New-Item -Path '.\pollute.txt' -Value $Value -ItemType 'File' -Force
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • 1
    *"Why are you using perl?"* Perhaps because the Perl program needs to do a whole lot more than run a PowerShell command? – Borodin Nov 09 '17 at 18:28
  • 1
    @Borodin Those things can be done within PowerShell, or within perl. It doesn't make a lot of sense to use two languages to do a task that can be completed with one. You create a confusing cascade of scripts and dependencies. – Maximilian Burszley Nov 09 '17 at 18:29
  • What things? We haven't seen a whole Perl program and have no way of knowing what's in it. What the OP has shown isn't valid Perl. Although I agree that creating a new file from a template is well within Perl's capabilities. – Borodin Nov 09 '17 at 18:30