0

I am trying to run below powershell script and passing $project_name in --project parameter but that is not accepting and not referring to variable. How would i use that ?

$project_name = "webapp_deploy1"

$command = "C:\Program Files\Octopus Deploy\Octopus\Octopus.Migrator.exe"

$param = @('partial-export','--project=$project_name','--password=deploy1','--directory=D:\Export Project','--ignore-history','--ignore-deployments','--ignore-tenants','--ignore-certificates','--ignore-machines')

& $command $param 
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1) Please use code formatting (indent 4 spaces). 2) How do you know your command line is not working? Do you get an error? (Remember: We can't see your screen.) – Bill_Stewart Aug 08 '19 at 18:22
  • I am getting below error Octopus.Migrator.exe : The following projects were not found: '$project_name' At line:7 char:1 + & $command $param + ~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (The following p...'$project_name':String) [], RemoteExceptio n + FullyQualifiedErrorId : NativeCommandError – brijesh kalavadia Aug 08 '19 at 18:26
  • For an overview of string literals and string expansion in PowerShell, see the bottom section of [this answer](https://stackoverflow.com/a/55614306/45375). – mklement0 Aug 08 '19 at 20:21

2 Answers2

1

Don't use single quotes if you want variables to be expanded. use "

"--project=$project_name"
G42
  • 9,791
  • 2
  • 19
  • 34
  • The `"` characters shouldn't be necessary. – Bill_Stewart Aug 08 '19 at 19:15
  • @Bill_Stewart Actually, I don't think powershell normally interprets variables when it looks like a parameter, unless there's a colon. – js2010 Aug 08 '19 at 19:40
  • 1
    Not sure why you say that, but it's not correct. I just tested again with my `showargs.exe` utility (it outputs the actual command line that PowerShell passes to it), and `--param=$arg` (where `$arg` contains a string) works just fine. If `$arg` contains spaces, PowerShell quotes the argument. – Bill_Stewart Aug 08 '19 at 20:11
  • Correct, @Bill_Stewart, but do note that the OP defines the parameter as a _string_ as part of an _array_, where quoting is indeed needed. – mklement0 Aug 08 '19 at 20:15
  • @js2010: The problem only occurs with a _single_ leading `-` followed by a letter; Try `Write-Output --project-name=$HOME`, and you'll see that it works fine. – mklement0 Aug 08 '19 at 20:16
  • I might have been thinking of start-process. – js2010 Aug 08 '19 at 20:18
  • My initial comment on this answer is related specifically to this answer. – Bill_Stewart Aug 08 '19 at 21:35
  • @Bill_Stewart: That's unfortunate, because taken _in isolation, as a stand-alone statement_ - the surrounding `"..."` are clearly necessary. _In the context of answering the question_, they are _also_ necessary, as explained in my previous comment. If your comment was meant to point out that _as a direct command argument_ something like `--project=$project_name` doesn't require double-quoting: it lacks that important qualification. – mklement0 Aug 08 '19 at 23:06
  • @mklement0: I didn't think this was confusing. My comment clarified. Not sure what else I can say... – Bill_Stewart Aug 09 '19 at 03:34
  • @Bill_Stewart, your original comment talked about a scenario different from the OP's - direct argument passing vs. the pass-arguments-by-array from the question - without explaining that context switch. Your follow-up comment built on that unspoken context switch and discussed a specific scenario in response to js2010's assertion. My comment was meant to highlight how different things were being talked about without proper framing, and provided that framing. I think there's now enough information in this exchange to avoid confusing future readers. – mklement0 Aug 09 '19 at 20:18
  • Sure, glad you think so. – Bill_Stewart Aug 11 '19 at 21:20
0

I don't suppose you could run it this way.

$project_name = 'webapp_deploy1'
& "C:\Program Files\Octopus Deploy\Octopus\Octopus.Migrator.exe" partial_export --project="$project_name" --password=deploy1 --directory='D:\Export Project' --ignore-history --ignore-deployments --ignore-tenants --ignore-certificates --ignore-machines
js2010
  • 23,033
  • 6
  • 64
  • 66
  • I am able to run this and it works as well but i want to pass variable in --project=$project_name instead of providing actual project name (Web_deploy1) – brijesh kalavadia Aug 08 '19 at 18:29
  • Ok, I changed it. – js2010 Aug 08 '19 at 18:31
  • This works $project_name = "webapp_deploy1" & "C:\Program Files\Octopus Deploy\Octopus\Octopus.Migrator.exe" partial-export --project="$project_name" --password=deploy1 --directory='D:\Export Project' --ignore-history --ignore-deployments --ignore-tenants --ignore-certificates --ignore-machines – brijesh kalavadia Aug 08 '19 at 18:39