Importing Sharepoint modules in TFS Powershell build script - powershell

I'm trying to run a release in TFS to Sharepoint Online and I can't seem to import and load the module.
I tried just a straight import-module statement:
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
I the found this solution but that's not playing ball either.
How to load PowerShell Module from custom script on vNext build agent?

You will have to load the assembly before you can use the types inside it.
You can use the loadfile method,
[Reflection.Assembly]::LoadFile(“c:\folderwhereyouhavesharepointassemblies/thedllname.dll”)
Import-Module Microsoft.Online.SharePoint.PowerShell

Related

Powershell module not visible when executing script from devops

I wrote a PowerShell script that uses the ReportingServicesTools to deploy reports to a PowerBI Report Server after a project build has terminated. When logging onto the server as myself and executing the script the module is imported successfully and the script executes.
When i execute the target script in a Azure DevOps release pipeline using a Powershell task the following error is returned:
import-module : The specified module 'ReportingServicesTools' was not
loaded because no valid module file was found in any module directory.
At D:\DevOps Agent\DeploySSRS2.ps1:3 char:1 import-module -Name
ReportingServicesTools
We are using a Self-Hosted agent for our releases and builds and the server has PS 5 installed.
I tried:
Moving the module into C:\Program Files\WindowsPowerShell\Modules as MSDN stated this would make the module visible to all users.
Adding the module into C:\Windows\system32\WindowsPowerShell\v1.0\Modules
Referencing the whole path instead of just the module name in the script.

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.

Create pure powershell Nuget module for PowerShellGet

What I want
I want to publish number of PowerShell scripts as Nuget package to be used on build systems.
I want to use PowerShellGet to do installation work for me and version management.
I don't want those scripts to be part of any Visual Studio solution, but as standalone scripts.
Usage scenario
On any system, with configured Nuget provider user executes:
Install-Module MyModule
From that moment all exports from that module permanently available for this user.
Also user can call that command again to update version of those scripts.
What I've done
You can find current state of package here: GitHub
I've added and configured Nuget provider to our local Nuget server
To do this call Get-PackageProvider -Name NuGet -ForceBootstrap and Set-PSRepository -Name My_Nuget_Repo -SourceLocation http://my-nuget/api -InstallationPolicy Trusted
Created proper module, which can be imported locally by Import-Module
Created and published Nuget package with that module
Problem
I can install that package by Install-Module cmdlet and I can see it later in Get-InstalledModule list.
But, no functions are available.
Also, no matter what, but Install-Module not calling any of scripts from my package:
Not calling ScriptsToProcess from MyModule.psd1
Not calling Install.ps1 from tools folder
Not calling Init.ps1 from tools folder
Cmdlets exported by module not available and module can't be imported by Import-Module
(Same package works properly when installed from Visual Studios Install-Package MyModule, scripts are called, PowerShell module is imported).
Investigation
Since PowerShellGet is based on OneGet it seems that problem is in Install-Package cmdlet (which is called inside Install-Module cmdlet).
When I'm executing Install-Package MyModule from Visual Studio Install.ps1 and Init.ps1 are called. But same command from pure PowerShell doing nothing.
After long reverse engineering I've found the root cause
Technical reason
Magical tag PSModule has to be added to <Tags> in nuspec file.
Real reason
You shouldn't create nuspec file and pack nuget package manually at all. Use Publish-Module cmdlet instead.
How to do it properly
I've updated powershellget-module GitHub with:
Example of minimal module which can be published
A way how to use local folder as Nuget feed
Publishing, installation and usage of that module
Reference script with no dependencies which does it all locally, so you can study it
Check it out.

Azure power shell cmdlet dll in .net project

Here I have a question about how to use Azure power shell cmdlet dll in .net project.
I got a look at this thread in msdn:
Use powershell cmdlet we can use this command.
Remove-AzureVM -ServiceName -Name
And this will involve the dll under Microsoft.WindowsAzure.Management.ServiceManagement.dll
which path is:
C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure
I use reflector open this dll.
And I can get the RemoveAzureVMCommand class, but how to use this class achieve the same thing as
powershell cmdlet?
Can some one share some light to me?
Thanks very much!
Azure PowerShell Cmdlets are essentially wrappers around Windows Azure Service Management API. What you could do is write your own C# code to invoke "Delete Role" (http://msdn.microsoft.com/en-us/library/windowsazure/jj157184.aspx) function which will remove the VM.
Hope this helps.

Setting up PSake

Does anybody know or have a link to how I set up psake to run through powershell?
I have absolutely no idea and I can find nothing to tell me.
Download it from here. Here are some examples of its use:
http://ayende.com/Blog/archive/2009/08/30/on-psake.aspx
http://www.jameskovacs.com/blog/IntroducingPsake.aspx
The 2.0 version of PSake is a PowerShell module which requires PowerShell 2.0. Extract the contents of the PSake ZIP into $env:Home\Documents\WindowsPowerShell\Modules\PSake. Then you import it like so:
Import-Module PSake
Define a "properties" hashtable and then start using its exported commands like Task in your build scripts.