DEV Community

Bach Huynh V. VN.Danang
Bach Huynh V. VN.Danang

Posted on

Cách đưa biến môi trường thành một phần của một commandline ở PowerShell

Tôi gặp một bài toán như thế này:
Command tôi cần chạy:

.\octo.exe deploy-release --project ""ClientPortal"" --channel $ClientPortal_Channel --version=$ClientPortal_ReleaseNumber --deployto $ClientPortal_Env --ignoreSslErrors $ClientPortal_Tenant --progress --updateVariables
Enter fullscreen mode Exit fullscreen mode

Đặc biệt với $ClientPortal_Tenant là một chuỗi mà tôi muốn nó trở thành một phần của command trên.

Giá trị của $ClientPortal_Tenant là:
--tenant ABC --tenant ABD --tenant ABE --tenant ABX

Nhưng khi thực hiện, thì báo lỗi ngay, vì biến môi trường trong trường hợp này được hiểu như một argument, ko phải là một thành phần của command.

Để giải quyết, ta sử dụng lệnh Invoke-Expression, một command trong powershell cho phép thực thi lệnh từ một chuỗi có sẵn.

Sửa lại như sau:

$ClientPortal=".\octo.exe deploy-release --project ""ClientPortal"" --channel $ClientPortal_Channel --version=$ClientPortal_ReleaseNumber --deployto $ClientPortal_Env --ignoreSslErrors $ClientPortal_Tenant --progress --updateVariables"
> Invoke-Expression $ClientPortal
Enter fullscreen mode Exit fullscreen mode

Top comments (0)