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

RMAG news

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

Đặ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

Leave a Reply

Your email address will not be published. Required fields are marked *