Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Pipe to connect to Powershell

John Issa May 4, 2020

Has anyone been able to connect to powershell via bitbucket pipelines? I have some simple SharePoint code that I house in my bitbucket repo and would like to start deploying those updates via pipelines.

I'm unable to find any documentation on this and was wondering if anyone else has had any experience doing this.  

Not much here, but this is what I have so far...

definitions:
services:
docker:
memory: 2048
steps:
- step: &connect-to-powershell
name: Connect to Powershell
image: mcr.microsoft.com/powershell
script:
- CMD [ "Connect-SPOService", "-Url", $psURL ]

pipelines:
branches:
master:
- step: *connect-to-powershell

 ...and this is the error message I recieve.

+ CMD [ "Connect-SPOService", "-Url", $psURL ]
bash: CMD: command not found

 Any help or guidance here would be greatly apprecaited.

 

Thank you!

2 answers

0 votes
Vishal Singh July 18, 2022

How to execute multiple lines 

like this one??
New-AzVm `
-ResourceGroupName 'myResourceGroup' `
-Name 'myVM' `
-Location 'East US' `
-VirtualNetworkName 'myVnet' `
-SubnetName 'mySubnet' `
-SecurityGroupName 'myNetworkSecurityGroup' `
-PublicIpAddressName 'myPublicIpAddress' `
-OpenPorts 80,3389

John Reardon October 14, 2022

Hi @Vishal Singh ,

Try something like this:
example.png

  

0 votes
Halyna Berezovska
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
August 28, 2020

@John Issa you can see 'bash' in error log.

It means by default pipeline supports bash commands.

But you can try execute in a script  powershell commands you need via pwsh tool.

For example:

step:
name: powershell
image: mcr.microsoft.com/powershell
script:
- pwsh -Command {Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable}

 

Also, looks like powershell does not have sharepoint installed. You can do something for this.

If you don't succeed with that, you can try going with Custom pipe. Look how you can create your custom pipe here . Because I see, that executing this just in pipeline seems quite complex.

Regards, Galyna

Cronje van Heerden September 23, 2021

... or a little easier on the eyes + multiline support. 

name: Gotta have pwsh
image: mcr.microsoft.com/powershell
script:
- >-
pwsh -Command "& {
Write-Host 'Hello Powershell'
}"
Like Dmytro Shchepak likes this
Dmytro Shchepak September 27, 2021

@Halyna Berezovska @Cronje van Heerden 

Could you please suggest how to execute pwsh command on the remote server?
Want to:
- make a backup,
- stop IIS,
- copy a new site,
- launch IIS.

I'll be appreciated for any help.

I have tried a few ways but still can't solve how to do this.

 

#2nd step to deploy to server

- step:

name: Deploy to server

deployment: staging

script:

- pipe: atlassian/scp-deploy:1.1.0

variables:

USER: 'deploy' # $USER

SERVER: '1.1.1.1' # $SERVER

REMOTE_PATH: 'D:/ttt/' # $REMOTE_PATH

LOCAL_PATH: 'release/*'

# DEBUG: 'true'

EXTRA_ARGS: ["-P", "8022"] #port

#3rd step runs script on server

- step:

#name: powershell

image: mcr.microsoft.com/powershell

script:

- pwsh -Command {Enter-PSSession -HostName '1.1.1.1' -UserName 'deploy'}

- pwsh -Command {Invoke-Command -ComputerName '1.1.1.1' -ScriptBlock {Get-UICulture}}

- pwsh -Command {cmd /c ver}

- pwsh -Command {Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable}

- pwsh -Command {Copy-Item -Path "c:\inetpub\GS 2.0_SM" -Destination "d:\ttt\site backup\*" -Recurse}
Cronje van Heerden September 27, 2021

I haven't considered that use case before .. my guess would be that the permissions and port forwarding requirements for remote Powershell would be a problem if you're running this from Atlassian Cloud build runners.

If it's an option to run docker containers on the target server I'd try running a self-hosted build runner on the target server with a volume mapped to the target IIS folder only. 

Dmytro Shchepak September 27, 2021

Thanks for the so fast answer.

We don't use Docker, maybe it'll be in the future.

Previously I sought to try to use some batch script but PowerShell looks smarter.

Maybe it has sense make some research http://techdiksha.com/run-powershell-scripts-remote-machine-azure-release-pipeline/ , I hope it can help reach the aim.
I'll try to look.

Sharath Chakali February 22, 2022

@Dmytro Shchepak 

 

I am working on similar kind of requirement, did you find a solution for this? please suggest.

- make a backup,
- stop IIS,
- copy a new site,
- launch IIS.

Thanks in advance.

Cronje van Heerden February 23, 2022

[PowerShell Remoting Over SSH - PowerShell | Microsoft Docs](https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/ssh-remoting-in-powershell-core?view=powershell-7.2) may be a way to technically achieving this, but the security implications of a allowing Powershell access from a Bitbucket-Pipelines cloud agent sounds very scary.

In my environment we use Octopus Deploy, Intune or Azure Automation to run Powershell in automation.

Azure Pipelines runner would also be an option - you can run one free one I think, and it would be possible to trigger it from a Bitbucket Pipeline.

I'd also be very interested if anyone knows about a secure and simple way to run Powershell from a Bitbucket-Pipeline directly.

Dmytro Shchepak February 23, 2022

@Sharath Chakalifinally decided to move to Azure DevOps. :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events