title: Windows Powershell script to alter table
body: <p>I have below script, named as alterTable.ps1. I am trying to alter table by adding two new columns to table.</p>
  
  <p><strong>Here is script</strong></p>
  
  <pre><code>function die {
      "Error: $($args[0])"
      exit 1
  }
  function verifySuccess {
      if (!$?) {
          die "$($args[0])"
      }
  }
  # Create variables and assign environment variables
  $INSTANCE_HOST = $env:HOST
  $INSTANCE_PORT = $env:PORT
  $INSTANCE_NAME = $env:INSTANCE
  $DATABASE_NAME = $env:DATABASE_NAME
  # Execute the alter table, passing the variables
  #sqlcmd -U sa -P sapassword -S "$INSTANCE_HOST\$INSTANCE_NAME,$INSTANCE_PORT" -q ”use $DATABASE_NAME; ALTER TABLE dbo.tabletest ADD Test1 VARCHAR(6) NULL, Test2 VARCHAR(10) NULL”
  VerifySuccess "sqlcmd failed to alter table tabletest"
  </code></pre>
  
  <p>When I execute script with values passing to script, getting error</p>
  
  <pre><code>C:\&gt; .\alterTable.ps1 "WINDOWSHOST" "1433" "MSSQLSERVER" "dbname"
  </code></pre>
  
  <p>Getting error as below:</p>
  
  <blockquote>
    <p>HResult 0x57, Level 16, State 1 SQL Server Network Interfaces:
    Connection string is not valid [87]. Sqlcmd: Error: Microsoft SQL
    Server Native Client 10.0 : A network-related or instance-specific
    error has occurred while establishing a connection to SQL Server.
    Server is not found or not accessible.</p>
  </blockquote>
  
  <p>If I hardcode those values in script, then it worked fine.</p>
  
  <p>One more quick thing, how I can exit script after execution and check the exit status?</p>