TFS Powershell Install-Module not found - powershell

I've written the PS script below to 7zip a folder using 7Zip4Powershell however when during the build it fails with the following error:
Install-Module : The term 'Install-Module' 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.
At C:\Build\Work\19\s\DevOps\7zCompress.ps1:3 char:5
+ Install-Module -Scope CurrentUser -Name 7Zip4PowerShell -Verbose -Force
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Install-Module:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Compress-7Zip : The term 'Compress-7Zip' 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.
At C:\Build\Work\19\s\DevOps\7zCompress.ps1:18 char:1
+ Compress-7Zip -Path $sourcedir -ArchiveFileName $filename -CompressionLevel $com ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Compress-7Zip:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Process completed with exit code 0 and had 1 error(s) written to the error stream.
Script
param([string]$sourcedir, [string]$filename, [string]$compressionLevel, [string]$compressionMethod)
If (-not (Get-Module -ListAvailable -Name 7Zip4PowerShell)) {
Install-Module -Scope CurrentUser -Name 7Zip4PowerShell -Verbose -Force
}
If(Test-Path $filename) {
Remove-Item $filename
}
If (-not $compressionLevel) {
$compressionLevel = "Normal"
}
If (-not $compressionMethod) {
$compressionMethod = "Lzma2"
}
Compress-7Zip -Path $sourcedir -ArchiveFileName $filename -CompressionLevel $compressionLevel -CompressionMethod $compressionMethod
How do I get TFS 2015 On-Premises to recognize the Install-Module is a valid command? I've already installed the MSI for Install-Module on that machine. Do I perhaps need to add it as a build agent capability?

According to your error message , double check your powershell version in the build agent machine.
Cause from Powershell 5.0 onwards you , you will be able to use the cmdlet to Install-Module, Save-Module. If you are using the lower version of PS, will get similar error.
You could also manually RDP to the build agent with the build service account and run the PS script. This will narrow down if the issue is related to the environment or your build definition.

Related

How to fix powershell after installing VS Code

I already was asking a similar question here: https://superuser.com/questions/1613630/most-of-powershell-commands-not-working?noredirect=1
Basically, I wasn't able to fix the issue so I got a replacement computer. Brand new. Started setting it up and only did the following:
Installed VS Code
Turned on Powershell extension in VS Code
It prompted something about Package Manager needing an update or it will not work properly
Asked me to install NuGet to get the Package Manager
After that PowerShell 5.1 stopped working again.
Modules not autoloading, fresh instance of powershell does this:
PS C:\WINDOWS\system32> Write-Host
Write-Host : The term 'Write-Host' 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.
At line:1 char:1
+ Write-Host
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Write-Host:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Any ideas?
This fixed it for me:
(Get-Module -ListAvailable).Name | %{Import-Module -Name $_ -Force}
[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12
Install-Module -Name PackageManagement -Force -RequiredVersion 1.4.6 -Scope AllUsers -AllowClobber -Verbose
Then if the last command doesn't install NuGet do this:
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

How to resolve the Add-PSSnapin Microsoft.TeamFoundation.PowerShell and Get-TfsChildItem error?

Below is the PowerShell Script that I am using to download the folder from TFS 2013 Update 4 to my local machine but i am getting some exception which I have copied below the code.
$AutoDeployDir = "$/Intel/Installscript/Utility Scripts"
$deployDirectory = "C:\powershell scripts\New folder\Demo TFS Code"
# Add TFS 2013 dlls so we can download some files
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")
$tfsCollectionUrl = "http://stptfs2013dbsvr:8080/tfs/intelcollection"
$tfsCollection = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $tfsCollectionUrl
$tfsVersionControl = $tfsCollection.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer])
# Register PowerShell commands
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
# Get all directories and files in the AutoDeploy directory
$items = Get-TfsChildItem $AutoDeployDir -Recurse -Server $tfsCollection
# Download each item to a specific destination
foreach ($item in $items) {
# Serverpath of the item
Write-Host "TFS item to download:" $($item.ServerItem) -ForegroundColor Blue
$destinationPath = $item.ServerItem.Replace($AutoDeployDir, $deployDirectory)
Write-Host "Download to" $([IO.Path]::GetFullPath($destinationPath)) -ForegroundColor Blue
if ($item.ItemType -eq "Folder") {
New-Item $([IO.Path]::GetFullPath($destinationPath)) -ItemType Directory -Force
}
else {
# Download the file (not folder) to destination directory
$tfsVersionControl.DownloadFile($item.ServerItem, $([IO.Path]::GetFullPath($destinationPath)))
}
}
Exception that occurs is:
Add-PSSnapin : The Windows PowerShell snap-in 'Microsoft.TeamFoundation.PowerShell' is not installed on this computer.
At C:\powershell scripts\demo1.ps1:12 char:1
+ Add-PSSnapin Microsoft.TeamFoundation.PowerShell
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.TeamFoundation.PowerShell:String) [Add-PSSnapin], PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
Get-TfsChildItem : The term 'Get-TfsChildItem' 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.
At C:\powershell scripts\demo1.ps1:15 char:10
+ $items = Get-TfsChildItem $AutoDeployDir -Recurse -Server $tfsCollection
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-TfsChildItem:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Please make sure you have fully installed the TFS Powertools.
By default it doesn’t install the PowerShell CmdLets. If it is, just install it and then the issue will be gone.
Another possibility is that the PowerShell Snap-in is stored in the inconsistent registry with the OS version (32bit/64bit).
PowerTools installer is 32bit, on 64bit machine, it will write to
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\PowerShell\1\PowerShellSnapIns,
but not to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns.
Reference this similar thread for details: TFS Power Tools 2008 Powershell Snapin won’t run in on 64-bit in Windows 2008 R2?

