Running Powershell.exe -command

Hi,

I have step template that has the following line of code:
Powershell.exe -Command “& {Add-PSSnapin SqlServerCmdletSnapin100;Add-PSSnapin SqlServerProviderSnapin100;$InvokeSQLcmd}” > $LogFilePath

I have copied this step Module to a ps1 file and ran it on target server with no error. But when I run it in Octopus deploy it comes up with the following error:

Unexpected token ‘&’ in expression or statement.
At D:\Octopus\Work\20160609152033-429\Script.ps1:36 char:31
Powershell.exe -Command “& <<<< {Add-PSSnapin SqlServerCmdletSnapin100;Add-PSSnapin SqlServerProviderSnapin100;$InvokeSQLcmd}” > $LogFilePath
+ CategoryInfo : ParserError: (&:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
The remote script failed with exit code 1

Any idea why?

Regards
B.

Hi B,

Thanks for reaching out.

Step templates already run in Powershell. Why is Powershell.exe being called from the PS script?

Can you try to just put this at the top of your script?

Add-PSSnapin SqlServerCmdletSnapin100 | out-file $LogFilePath -append
Add-PSSnapin SqlServerProviderSnapin100 | out-file $LogFilePath -append
$InvokeSQLcmd | out-file $LogFilePath -append

Regards,
Dalmiro

Hi,

There is a limitation in Powershell 2.0 where it cannot take the output from Invoke-sqlcmd to a file.
Yes, I have tried your method and it does not work.

The work around that I have found is that we run powershell.exe and send the result out to a file.

Any reason why I am unable to run powershell.exe –command “& {$InvokeSQLcmd}”?

Regards
Binh Au
Senior Release Analyst, Service Delivery
ILS Commercial Delivery & Technology

d: 01733 845878
w: www.bglgroup.co.ukhttp://www.bglgroup.co.uk/

Join BGL Group on social media
[Description: http://intranet/GroupCommunications/PublishingImages/Social%20Media/Facebook-email-logo.jpg]https://www.facebook.com/pages/BGL-Group/337417076303996[Description: http://intranet/GroupCommunications/PublishingImages/Social%20Media/Twitter-email-logo.jpg]https://twitter.com/The_BGL_Group[http://t0.gstatic.com/images?q=tbn:ANd9GcTFOZJtni6_J0v5TJlWW-CTVIk90I79BtZmJ2TA2xQcoA5I5zHBO4mCXuWB]http://www.linkedin.com/company/162701

image003.jpg

image001.jpg

image002.jpg

Hi Binh,

I can’t think why it wouldn’t work. Just tried something very similar (without SQLcmd) on my end and it does work as expected.

  • Which version of Octopus are you running?
  • Are you running this PS script from a script step in your process? or from a script module in Octopus?

Thanks,
Dalmiro

  • Which version of Octopus are you running?
    3.3.8

  • Are you running this PS script from a script step in your process? or from a script module in Octopus?
    Via PS script

Regards
Binh Au
Senior Release Analyst, Service Delivery
ILS Commercial Delivery & Technology

d: 01733 845878
w: www.bglgroup.co.ukhttp://www.bglgroup.co.uk/

Join BGL Group on social media
[Description: http://intranet/GroupCommunications/PublishingImages/Social%20Media/Facebook-email-logo.jpg]https://www.facebook.com/pages/BGL-Group/337417076303996[Description: http://intranet/GroupCommunications/PublishingImages/Social%20Media/Twitter-email-logo.jpg]https://twitter.com/The_BGL_Group[http://t0.gstatic.com/images?q=tbn:ANd9GcTFOZJtni6_J0v5TJlWW-CTVIk90I79BtZmJ2TA2xQcoA5I5zHBO4mCXuWB]http://www.linkedin.com/company/162701

image003.jpg

image001.jpg

image002.jpg

Correction

  • Are you running this PS script from a script step in your process? or from a script module in Octopus?
    Step templates

Regards
Binh Au
Senior Release Analyst, Service Delivery
ILS Commercial Delivery & Technology

d: 01733 845878
w: www.bglgroup.co.ukhttp://www.bglgroup.co.uk/

Join BGL Group on social media
[Description: http://intranet/GroupCommunications/PublishingImages/Social%20Media/Facebook-email-logo.jpg]https://www.facebook.com/pages/BGL-Group/337417076303996[Description: http://intranet/GroupCommunications/PublishingImages/Social%20Media/Twitter-email-logo.jpg]https://twitter.com/The_BGL_Group[http://t0.gstatic.com/images?q=tbn:ANd9GcTFOZJtni6_J0v5TJlWW-CTVIk90I79BtZmJ2TA2xQcoA5I5zHBO4mCXuWB]http://www.linkedin.com/company/162701

From: Binh Au
Sent: 11 June 2016 06:37
To: ‘Dalmiro Grañas’ tender2+d96ba32058@tenderapp.com
Subject: RE: [EXTERNAL] Re: Running Powershell.exe -command [Problems #46237]

  • Which version of Octopus are you running?
    3.3.8

  • Are you running this PS script from a script step in your process? or from a script module in Octopus?
    Via PS script

Regards
Binh Au
Senior Release Analyst, Service Delivery
ILS Commercial Delivery & Technology

d: 01733 845878
w: www.bglgroup.co.ukhttp://www.bglgroup.co.uk/

Join BGL Group on social media
[Description: http://intranet/GroupCommunications/PublishingImages/Social%20Media/Facebook-email-logo.jpg]https://www.facebook.com/pages/BGL-Group/337417076303996[Description: http://intranet/GroupCommunications/PublishingImages/Social%20Media/Twitter-email-logo.jpg]https://twitter.com/The_BGL_Group[http://t0.gstatic.com/images?q=tbn:ANd9GcTFOZJtni6_J0v5TJlWW-CTVIk90I79BtZmJ2TA2xQcoA5I5zHBO4mCXuWB]http://www.linkedin.com/company/162701

image003.jpg

image001.jpg

image002.jpg

Ignore this. I’ve found the issue is due to my powershell script syntax