Output a Command to a Variable(file) to be run later

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Alessandro Alessandro 2 weeks, 4 days ago.

  • Author
    Posts
  • #72164
    Profile photo of Alessandro
    Alessandro
    Participant

    Hi Folks,
    I'm trying to get powershell to output a command to a variable, so that the file will contain the code line (ie. not run it there and then, but have it build a file of powershell commands. But, I can't for the life of me, figure out how to get it to do that. The line of code works fine when 'run' but not when I try and output it to a variable. I know it's a matter of formatting and escaping some quotes etc, but I've tried so many combinations I'm going crazy! Can anyone help?

    This line works, and updates the bindings. But, I don't want to update bindings now, I want to out the line of code to a variable (a file) and run the file later on.

    $Output += Set-ItemProperty IIS:\sites\$($Node.name).domain.com -name bindings -value @{protocol="http";bindingInformation="$($Node.value):80:$($node.Name)-$NodeNumber.domain.com"}
    

    But when I enclose it in quotation marks, so that the whole line is output to the variable it doesn't work:

    $Output += "Set-ItemProperty IIS:\sites\$($Node.name).domain.com -name bindings -value @{protocol="http";bindingInformation="$($Node.value):80:$($node.Name)-$NodeNumber.domain.com"}"
    

    I've tried escaping everything but the variables etc, and no luck!
    I'm stuck 🙁 HELP ?!!!

  • #72166
    Profile photo of Chris
    Chris
    Participant

    Hi,

    If you are planning on calling it later on, i think maybe a Function would better suit your purpose? Although i will stand to be corrected when the wizards get here 😉

     function Output { *script here* } 

    You can then call the function at any point.

    • #72170
      Profile photo of Alessandro
      Alessandro
      Participant

      hey thanks for the tip, but I what I need it to do is to create a file, and I will run that file later on – not part of the same script.
      So, the script I am trying to get working will loop through an XML file, and create new bindings for sites in the XML. The script will create a file (eg. update-bindings.ps1) that I can use on the server later.

      So the file would look like:

      Set-ItemProperty IIS:\sites\$($Node.name).domain.com -name bindings -value bla bla bla
      Set-ItemProperty IIS:\sites\$($Node.name).domain.com -name bindings -value bla bla bla
      Set-ItemProperty IIS:\sites\$($Node.name).domain.com -name bindings -value bla bla bla
      Set-ItemProperty IIS:\sites\$($Node.name).domain.com -name bindings -value bla bla bla
      Set-ItemProperty IIS:\sites\$($Node.name).domain.com -name bindings -value bla bla bla
      

      That's my need for this example !
      THANKS!

  • #72175
    Profile photo of Alessandro
    Alessandro
    Participant

    I edited my original post to fix the code to be between PRE tags, and now the original post has dissappeared?
    Is there any way to get it back? Or I need to start again?

You must be logged in to reply to this topic.