Azure power shell cmdlet dll in .net project - powershell

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.

Related

PowerShell Core and AppX package management

I am using PowerShell 6.2 preview at the moment. In my script I am trying to do stuff with Windows 10 apps. To be able to use commands like Get-AppxPackage, I need to import Windows modules from previous PowerShell like so:
Import-Module C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Appx\Appx.psd1 -SkipEditionCheck
Import-Module C:\Windows\system32\WindowsPowerShell\v1.0\Modules\dism\dism.psd1 -SkipEditionCheck
Does PowerShell core has its own modules to work with this? I found Get-Package for example, but that does not give me anything.
Since this is one of the top search results for PowerShell Core Get-AppxPackage, I'm going to take the information from the link provided in the comments and provide an answer, with example.
As LangsGalgEnRad pointed out in the comments, it's easiest just to do this from Windows PowerShell, but ultimately that's just-shy-of-deprecated at this point, with Microsoft stating that there are to be no more fixes or changes other than critical security issues. That said, it's still (afaik) universally available in Windows installations.
But for those of us who want to follow Microsoft's advice to use PowerShell Core, LangsGalgEnRad also points out in the comments the WindowsCompatibility module from Microsoft. Reading the blog post, this seems a bit safer than importing a Windows module (e.g. AppX) from PowerShell Core, since among other things ...
WindowsCompatibility is very careful to not overwrite native PowerShell core commands.
To install from PowerShell Gallery:
Install-Module WindowsCompatibility
Example usage for AppX:
Import-Module WindowsCompatibility
Import-WinModule AppX
Get-AppxPackage

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.

Importing Sharepoint modules in TFS Powershell build script

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

How to install Azure cmdlets using powershell

I'm trying to install Azure cmdlets using powershell, not the wizard provided by Microsoft.
That's because my script (which has Azure cmdlets) will be used in a new virtual machine located in Azure and if my script try to run some cmdlet of Azure, will fail for sure.
I would like to put the installation lines of the powershell cmdlets on the top of my script for install the whole cmdlets and after that, that my script execute the other cmdlets without problem.
So, anyone knows?
Thanks!
If you have the Web Platform Installer in the VM, you can use the script I posted at PowerShell Magazine.
http://www.powershellmagazine.com/2014/02/27/using-powershell-and-web-platform-installer-to-install-azure-powershell-cmdlets/
Or get the Windows Standalone installer from https://github.com/Azure/azure-sdk-tools/releases and use msiexec to install that.
If you want to use PowerShell to download the latest version too:
You can use Invoke-WebRequest to read the page (https://github.com/Azure/azure-sdk-tools/releases) and then get all links from that. You can then get all links that end with .msi and take the first link for download.
#Code not tested
$doc = Invoke-WebRequest 'https://github.com/Azure/azure-sdk-tools/releases'
$links = $doc.Links.Href

How to add a PSCmdlet or PSSnapin to hosted Powershell runtime without installing the snapin

My scenario is as follows.
I am uploading my dll through web UI.
I am hosting a PowerShell runspace in an ASP.NET application.
I The DLL contains the PSCmdlet and would like to make use of it.
The PSCmdlet only needs to be accessible within the hosted runspace and does not need to be
used in any external scenario.
The application does not have access to the windows registry as it runs with limited privileges, so I cannot install the PsSnapin.
Is it possible for me to use the commandlet without going through the installation process?
If your use case is straightforward then it is possible. Please see these question: Hosted PowerShell cannot see Cmdlets in the same Assembly. Several answers there provide different ways, choose one that works better or you like more.
If you use PowerShell V2 you can use an unregistered snapin as a binary module.
Import-Module ‹path-to-dll›
add a -passthru to get the PSModuleInfo reference which describes the module.