I am using Visual Studio Team Services (VSTS) hosted build agents in my build process. My builds mostly rely on the 'PowerShell' step that calls a script that I have in git. From within this script, I would like to manage PowerShell modules using PowerShellGet. For example, I would like to be able to install pscx simply by running
Install-Module -Name pscx
Unfortunately, hosted agents use PowerShell 4 and they don't have the PowerShellGet module installed. As a result, the Install-Module function is not available.
Anybody has any suggestion to use the PowerShellGet module on VSTS hosted agent? Note that since I don't have admin rights on this machine, I can't install the msi that installs PowerShellGet for PowerShell 4.
To be able to use PowerShellGet, two PowerShell modules are required:
PowerShellGet
PackageManagement
These are available out of the box with PowerShell 5 or through the msi installer available on the PowerShell Gallery.
Instead of deploying these modules through the msi, you can simply add them to your git repository (ex: in a folder named PsModules). You will be able to get a hand on these modules on a machine that has either PS5 or the msi installed. They are usually in the C:\Program Files\WindowsPowerShell\Modules folder.
Then, add the PsModules folders to your PSModulePath environment variable. Starting from there, it is possible to use PowerShellGet as in the following:
$env:PSModulePath = "$env:BUILD_SOURCESDIRECTORY\PsModules;$env:PSModulePath"
Import-Module PowerShellGet
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope currentuser
Save-Module pscx -path "$env:BUILD_SOURCESDIRECTORY\PsModules"
import-module pscx
Write-Host '************************'
Get-Command -module pscx
Related
I have an error in the PowerShell console with Chocolatey's link.
chocolatey's link error
If have not a solution, I need to learn how to install Chocolatey with the downloaded file "chocolatey.0.10.15.nupkg".
chocolatey's file .nupkg
Installing choco is a very common use case, including NuGet.
PowerShell already comes with a package manager that uses NuGet that it already installs by default. You can still install choco for other reasons, but for every day, normal PowerShell use, it's not needed.
Installing PowerShellGet
https://learn.microsoft.com/en-us/powershell/scripting/gallery/installing-psget?view=powershell-7
PowerShellGet is an in-box module in the following releases Windows 10
or newer Windows Server 2016 or newer Windows Management Framework
(WMF) 5.0 or newer PowerShell 6
Set your security level in your PowerShell session.
# Required for use with web SSL sites
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
If your ExecutionPolicy is set to AllSigned, then any script you run must be signed. Otherwise, set the ExecutionPolicy to RemoteSigned or ByPass in your user session.
How to Install Chocolatey using PowerShell
Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression (
(New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')
)
I am in a tricky situation where I need to install the MSOnline PowerShell module on a PC and it needs to install NuGet first. Unfortunately, the company firewall does not trust the NuGet website, so the PowerShell cmdlets fail at installation.
I was wondering whether I could simply copy a directory containing the MSOnline module from one PC to my target PC. Is this a viable workaround? Or is there another workaround?
I understand that ideally, I should try to have the website whitelisted, but there is a question of timing here.
As exlpained in the comment of #TheMadTechnician you can copy the module from the $env:PSMdulePath the your requested target machines.
If you've multiple machines for module distribtion you can setup your own private Powershell gallery via a network share as explained here (if that's possible in your company). Based on that multiple machines can use the Get-PSRepository, Find-Module -Repository MyRepository, Install-Module -Name MyModule -Repository MyRepository ,.. cmdlets for module administration. I'm using this a private Powershell gallery to distribute some Git tools about multiple dev-machines. You could also publish own Powershell modules in this gallery, in the background NuGet is used. So your machine with access to NuGet could store the newest module via Save-Module (maybe via a background job) and re-publish it to your private gallery.
You should also check if the MSOnlinemodule has any RequiredPackages that you also need to copy. You can do this via (where I used PowerShellGet as example):
PS C:\> (Get-Module -ListAvailable | ? Name -eq PowerShellGet ).RequiredModules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0.0.1 PackageManagement
Maybe this helps.
From a computer that has access you could use the Save-Module cmdlet to save the module to a path, then copy that folder to the machine in question that's blocked. Then you can either place the folder in the $env:PSModulePath, or load it explicitly with the full path to the folder.
On a computer that has access to the PowerShell Gallery you can run:
Find-Module MSOnline|Save-Module -Path $home\Downloads
Then copy the MSOnline folder from your downloads folder on that computer to the target computer, and place it in one of the folders listed in $env:PSModulePath, or when you want to load the module you can do it explicitly by defining the path to the module.
I see in powershell gallery that there is a newer version of the iisadministration module than the one that shipped with Server 2016.
I got it installed using:
install-module -name IISadministration -scope currentuser -verbose -force -SkipPublisherCheck
I now have two versions, the newer one installed for the currentuser and the original that shipped with the OS.
Import-Module IISAdministration was successful and so was explicitly specifying the psd1 path. However, when I try to use New-IISSiteBinding (included in the newer version), it complains that the cmdlet is not recognized.
Article referencing the new cmdlt
https://blogs.iis.net/iisteam/introducing-iisadministration-in-the-powershell-gallery
Can anyone please help? Thanks
Trying to set up my VSTS windows build agent to accommodate for powershell steps within VSTS but having some problems. Initially it reported that the AzurePS system capability was not present, so I made sure to install the latest Azure modules via the powershell gallery. Now, the AzurePS capability is present but certain powershell commands do not seem to work.
This may not be related to VSTS at all but rather just a configuration issue with my specific powershell installation / configuration. The full error is as follows:
The term 'Select-AzureRMSubscription' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
This error can be seen both in the VSTS release logs of the particular definition I am trying to execute, as well as when attempting to run the Select-AzureRMSubscription command directly on the box itself.
According to This MS Docs for Get-AzSubscription it's located in the module Az.Accounts.
So I had to run:
Install-module Az -AllowClobber -Force
Import-module Az
to make the command avaliable to me.
For me, updating the AzureRM module did the trick:
Update-Module -Name AzureRM
Depending on your system, you may get some messages while installing
Powershell requires NuGet provider version '2.8.5.201' or newer to
interact with NuGet-based repositories. The NuGet provider must be
available in 'C:\Program Files\PackageManagement\ProviderAssemblies'
or
'C:\Users\<...>\AppData\Local\PackageManagement\ProviderAssemblies'.
You can also install the NuGet provider by running
Install-PackagePovider -Name NuGet -MinimumVersion 2.8.5.201 -Force'.
Do you want PowerShellGet to install and import the NuGet provider
now?
[Yes] {No] [Suspend]
Choose "Yes"
You are installling the modules form an untrusted repository. If you
trust this repository, change its installationPolicy value by running
the Set-PSRepository cmdlet. Are you sure you want to install the
modules from 'PSGallery'?
[Yes] [Yes to All] [No] [No to All] [Suspend]
Choose "Yes to all"
Up to three modules will be installed/updated and directly after that, the Select-AzureRmSubscription should work immediately.
Select-AzureRmSubscription b0cabaca-1234-1337-abcd-bebedada1337
# note: this subscription GUID is completely fictional.
# To get the correct one, query your subscriptions with
# Get-AzureRmSubscription
You have to make sure that certain Azure Powershell modules are installed for certain cmdlets to work. You can check which modules are available by running Get-Module -ListAvailable. Select-AzureRMSubscription requires AzureRM module.
You could just install Azure Powershell with its modules using webPI - I've never had problems using it and I've heard that installing PS modules using gallery could cause problems (see here).
You can run azure PowerShell with Azure PowerShell step/task.
Edit your build definition
Click Add build step
Select Deploy catalog > Add Azure PowerShell step/task
the OS in question is server 2012
To install PowerShell on windows server 2012, we should add the .NET Framework 3.5 feature and add the windows PowerShell 2.0 Engine feature.
About how to add features, refer to the link.
Then we can download PowerShell installation package, and install it.
Then we can use windows server 2012 PowerShell to login azure.
You need to use the Set-AzContext,
Select-AzSubscription -SubscriptionName 'SubName' | Set-AzContext
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.