【问题标题】:PowerShell script with parameter for Windows VM instance on Google Cloud Platform带有参数的 PowerShell 脚本,用于 Google Cloud Platform 上的 Windows VM 实例
【发布时间】:2019-12-04 08:10:01
【问题描述】:

我正在尝试通过 terraform 在 Google Cloud 上部署 Windows VM。虚拟机正在部署,我可以使用 windows-startup-script-url 执行 PowerShell 脚本。

通过这种方法,我只能使用已经存储在 Google Storage 中的脚本。如果脚本有参数/变量,那么如何传递该参数,任何线索!

provider "google" {
  project = "my-project"
  region  = "my-location"
  zone    = "my-zone"
}

resource "google_compute_instance" "default" {
  name         = "my-name"
  machine_type = "n1-standard-2"
  zone         = "my-zone"

  boot_disk {
    initialize_params {
      image = "windows-cloud/windows-2019"
    }
  }

  metadata {
    windows-startup-script-url = "gs://<my-storage>/<my-script.ps1>"
  }


  network_interface {
    network = "default"

    access_config {

    }
  }

  tags = ["http-server", "windows-server"]
}


resource "google_compute_firewall" "http-server" {
  name    = "default-allow-http"
  network = "default"

  allow {
    protocol = "tcp"
    ports    = ["80"]
  }


  source_ranges = ["0.0.0.0/0"]
  target_tags   = ["http-server"]
}

resource "google_compute_firewall" "windows-server" {
  name    = "windows-server"
  network = "default"

  allow {
    protocol = "tcp"
    ports    = ["3389"]
  }

  source_ranges = ["0.0.0.0/0"]
  target_tags   = ["windows-server"]
}

output "ip" {
  value = "${google_compute_instance.default.network_interface.0.access_config.0.nat_ip}"
}

【问题讨论】:

    标签: powershell google-cloud-platform google-compute-engine terraform


    【解决方案1】:

    Terraform 不需要从 GCS 存储桶中提取启动脚本。

    示例here 显示:

     }
    
      metadata = {
        foo = "bar"
      }
    
      metadata_startup_script = "echo hi > /test.txt"
    
      service_account {
        scopes = ["userinfo-email", "compute-ro", "storage-ro"]
      }
    }
    

    更多关于 GCE 和 Powershell 脚本的官方文档here

    【讨论】:

    • 不幸的是,使用 metadata_startup_script 我无法实现我的目标。目标是将一些参数传递给 VM,以便 PowerShell 脚本可以将这些参数用作变量。
    • 你能扩展一下吗?我能够使用 { metadata_startup_script= "$file("./powershell.ps1)}" } 运行与您的部署类似的示例 powershell 脚本
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2019-01-22
    • 2017-01-20
    • 1970-01-01
    • 2017-10-14
    相关资源
    最近更新 更多