Azure Powershell - Switch-AzureMode error in version 1

Hello I am not powershell programmer and I learning right now using pluralsight and exericese file giving me error and I think it happening because or version change of Azure Powershell from 0.9.8 to version 1.0
Here is error :
c:\Pluralsight\chef\2-chef> .\Create-CourseEnvironmentARM.ps1
Switch-AzureMode : The term 'Switch-AzureMode' 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.
At C:\Pluralsight\chef\2-chef\Create-CourseEnvironmentARM.ps1:28 char:1
+ Switch-AzureMode AzureResourceManager -Verbose
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Switch-AzureMode:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Test-AzureResourceGroup : The term 'Test-AzureResourceGroup' 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.
At C:\Pluralsight\chef\2-chef\Create-CourseEnvironmentARM.ps1:32 char:5
+ if((Test-AzureResourceGroup -ResourceGroupName $GroupName) -eq $false){
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Test-AzureResourceGroup:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
New-AzureResourceGroupDeployment : The term 'New-AzureResourceGroupDeployment' 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.
At C:\Pluralsight\chef\2-chef\Create-CourseEnvironmentARM.ps1:44 char:1
+ New-AzureResourceGroupDeployment `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (New-AzureResourceGroupDeployment:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
File Name :.\Create-CourseEnvironmentARM.ps1
I was searching error in google and confirm that azure power shell
deprecate function "Switch-AzureMode AzureResourceManager -Verbose"
https://github.com/Azure/azure-powershell/wiki/Deprecation-of-Switch-AzureMode-in-Azure-PowerShell
Switch-AzureMode AzureResourceManager -Verbose
Here is code from Create-CourseEnvironmentARM.ps1
Switch-AzureMode AzureResourceManager -Verbose
### Create Resource Group ###
if((Test-AzureResourceGroup -ResourceGroupName $GroupName) -eq $false){
New-AzureResourceGroup -Name $GroupName -Location $Location -Verbose
$ResourceGroup = Get-AzureResourceGroup -Name $GroupName
}
else {$ResourceGroup = Get-AzureResourceGroup -Name $GroupName}
$parameters = #{
'newStorageAccountName'="$StorageName";
'adminUsername'="$AdminUsername";
'dnsNameForPublicIP'="$PublicDNSName"
}
New-AzureResourceGroupDeployment `
-Name $DeploymentName `
-ResourceGroupName $ResourceGroup.ResourceGroupName `
-TemplateFile azuredeploy.json `
-TemplateParameterObject $parameters `
-Verbose
Please help me to correct this code. I think author is never update course and I am in middle of course. I hope someone help me to fix this problem.
There is no more "Switch-AzureMode" since v. 1.0.0 of the Powershell.
The ARM and ASM cmdlets exists together and live together. The ASM cmdlets are with unchanged names, but the ARM cmdlets are all now with RM in their name. Like:
Add-AzureRmAccount
Both the powershell cmdlets can be installed via Web Platform Installer.
More, when on Windows 10, the ARM cmdlets can be installed via PowerShell Gallery using the ofllowing commands (under Aministrative PowerShell console):
Install-Module AzureRM
Confirm all the questions being asked. Then run the
Install-AzureRM
Then you are done with the ARM module. Just make sure that your local execution policy is at least "RemoteSigned".
At the end, you will have to edit all your PowerShell scripts to match the new cmdlets and their parameters. Like the New-AzureResourceGroup is now New-AzureRmResourceGroup: https://msdn.microsoft.com/en-us/library/mt603739.aspx

Install and invoke Pester remotely

I'm new to PowerShell, Pester and the new VSO build system and I'm trying to get Pester to run my tests for each check in. I've set up and configured a simple PowerShell script to run on each check in.
I first tried simply Invoke-Pester to see if the VSO team had envisioned this and set up common unit test frameworks, but I go the following predictable result:
Invoke-Pester : The term 'Invoke-Pester' 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.
At C:\a\64df1c11\Operations Tools\powershell\runPowershellTests.ps1:1 char:1
+ Invoke-Pester
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-Pester:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I tried to use PSGet:
(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
install-module Pester
Invoke-Pester
But that isn't working either:
New-Item : Access to the path 'PsGet' is denied.
At line:17 char:5
+ New-Item -Path ($Destination + "\PsGet\") -ItemType Directory -Force | Out-N ...
How can I remotely install and execute Pester to run my unit tests?
Try this with PsGet:
(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex
Install-Module Pester -Global
Or use Chocolatey:
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
choco install pester
There is a task in the VSO marketplace that lets you run your Pester unit tests. The task can be found here at the marketplace. You can see the source code at: git Vsts-Build-Pester

New-Object : The term New-Object is not recognized as the name of a cmdlet

I want to configure high trusted app for app dev in SharePoint, and to do so, i need first to insert some commands in the powershell editor like :
$publicCertPath = "C:\Certs\HighTrustSampleCert.cer"
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($publicCertPath)
I am using windows PowerShell on Windows Server 2012 R2 which includes Windows PowerShell 4 that includes by default the new-object cmd-let... I don't understand though, why doesn't my operating system recognize that command ... I don't stop having the following error : New-Object : The term 'New-Object' is not recognized as the name of a cmdlet.
When i open powerShell i get this :
*select :
The term 'Select-Object' 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. At C:\Program Files\Common Files\Microsoft Shared\Web
Server Extensions\15\CONFIG\POWERSHELL\Registration\SharePoint.ps1:1
char:16
+ $ver = $host | select version
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (Select-Object:String) [], Comma ndNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException Set-location : The term 'Set-location' 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 At C:\Program Files\Common Files\Microsoft Shared\Web Server
Extensions\15\CONFIG\POWERSHELL\Registration\SharePoint.ps1:4 char:1
+ Set-location $home
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-location:String) [], Comman dNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException*
I thought that was normal until today... does it have any relation with the error? And here is the hole (new-object) exception stack:
New-Object : The term 'New-Object' 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. At line:1 char:16
+ $certificate = New-Object System.Security.Cryptography.X509Certificates.X509Cert ...
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (New-Object:String) [], CommandN otFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
ps: i want to mention that when i used enter-psSession and worked remotely, the command new-object was recognized but sharepoint commands (like Get-SPAuthenticationRealm) were no more recognized ... And it's like there is a problem related to the operating system.
It appears that your PowerShell installation is corrupted and needs to be repaired. The New-Object cmdlet is exported by the Microsoft.PowerShell.Utility module, which is one of the Core PowerShell modules and should be imported by default on all PowerShell installations.
This can be because the Registry key entry for PSModulesPath is not pre-filled with the default PowerShell Modules path.
$PSModulePath = Get-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -Name "PSModulePath"
$newPSModulePath = $PSModulePath.PSModulePath + ";C:\Windows\System32\WindowsPowerShell\v1.0\Modules"
Set-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -Name "PSModulePath" -value $newPSModulePath
Nothing verified here, but I am advancing the hypothesis that Powershell has run into a runtime error that caused it to corrupt its process.