[英]How to get shell script to work on gcloud CLI in PowerShell?
I've installed the gcloud CLI according to https://cloud.google.com/sdk/docs/install .我已经根据https://cloud.google.com/sdk/docs/install安装了 gcloud CLI。
When using cloud shell on browser, I could simply paste a script and it would work.在浏览器上使用云 shell 时,我只需粘贴一个脚本即可。 But it won't do the same when using cloud CLI on Powershell.
但在 Powershell 上使用云 CLI 时,它不会做同样的事情。
Script:脚本:
# List Projects accessible to these credentials
PROJECTS=$( `
gcloud projects list `
--format="value(projectId)")
# Iterate over each Project
for PROJECT in ${PROJECTS}
do
echo "Project: ${PROJECT}"
# Check Compute Engine service
ENABLED="$( `
gcloud services list `
--project=${PROJECT} `
--filter=config.name=compute.googleapis.com `
--format='value(state)')"
# Is it enabled?
if [ "${ENABLED}" = "ENABLED" ]
then
# Enumerate Disks that have `users` and output `name`
gcloud compute disks list `
--project=${PROJECT} `
--filter="-users:*" `
--format="csv(name,sizeGb,zone,status,type,lastAttachTimestamp,lastDetachTimestamp)"
fi
done
Result on browser cloud shell: successfully iterated through projects and listed disks in that project.浏览器云shell上的结果:成功迭代项目并列出该项目中的磁盘。
Result on Powershell: Powershell 的结果:
PS C:\WINDOWS\System32> C:\Users\minh.tran\Documents\Get Disk.ps1
At C:\Users\minh.tran\Documents\Get Disk.ps1:7 char:4
+ for PROJECT in ${PROJECTS}
+ ~
Missing opening '(' after keyword 'for'.
At C:\Users\minh.tran\Documents\Get Disk.ps1:8 char:3
+ do
+ ~
Missing statement body in do loop.
At C:\Users\minh.tran\Documents\Get Disk.ps1:17 char:5
+ if [ "${ENABLED}" = "ENABLED" ]
+ ~
Missing '(' after 'if' in if statement.
At C:\Users\minh.tran\Documents\Get Disk.ps1:17 char:7
+ if [ "${ENABLED}" = "ENABLED" ]
+ ~
Missing type name after '['.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword
PS C:\WINDOWS\System32>
The comment from @John Hanley is the correct answer. @John Hanley 的评论是正确答案。 I tried to use a Linux shell script as a PowerShell script on a Windows machine.
我尝试在 ZAEA23489CE3AA9B6406EBB28E 机器上使用 Linux shell 脚本作为 PowerShell 脚本。
The simplest solution for my case is to convert the shell script to a PowerShell script and run the converted script from PowerShell.对于我的情况,最简单的解决方案是将 shell 脚本转换为 PowerShell 脚本,然后从 PowerShell 运行转换后的脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.