Debug Azure Devops powershell scripts locally in VSCode - powershell

Judging from the Debugging Azure Functions https://learn.microsoft.com/en-us/azure/azure-functions/functions-debug-powershell-local, it looks possible. Can somebody post a detailed example?
Let's say we have pipeline that run the Build.ps1. The script installs other PS modules builds my Visual studio solution and creates the installer.
The installer part breaks so I guess I have to use a Wait-Debugger before it and try to somehow attach to the agent powershell process. But oi am not certain how to do it.

Related

Execute SQLCMD.exe from PowerShell in an Azure DevOps Release Pipeline

I'm porting our deployment infrastructure to Azure DevOps (formerly Visual Studio Team Services), but I've run into an issue I can't find any good solutions to.
We're using EF Core Migrations, and to work around some unrelated issues we need to dynamically wrap SQL scripts in SQLCmd, which is easy using PowerShell.
However, when executing our script as a regular PowerShell step in the release pipeline, it fails because SQLCMD.exe is not available. Not so surprising - but I also can't find any documented way of installing it.
Is there a tool installer or some similar ready-made component that will let me execute a PowerShell script that calls out to SQLCMD.exe (via Invoke-SqlCmd) as part of an Azure Devops Pipeline? If not, what's the easiest way to accomplish this anyway?
Haven't tried this myself, but are you allowed to install Powershell modules for the current user on a hosted agent? Because Invoke-SqlCmd is part of the SqlServer module, which can be installed from the Powershell gallery with:
Install-Module -Name SqlServer -Scope CurrentUser
You could try to create a package to install the needed tools on the agent, since that is now possible. You could use a Chocolatey task to run a package from it
There is an (old) chocolatey package available that you could try: https://chocolatey.org/packages?q=SQLCMD
Can't you use normal sql scripts via one of the available extensions?
See here or here
Ofcourse, using a self-hosted agent could be an option, then you can install anything you want.
This one worked for me
Install-Module -Name SqlServer -Force -AllowClobber
Not sure about -AllowClobber though, on my local PC the command complained about something without it and I didn't check on Azure tbh.

VSTS Run Powershell Script on Mac with Parameters

I have a Xamarin.Forms project wherein the Android project is built in VSTS using a hosted build agent which runs a powershell script at run-time.
I need to run the same powershell script against the Xamarin.iOS which uses an On Premise Mac Build Agent. But how?
I found this answer and a comment under the answer also had the same question regarding parameters, so I am posting the solution here since the issue is slightly different and that question has an accepted answer.
First off, I installed powershell on the mac using these instructions and I modified the shell script task to include the Visual Studio Team Services (VSTS) environmental variables that I wanted to pass to the powershell script.
Next, I pass the arguments through to the called powershell script by slightly modifying the shell script mentioned by the referenced answer.
#!/bin/bash
powershell ./Version.ps1 $1 $2
Finally, in the powershell script, I catch the arguments that have been passed through using using param like this:
param([string]$version, [string]$path)
Wherein I can now use the variables $version and $path which contain the original arguments entered in VSTS to the needs of my powershell script.

Run powershell build step in VSTS agent installed on mac?

