1

I have below powershell commands,

$tmp = Get-Content 'C:\Users\username\Documents\emp_details.txt'
$tmp[0..75], ':tech emp details', $tmp[76.. ($tmp.Count -1)] | 
    Set-Content 'C:\Users\username\Documents\emp_details.txt'

which I want to encode and this entire commands as string and store it to variable and then will decode and execute it whenever I needed. But when I tried to decode using below which internally executes the Get-Content command as well.

$Bytes = [System.Text.Encoding]::Unicode.GetBytes("$tmp = Get-Content 'C:\Users\username\Documents\emp_details.txt'
$tmp[0..75], ':tech emp details', $tmp[76.. ($tmp.Count -1)] | 
    Set-Content 'C:\Users\username\Documents\emp_details.txt'")
$EncodedText =[Convert]::ToBase64String($Bytes)
$EncodedText

Any idea please to encode those commands as strings without executing those commands internally.

2
  • 1
    Look into assigning it as a script block to a variable. Commented May 6, 2017 at 15:28
  • On another note, the first code block appears to be reading and writing to the same file. Has this worked well? Commented May 6, 2017 at 16:55

1 Answer 1

2

Phil's suggestion of making a script block is a good one. Here is how to store the command into a variable $thecmd.

$thecmd = {$tmp = Get-Content "C:\Users\$Env:USERNAME\Documents\$emp_details.txt"
    $tmp[0..75], ':tech emp details', $tmp[76.. ($tmp.Count -1)] | 
    Set-Content "C:\Users\$Env:USERNAME\Documents\emp_details_out.txt"}

The value of $thecmd will be the scriptblock text. In order to run it, use the invocation operator (&).

& $thecmd

I changed your hardcoded path to use the $Env:USERNAME variable. Also, what happens if the source text file does not have at least 75 lines?

Sign up to request clarification or add additional context in comments.

6 Comments

Am pretty sure the content would be there on all file, since it was copied myself only but missed to add one line. It works perfect. But its not working when I try to encode and decode and then execute it. Below the syntax which I attempted $readbytes = [System.Text.Encoding]::Unicode.GetBytes($thecmd) $ecryptString =[Convert]::ToBase64String($readbytes) $decryptString = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($ecryptString)) & $decryptString
Getting below error, The term '$tmp = Get-Content "C:\Users\$Env:USERNAME\Documents\$emp_details.txt" $tmp[0..75], ':tech emp details', $tmp[76.. ($tmp.Count -1)] | Set-Content "C:\Users\$Env:USERNAME\Documents\emp_details_out.txt"' is not recognized as the name of a cmdlet, func tion, 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.
I do not see the invocation operator before $thecmd. Please edit code into the question. It is too difficult to read in a comment.
Found the issue, When we encrypt and decrypt the ScriptBlock is converted to String type, so had to convert back to ScriptBlock to make it work, Thanks
My guess is that the invocation operator (`&) converts the string to a scriptblock. It would be helpful if you could edit the working code into the question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.