Error executing script

Aug 17, 2009 at 11:17 PM

Hi

I'm getting the following error from my team build:  

"scipt contents" is an invalid value for the "Script" parameter of the "Powershell" task. Multiple items cannot be passed into a parameter of type "Microsoft.Build.Framework.ITaskItem

The script is about 100 lines long and contains a couple of functions.  I use it to restore some HyperV snapshots before deploying files onto the machines.  The build file looks like this"

 <PropertyGroup>
    <DeployScript>
          <![CDATA[
function waitForVMToStop([string] $vmname, [int] $maxtime)
{

  $loops = 0
  $vm = Get-VM $vmname -server $server
  while ($vm.EnabledState -ne 3)
  { 
    $mod = $loops % 5
    if($mod -eq 0) {
  $msg = "Waiting for " + $vmname + ". Current state = " + $vm.EnabledState
      write-host $msg
    }
    sleep 1
    $loops += 1
    if($loops -gt $maxtime)
    {
  Write-Host "VM did not stop soon enough";
      break
    }
    $vm = Get-VM $vmname -server $server
  }
}

function waitForVMToStart([string] $vmname, [int] $maxtime)
{

  $loops = 0
  $vm = Get-VM $vmname -server $server
  while ($vm.EnabledState -ne 2)
  { 
    $mod = $loops % 5
    if($mod -eq 0) {
    $msg = "Waiting for " + $vmname + ". Current state = " + $vm.EnabledState
      write-host $msg
    }
    sleep 1
    $loops += 1
    if($loops -gt $maxtime)
    {
  Write-Host "VM did not start quickly enough";
      break
    }
    $vm = Get-VM $vmname -server $server
  }
}

$server = "chcvm5"

# get the vm's

$vmweb01 = Get-VM "Luniweb01" -server $server
$vmweb02 = Get-VM "Luniweb02" -server $server
$vmwss01 = Get-VM "Luniwss01" -server $server

# stop the vms

Shutdown-VM $vmweb01
Shutdown-VM $vmweb02
Shutdown-VM $vmwss01

waitForVMToStop "Luniweb01" 60
waitForVMToStop "Luniweb02" 60
waitForVMToStop "Luniwss01" 60


# get the snap shots

$snweb01 = Get-VMSnapShot $vmweb01 "001"
$snweb02 = Get-VMSnapShot $vmweb02 "001"
$snwss01 = Get-VMSnapShot $vmwss01 "001"

# apply the snapshots
Apply-VMSnapShot $snweb01
Apply-VMSnapShot $snweb02
Apply-VMSnapShot $snwss01

# start the vm's again
Start-VM $vmweb01
Start-VM $vmweb02
Start-VM $vmwss01

waitForVMToStart "Luniweb01" 60
waitForVMToStart "Luniweb02" 60
waitForVMToStart "Luniwss01" 60

Write-Host "Waiting for VM's to get their A into G"
sleep 60       
   
]]></DeployScript>

...

<Powershell Script="$(DeployScript)">
      <Output TaskParameter="Output" PropertyName="ScriptOutput" />
    </Powershell>

 Any ideas what I am doing wrong?  I'm happy to use a script file but I cant see how to do this.

Thanks

 

 

Coordinator
Aug 18, 2009 at 8:40 PM

Took some binary searching, but I figured out what the problem is: your semicolons after the Write-Host statements about the VMs not responding quickly enough.

MSBuild uses semicolons as delimiters for lists; if you have a semicolon in your script, it'll interpret that as an attempt to pass a list to the Powershell task.

I suggest you use an external script file instead, and your problem reminds me that I've forgotten to document those ;-)

I'll go do that now.

Coordinator
Aug 18, 2009 at 9:01 PM
Edited Aug 18, 2009 at 9:51 PM

I've updated the Usage page with an example of how to use external script files http://powershellmsbuild.codeplex.com/Wiki/View.aspx?title=Usage

Aug 18, 2009 at 9:30 PM

THanks very much for that.  It looks like there are only a couple of semi-colons in there - I normally dont bother with them so will try without.  The scriptfile would be betterer though.

Cheers

Aug 18, 2009 at 10:11 PM

Next problem :)  The HyperV stuff my script calls is in the profile for the TFSService which is running the build.  The powershell task doesnt appear to be loading the profile.  Aside frmo including the hyperv stuff in my script can you think of anyway this could work?  It would also be cool if the task could runas a particular user.  I think I will get the code at some point and have a hack.

Cheers