I installed VSTS build agent on mac to build xamarin iOS project. Builds worked fine until I added powershell build step.
Even though I installed powershell for mac (https://github.com/PowerShell/PowerShell) and re-installed the agent, VSTS complains it does not have agent that is capable to run the build.
No agent could be found with the following capabilities:
DotNetFramework, Xamarin.iOS, npm
When I disable the build step, builds work just fine.
Is it possible to run powershell build step on Mac?
As MrHinsh clarified, the PowerShell task cannot be used on Mac.
As a workaround I used ShellScript task:
With the following bash script:
#!/bin/bash
powershell ./SetAppVersion.ps1
Also, the powershell installer did not seem to add powershell to my PATH so I had to add it:
$ export PATH=$PATH:/usr/local/microsoft/powershell/6.0.0-alpha.16
If you're sure that DotNetFramework is installed then you can go to the Agent Queues settings and add a custom Capability to it called exactly that.
That should allow it to run but it might fail after that if the agent can't actually find them, but it might also succeed so it's probably worth a try.
No, you can't use a PowerShell task on a Mac, only node tasks are supported.
PowerShell tasks as currently written in PowerShell3 which is not supported on Mac. You can request that the team implement this on http://visualstudio.uservoice.com
In TFS build go to Agents Queues=>Capablilities=>Add variable named as DotNetFramework and give value for mac agent's dotnet framework path.
It's fix for the issue "No agent could be found with the following capabilities:DotNetFramework"
This is a follow-up to the accepted answer to address a question in a comment which I also had.
Thanks to spatialguy for posting and finding a simple solution to this problem. I had the same problem as KeithA45:
QUESTION: What if you wanted to do the same, but also pass arguments to the Bash script which passes them to the Powershell script?
I found a solution to this, first off, I modified the shell script task to include the Visual Studio Team Services (VSTS) environmental variables that I wanted to pass to the powershell script.
Next, I pass the arguments through to the called powershell script by slightly modifying the shell script mentioned by the accepted answer.
#!/bin/bash
powershell ./Version.ps1 $1 $2
Finally, in the powershell script, I catch the arguments that have been passed through using using param like this:
param([string]$version, [string]$path)
Wherein I can now use the variables $version and $path which contain the original arguments entered in VSTS to the needs of my powershell script.
Things seem to have moved forward because I ran successfully today a PowerShell#2 task on a Mac Self-Hosted Agent from an Azure DevOps build pipeline.
By checking "Enable system diagnostics" when queuing the build, the log shows me that the task found itself the path to the PowerShell Core (pwsh) that I installed on my Mac with the help of Homebrew (brew cask install powershell - see https://learn.microsoft.com/fr-fr/powershell/scripting/install/installing-powershell-core-on-macos).

Run Powershell script on TFS server for publish to Azure

I try to configure TFS for Continuous Delivery to Azure by this article
In article TFS published package to Azure with Powershell script.
When build starts I get errors like ObjectNotFound: (Set-AzureDeployment:String) [], CommandNotFoundException. Looks like I didn't install Azure cmdlets, but I install all from Web Platform Installer.
And when I try to run script locally on server - it works and deploys package.
In article Powershell starts by adding InvokeProcess to template with Filename="PowerShell".
I think I just don't run Powreshell correctly.
Maybe somebody has some ideas which command should I use?
Find a solution
Powershell cann't find Azure module.
Add this before Import-Module Azure command in script
$env:PSModulePath=$env:PSModulePath+";"+"C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell"
Could be that you installed the cmdlets on the user profile. Try re-install after logging in with the account running the build service.

How would you deploy this .net stack?

I have a .net app which has an MVC3 front end and 2 windows services.
It all depends on 2 RavenDB installations which can either be ran as windows services or IIS - I'm not bothered here.
The services are built using TopShelf and testing is done with straight NUnint. I use Github as my repo.
Ideally on each successful 'Release' build I'd like a build, test, wipe directories and RavenDb data dirs and then deploy (web and services) and then start the processes to run finishing off on a console app i'm building which can run in a default set of data.
How would you manage the deployment here? I have no CI server yet. I have a completely fresh server that I can do as I please with. I haven't done CI/CD for a long time and I suspect the weapons have changed.
Should I be looking at MSBuild/NAnt? PSake, Rake? Team City?
How would you manage the post build processes?
I've been using Jenkins with psake and it's working very well. To be honest psake does most of the work, with Jenkins simply pulling down the source and then invoking my psake script, but as Nick Nieslanik says you can easily have Jenkins call MSBuild/NUnit/etc directly if you like.
Jenkins
From a peruse of Jenkins vs. CruiseControl(.NET) threads on StackOverflow, the general consensus appeared to be to go with Jenkins. Having not really tried CruiseControl I can't vouch for that, but I will say that Jenkins is very good. I found Jenkins easy to set up. I had a quick peek at CruiseControl.NET and found Jenkins easier to get started. I haven't looked at TeamCity at all so can't speak to that.
CI: Hudson with .Net vs CruiseControl.Net
What is the difference between Hudson and CruiseControl for Java projects?
How and why do I set up a C# build machine?
Jenkins has a nice plugin system, and a lot of plugins, including one for Powershell which makes it easy to call through to a psake script.
Psake
So far I think psake is awesome. It's based on rake syntax, but is a wee bit more native to Windows than rake. As it's sitting on top of powershell, you can leverage a lot of the handy Windows admin functionality that comes with that. For example, see this post
for a great example of setting up and tearing down IIS app pools and sites direct from your psake tasks. I think that's great, and I'm not sure how you'd go about doing that in MSBuild, Nant, or rake. Basic file system operations are also it's bread and butter -- seems better than having a bunch of angle brackets just to copy some files somewhere.
As for MSBuild and Nant, I think they're both pretty powerful, but editing XML files for this kind of thing just feels painful. Powershell is a proper scripting language with deep Windows integration. psake is a DSL for building and other tasks on top of that. It's a good combo.
That said, for actually building, I just farm that out to msbuild from within psake and call it on the solution/project files I want to build. psake has a built in command for calling msbuild and specifying which version to use, etc. (Truth be told the most pain so far is from msbuild borking up on solution files that build just fine in Visual Studio.)
As you're working with RavenDB, you might be interested to know that they're using psake for building RavenDB (and Rhino-ESB).
For some good psake tips in general, see this post.
Long story short, personally I'd recommend Jenkins and psake. That combo will integrate nicely with git, msbuild, NUnit, IIS, and probably even windows services.
I'd use Jenkins as the job execution engine. Then I could create a set of MSBuild scripts to perform the core-build and use the Jenkins plug in model to add pre & post build tasks as necessary (i.e. NUnit execution and result parsing, Powershell script execution for some deployment) etc. Jenkins has great integration with Github using Post/Pre-commit hooks that allow you to set up CI builds quite simply.