1

I'm trying to do the following simple command in Windows 10 via Power-Shell CLI. How can I do that?

ID=test001 ./initializeID.sh

Where initializeID.sh is:

#!/bin/bash
echo "Initializing for ID: $ID"
DPPROG="{\"title\":\"Test \", \"Id\": \"$ID\"}"
curl -H "Content-type: application/json" -d "$DPPROG" http://localhost:8080/StorageApi/id

Currently if I run it, it shows this error:

ID=test001 : The term 'ID=test001' 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:1 char:1
+ ID=test001 ./initializeID.sh
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (ID=test001:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Tina J
  • 4,983
  • 13
  • 59
  • 125
  • 1
    Keep in mind you're not going to be able to just run a `.sh` script in Powershell. The shebang also isn't a thing in Windows either. – codewario Oct 14 '19 at 19:06
  • Please see the linked post and [this answer](https://stackoverflow.com/a/43030126/45375) for details. – mklement0 Oct 14 '19 at 19:16
  • 1
    Good point, @BendertheGreatest. Tina J, the syntax you're using only works in POSIX-like shells such as Bash. In PowerShell, `ID=test001` is being interpreted as the _name of command_, which explains the error message. If you also need to translate the script itself: look at `Invoke-RestMethod` and `Invoke-WebRequest` for `curl`-like functionality. – mklement0 Oct 14 '19 at 19:23
  • Even in linux I had problems running that. `./initializeID.sh` needs permission and cannot run without `bash`. But Idk how to pass the `ID=test001` then. – Tina J Oct 14 '19 at 20:10

1 Answers1

1

There isn't a way to do this directly. You could set the variable and then run the command like so:

$env:VARIABLE = 'somevalue'; some-command-or-script

but this will persist the envvar in the current powershell session that invoked some-command-or-script. You would need to either run Remove-Item ENV:VARIABLE after some-command-or-script exits or store off the original value before the line above, and restore it after the command completes.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • Even in linux I had problems running that. `./initializeID.sh` needs permission and cannot run without `bash`. But Idk how to pass the `ID=test001` then. Is there any workaround for that? – Tina J Oct 14 '19 at 20:11