0

So I'm looking for a way to convert .ps1 scripts(using an online tool or whatever that works) to string format so that I can just copy and paste it into my C# code in a string variable.

This will enable me to stop referencing the paths of my scripts in the code as these may change from one computer/user to another.

Thanks!

Vishal Beep
  • 1,873
  • 1
  • 10
  • 25
  • 1
    I would suggest you put the script in some shared folder and read the file from C# code. And define the shared folder path in config file so that it will be easy to change it later – Vivek Nuna Jan 30 '22 at 12:03
  • 2
    I think you better convert it to a base64 string and execute it with `powershell.exe -EncodedCommand $encodedcommand` see https://devblogs.microsoft.com/scripting/powertip-encode-string-and-execute-with-powershell/ – Avshalom Jan 30 '22 at 12:04
  • 1
    Does this answer your question? [Convert PowerShell script into non-readable format](https://stackoverflow.com/questions/20943410/convert-powershell-script-into-non-readable-format) – iRon Jan 30 '22 at 12:06
  • @viveknuna Thanks, I thought about this, especially the fact that I could easily change it later. But in the case where the users are using a different computer, can a share file still be used? Sorry if the question is dumb I'm very new to this. – Shorty Beast Jan 30 '22 at 12:09
  • @Avshalom Thanks I'm gonna take a look into this – Shorty Beast Jan 30 '22 at 12:09
  • @iRon Thanks, this does what I want. Except for the fact that the returned string is unreadable I like this. – Shorty Beast Jan 30 '22 at 12:16
  • @ShortyBeast yes it can be. Or if your script is not going to change by others then you can Copy it to your bin by setting Copy if Newer property to true – Vivek Nuna Jan 30 '22 at 12:19
  • @viveknuna Thanks, I'll also try this – Shorty Beast Jan 30 '22 at 12:21
  • Actually, knowing that `.ps1` are just strings by itself, you might just consider to put them in a [`here string`](https://stackoverflow.com/questions/1100260/multiline-string-literal-in-c-sharp) – iRon Jan 30 '22 at 14:07
  • The following may be helpful: https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – Tu deschizi eu inchid Jan 30 '22 at 17:59
  • I think, using powershell files as embedded resources can solve your problem: https://stackoverflow.com/questions/44577716/net-get-embedded-resource-file – Yevhen Cherkes Jan 30 '22 at 23:03
  • I would rather use relative paths. As long as the relative position of the script against your working directory stays the same changes in the absolute position does not matter. – Gerhard Jan 31 '22 at 06:50
  • Copying scripts also violates the [Dry principle](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself). – Gerhard Jan 31 '22 at 06:53

1 Answers1

1

base64 encode and decode

I would use this solution: https://www.base64encode.org/ You can also test the string... with the decode link: https://www.base64decode.org/

I am sure in C# you have also base64decode to reconvert into PS1.

Mike
  • 229
  • 1
  • 4