0

I'm kind of new to Powershell, but now I need to build a script which takes a backup and encrypts it using Veracrypt. Fortunately there is a command-line syntax to do that, but for my script I'll use the Powershell's shell instead. I've managed to call CMD from within my Powershell script, but then I need to user a pre-configured variable along with the command-line command, which I previously set in Powershell. To make a sense for my request, here's how my script goes:

$Folder="e:\shares\Test"

$Size=(Get-ChildItem $Folder -Recurse | Measure-Object -Sum Length).Sum

& 'C:\Program Files\VeraCrypt\VeraCrypt Format.exe' /create e:\shares\Test\test.hc /password test /hash sha512 /encryption serpent /filesystem NTFS /size $Size

As you can see from above example, I need to make the Veracrypt command make use of the $Size variable for the /size flag. That's because I assume that my backup size will change due time.

Is therea way to call my Powershell variable from CMD?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • So what's wrong with your code at the moment? – gvee Feb 19 '18 at 10:25
  • The Veracrypt command fails because it can't read the size input for the volume creation. That's where I needed to use my variable to set the size. But seems like cmd doesn't recognize it. – Shachar Feb 19 '18 at 10:28
  • So if you hard-code a value instead of using your `$Size` variable; what happens? – gvee Feb 19 '18 at 10:50
  • Then the command is successful. But that miss the entire purpose of my script. My backup size will differ from one backup process to another... – Shachar Feb 19 '18 at 11:21
  • just trying to help you debug the solution `;-)` Does the value of `$Size` match the value you hard-coded? – gvee Feb 19 '18 at 11:33
  • Yes. But the thing is that its not recognized as a variable. Thats all my issue is...thanks for the help so far :) – Shachar Feb 19 '18 at 12:39
  • to be clear: `/size 10240` works, but `$Size=10240 ... /size $Size` does not?! – gvee Feb 19 '18 at 14:11
  • Correct! :) As I said the variable is not recognized by the cmd.exe interprator. – Shachar Feb 19 '18 at 14:24
  • Now that is maddeningly bizarre! Unfortunately that stumps me at the moment and, without an environment in which I can test your scenario, I don't think I can be of any more assistance. Sorry! – gvee Feb 19 '18 at 15:08
  • In your code snippet, `cmd.exe` is _not_ involved: you're simply invoking a _console application_ directly from PowerShell, and passing arguments to it. There are many pitfalls associated with passing arguments to console applications, but the code in your question _should_ work, so there must be something you're not mentioning in your question. – mklement0 Feb 20 '18 at 02:29
  • Thanks for the help. This was resolved after changing the source folder! sorry for all the bothering! – Shachar Feb 20 '18 at 07:37
  • @Shachar: Glad to hear it. I encourage you to _delete_ this question, then, given that it's unlikely to benefit future readers. – mklement0 Feb 21 '18 at 02:07
  • Dont know how to delete it :/ – Shachar Feb 22 '18 at 03:03

0 Answers0