remote-exec Powershell escape the pipe

2,491 views
Skip to first unread message

Stephen Small

unread,
Aug 30, 2016, 10:13:33 AM8/30/16
to Terraform
All,

I'm having trouble passing an inline command with a remote-exec provisioner.

resource "null_resource" "server1" {
  provisioner "remote-exec" {
    inline = [
      "powershell.exe $output = 'party' | Write-host"
  ]

I get this:


null_resource.server1 (remote-exec): C:\Users\Administrator>powershell.exe $output = 'party'   | Write-host
null_resource.server1 (remote-exec): 'Write-host' is not recognized as an internal or external command,
null_resource.server1 (remote-exec): operable program or batch file.


How do I tell terraform to escape the pipe and allow powershell to do it's thing?

Thanks!

Jeremy Young

unread,
Sep 2, 2016, 3:03:25 PM9/2/16
to Terraform
What if you enclose all of the arguments passed to Powershell in quotes?

resource "null_resource" "server1" {
 provisioner "remote-exec" {
   inline = [
     "powershell.exe \"$output = 'party' | Write-host\""
 
]

Jeremy Young

unread,
Sep 2, 2016, 3:20:29 PM9/2/16
to Terraform
I'm using this on Fedora 24 to test:

## In my .bash_profile or .bashrc

powershell () {
DOCKER_RUN="docker run --rm --name powershell -it"
DOCKER_IMAGE="centos/powershell"
DOCKER_COMMAND="/usr/bin/powershell"

## Check permissions on Docker socket and fix if necessary
if [[ ! -w /var/run/docker.sock ]]; then
sudo setfacl -m u:jyoung:rw /var/run/docker.sock
fi

## Make sure Powershell image is installed
if [[ "$( docker images | grep -i powershell &>/dev/null; echo $? )" != "0" ]]; then
printf "Pulling powershell image for use...\n"
docker pull "${DOCKER_IMAGE}" &>/dev/null
fi

## If we passed arguments, parse them, and if necessary, mount in the directory of the script we're trying to execute.
if [[ "$#" > 0 ]]; then
ARGS="$@"
while [[ "$#" > 0 ]]; do
if [[ "$1" == *".ps1"* ]]; then
CURRENT_DIR=$( dirname $1 )
fi
if [[ "$1" == *".psm1"* ]]; then
CURRENT_DIR=$( dirname $1 )
fi
shift
done
if [[ -n "${CURRENT_DIR}" ]]; then
${DOCKER_RUN} -v ${CURRENT_DIR}:/opt/powershell_volume:rw -w /opt/powershell_volume ${DOCKER_IMAGE} ${DOCKER_COMMAND} ${ARGS}
else
${DOCKER_RUN} ${DOCKER_IMAGE} ${DOCKER_COMMAND} ${ARGS}
fi
else
${DOCKER_RUN} ${DOCKER_IMAGE} ${DOCKER_COMMAND}
fi
}



So that lets me do this:
powershell get-host


Name             : ConsoleHost
Version          : 6.0.0
InstanceId       : bf28d67b-5c9d-48f8-a8d8-cd8521d6be31
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : 
CurrentUICulture : 
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace



Escaping the $ so my Bash shell doesn't pick it up, I get this:
$ powershell "\$output = 'party' | Write-Host"
party


Stephen Small

unread,
Sep 19, 2016, 2:21:00 PM9/19/16
to Terraform
way too much work for a windows guy ;). I ended up passing powershell scripts like this:

  resource "null_resource" "rename1" {
    depends_on = ["null_resource.foldersandfiles1"]

    provisioner "remote-exec" {
      inline = [
        "powershell.exe -sta -ExecutionPolicy Unrestricted -file C:\\Ec2VMConversion\\Powershell\\DevOPS_domain_rename_dc01.ps1",
      ]

      connection {
        type     = "winrm"
        host     = "${aws_eip_association.eip_assoc1.public_ip}"
        user     = "Administrator"
        password = "P@ssword"
        timeout  = "30m"
Reply all
Reply to author
Forward
0 new messages