Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell Google Cloud-使用存储在云存储上的启动脚本的gcloud脚本_Powershell_Google Cloud Platform_Gcloud - Fatal编程技术网

Powershell Google Cloud-使用存储在云存储上的启动脚本的gcloud脚本

Powershell Google Cloud-使用存储在云存储上的启动脚本的gcloud脚本,powershell,google-cloud-platform,gcloud,Powershell,Google Cloud Platform,Gcloud,在GCP云中,我想使用gcloud启动脚本部署并配置Windows服务器。我已经准备并测试了PowerShell脚本(用于Windows服务器配置)。我已将此PowerShell脚本上载到云存储中。我已经为VM部署准备了gcloud命令脚本(gcloud命令脚本已保存为.sh脚本)。我把这个.sh脚本上传到云Shell中。在.sh脚本中,我使用的是元数据启动脚本部分--metadata=startup script url=“gs://bucketName/Server Configuratio

在GCP云中,我想使用gcloud启动脚本部署并配置Windows服务器。我已经准备并测试了PowerShell脚本(用于Windows服务器配置)。我已将此PowerShell脚本上载到云存储中。我已经为VM部署准备了gcloud命令脚本(gcloud命令脚本已保存为.sh脚本)。我把这个.sh脚本上传到云Shell中。在.sh脚本中,我使用的是元数据启动脚本部分
--metadata=startup script url=“gs://bucketName/Server Configuration script.ps1
。在
服务器配置脚本.ps1
PowerShell脚本中,在srcipt参数下面,我有4个必须传递的参数

param (
    [Parameter(Mandatory=$true)][string] $paramA,
    [Parameter(Mandatory=$true)][string] $paramB,
    [Parameter(Mandatory=$true)][string] $paramC,
    [Parameter(Mandatory=$true)][string] $paramD
 )

我的问题是如何在我的.sh脚本中使用启动脚本部分
--metadata=startup script url=“gs://bucketName/Server Configuration script.ps1
中的所有四个参数?

我建议您在VM的元数据中设置参数的值

gcloud compute instances add-metadata instance-name \
    --metadata ParamA=4,ParamB=25,ParamC=1,ParamD="Windows Server 2016"
然后,在启动脚本中,您可以这样获得它们

paramA=$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA -H "Metadata-Flavor: Google")

编辑1

在windows机器上测试后,我发现了如何在启动时执行此操作

Invoke-RestMethod -H @{'Metadata-Flavor'='Google'} -Uri 'http://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA'
Curl是
Invoke WebRequest
的别名,需要对当前用户进行Internet Explorer配置的首次初始化。当您运行启动脚本时,它作为系统帐户运行,并且您没有为此用户配置IE


我不擅长在powershell。当我将输出重定向到一个文件时,该命令起作用,如
>c:\Windows\Temp\paramA.txt
。使代码适应比我更好的PS脚本

我建议您在VM的元数据中设置参数的值

gcloud compute instances add-metadata instance-name \
    --metadata ParamA=4,ParamB=25,ParamC=1,ParamD="Windows Server 2016"
然后,在启动脚本中,您可以这样获得它们

paramA=$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA -H "Metadata-Flavor: Google")

编辑1

在windows机器上测试后,我发现了如何在启动时执行此操作

Invoke-RestMethod -H @{'Metadata-Flavor'='Google'} -Uri 'http://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA'
Curl是
Invoke WebRequest
的别名,需要对当前用户进行Internet Explorer配置的首次初始化。当您运行启动脚本时,它作为系统帐户运行,并且您没有为此用户配置IE



我不擅长在powershell。当我将输出重定向到一个文件时,该命令起作用,如
>c:\Windows\Temp\paramA.txt
。使代码适应比我更好的PS脚本

启动VM之前您知道参数吗?是的,但我不想使用值硬编码的im powershell脚本。我个人更喜欢计算引擎自定义元数据,而不是硬编码值。这使您能够更灵活地在其他VM上重用相同的脚本,而无需更改脚本的内容。有没有理由硬编码该值?您会在其他具有不同参数的VM上使用相同的脚本吗?正如我提到的,我不会使用硬编码值。在我存储在云存储桶中的PowerShell代码中,我有四个参数,如上文所列。在我的cloud shell gcloud命令中,我想使用元数据部分
--metadata=启动脚本url=“gs://bucketName/Server-Configuration-Script.ps1-paramA-paramB-paramC-paramD
,但我不知道如何创建此命令,如何传递paramA-paramA…D。如果传递启动脚本url,则无法提供参数。这就是为什么您需要在其他地方提供它们,例如在元数据中启动VM之前您知道参数吗?是的,但我不想使用值硬编码im powershell脚本。我个人更喜欢计算引擎自定义元数据,而不是硬编码值。这使您能够更灵活地在其他VM上重用相同的脚本,而无需更改脚本的内容。有没有理由硬编码该值?您会在其他具有不同参数的VM上使用相同的脚本吗?正如我提到的,我不会使用硬编码值。在我存储在云存储桶中的PowerShell代码中,我有四个参数,如上文所列。在我的cloud shell gcloud命令中,我想使用元数据部分
--metadata=启动脚本url=“gs://bucketName/Server-Configuration-Script.ps1-paramA-paramB-paramC-paramD
,但我不知道如何创建此命令,如何传递paramA-paramA…D。如果传递启动脚本url,则无法提供参数。这就是为什么您需要在其他地方提供它们,例如在元数据中,所以您建议将这个curl请求-->
paramA=$(curlhttp://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA -H“Metadata Flavor:Google”)
到我的启动PowerShell脚本中?是的,不需要外部参数,要在脚本开始时初始化它们,我使用了启动PowerShell脚本-->
$ParamA=$(curl)中提到的参数http://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA -H“Metadata Flavor:Google”)
。遗憾的是,该解决方案不起作用,部署后仍未配置VM:(这不是linux VM,而是Windows one,我想没有安装curl软件。我可以在我的Windows工作站上与PowerShell一起使用curl,我认为在GCE上也是一样。好的,我已经在gcloud命令中更正了我的元数据部分,现在它工作了,谢谢!所以你建议将此curl请求-->
paramA=$(卷曲http://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA -H“Metadata Flavor:Google”)
到我的启动PowerShell脚本中?是的,在脚本开头初始化它们时不需要外部参数,而是使用了启动PowerShell脚本中提到的参数-->
$ParamA=$(卷曲http://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA -H“Metadata Flavor:Google”)
。不幸的是,该解决方案不起作用,部署后VM仍然没有配置:(这不是linux虚拟机,而是Windows虚拟机,我想没有安装curl软件。我可以在我的Windows工作站和PowerShell上使用curl,我认为它在GCE上也是一样的。好的,我有相应的