I am using TFS 2015 Release. I have a very simple script: it imports module and runs another script. The problem is I see Import-Module logs in TFS Release log and I cannot hide them. Example:
# ScriptRunner.ps1
Import-Module MyModule -DisableNameChecking -Global
& ".\script.ps1"
When I run this locally by using below command, I don't see any output from Import-Module command:
powershell "path\ScriptRunner.ps1"
In TFS Release I have "PowerShell on Target Machines" task, and I run the same script ScriptRunner.ps1. And this is what I see in the log:
Deployment started on target machine...
Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\MyModule\MyModule.psm1'
Exporting function 'My-Function-1'.
...
Exporting function 'My-Function-10'.
The 'My-Function-1' command in the MyModule module was imported, but because its name does not include an approved verb, it might be difficult to find. For a list of approved verbs, type Get-Verb.
The command name 'My-Function-1' from the module 'MyModule' contains one or more of the following restricted characters: # , ( ) { } [ ] & - / \ $ ^ ; : " ' < > | ? # ` * % + = ~
Importing function 'My-Function-1'.
...
How is this getting logged? I specify DisableNameChecking flag. I tried to hide this messages by changing Import-Module line in my script, but this doesn't help:
[void](Import-Module MyModule -DisableNameChecking -Global)
Import-Module MyModule -DisableNameChecking -Global | Out-Null
Import-Module MyModule -DisableNameChecking -Global -WarningAction SilentlyContinue
You can turn of $ErrorActionPreference flag to Silentlycontinue. That should fix the issue.
Please be aware that when you add this line to the script the whole script change the Error Action Preference.
$ErrorActionPreference = "Silentlycontinue"
Related
I am trying to install the PSReadLine module:
PS> Find-Module PSReadLine | Install-Module -scope currentUser -force
This command runs and terminates without any message or any indication that something was unsuccessful.
However, when I want to load the module, I get an error message:
PS> import-module PSReadLine
import-module : The specified module 'PSReadLine' was not loaded
because no valid module file was found in any module directory.
So, I am wondering how I can install and/or load this module.
Feedback on comments
As per #Lee_Dailey's comment, I do indeed have a path with a lowercase l:
PS> ls 'C:\Program Files\WindowsPowerShell\Modules\psread*' -name
PSReadline
Unfortunately, I cannot change that lowercase l to a L because I have this issue on a machine without admin privileges.
As per #Scepticalist comment, I should find the module in one of the module paths' locations. It turns out: I don't. The following command returns/prints nothing:
foreach ($mp in $Env:PSModulePath -split ';') { ls "$mp\psread*" }
Thus, also the command suggested by #pwnosh returns nothing:
($env:PSModulePath -split ';' | Get-ChildItem -Filter 'PSReadLine' | Get-ChildItem).FullName
Find-Module PSReadLine returns (echoes)
Version Name Repository Description
------- ---- ---------- -----------
2.1.0 PSReadLine PSGallery Great command line editing in the PowerShell console host
Running Register-PSRepository -Default results in an error message:
PackageManagement\Register-PackageSource : Module Repository 'PSGallery' exists.
Set-PSRepository 'PSGallery' -InstallationPolicy Trusted runs and terminates without any message echoed to the console. Yet, after running this command, I am still unable to import the module.
I am trying to run a powerhsell script (my_script). The script is stored in this_folder on the desktop. I am executing the command at the the correct path by using C:\Users\me\desktop\this_folder> .\my_script. When I try to do this I get the error
The term 'Add-AzureRmAccount' 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
I know that the Add-AzureRmAccount is a part of the AzureRM.Profile module. When I got to the modules folder, I see that AzureRM.Profile is listed. To get these modules, I ran Install-Module -name AzureRM and Import-module AzureRM.
Any ideas what might be going on?
---Update---
When I ran Get-Module commands I saw that only that AzureRM module was listed. I thne tried to run Install-Module -Name AzureRM.Profile and got the error:
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1772 char:21
+ ... $null = PackageManagement\Install-Package #PSBoundParameters
Since I see the AzureRM.Profile folder with a nupkg at one of the paths listed by $env:PSModulePath I thought I could just run Import-Module -Name AzureRM.Profile and I get the error
import-module : The specified module 'AzureRM.Profile' was not
loaded because no valid module file was found in any module directory.
Any ideas?
You say that you've installed and imported the AzureRM module, but have you connected the Azure account with the Connect-AzureRmAccount cmdlet?
I know a number of other modules does not expose all of the available cmdlets until there is an active connection to an account/subscription.
Be aware that scripts can ignore the context in which they are run so you might need to add the following to the start of the script:
Import-Module AzureRM
Connect-AzureRmAccount
Rather than running them manually in the console and then running the script.
You might want also want to look at using the Az module instead as the AzureRM module is being retired, and will only get bug-fixes till the end of December 2020: Introducing the new Azure PowerShell Az module
I am attempting to install the az cmdlet onto Kudu for my Azure Function. I am currently following this guide:
How to install a PowerShell module in an Azure Function
... however - I am still getting the following error within my Azure Function:
az : The term 'az' 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 run.ps1: line 1
Steps I have done till now:
Created a module folder under D:\home\site\wwwroot\Communication_with_Azure_Container_Registry\>
Within the module folder I have added the contents of azure-cli/2.0.35/..., (which looks like this):
... Azure Function code is very simple to proof out the ability to install the cmdlet:
if (-not (Get-Module -Name "az"))
{
Write-Output "azure-cli not installed";
}
else
{
Write-Output "azure-cli installed";
}
$test = 'az --help'
Invoke-Expression $test
Write-output `n$test
Question:
Is there something within my configuration that is not allowing for the az cmdlet to install?
Is there an alternative way to gain access to the azure-cli without implementing the node module?
I solved the following part of your problem
az : The term 'az' 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 run.ps1: line 1
If you execute
Test-Path -Path 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin'
You will probably get False. This means that you need to install the Azure CLI eg from https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest
I haven't testing this myself, but according to https://blogs.msdn.microsoft.com/powershell/2017/02/24/using-powershell-modules-in-azure-functions/ you should be able to do an Import-Module. In their example...
Write-Output “Loading Wunderlist Module”
import-module ‘D:\Home\site\wwwroot\HttpTriggerPowerShellDemo\Modules\Wunderlist\1.0.10\Wunderlist.psm1’
$Result = Get-Help Get-WunderlistUser | Out-String
Write-Output $Result
Install Azure CLI
https://learn.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
This should give you the az command
I always thought that doing Import-Module in my current powershell console will make the module available throughout the console.
Recently however I have learned that when doing Import-Module from within a module, things are getting complicated.
Compare:
Windows PowerShell Session State
...
Module Session State
Module session states are created whenever the module or one of its nested modules is imported into the session. When a module exports an element such as a cmdlet, function, or script, a reference to that element is added to the global session state of the session. However, when the element is run, it is executed within the session state of the module.
What I can observe, is:
# This is mod1.psm1
Import-Module -Force mod2.psm1
# end mod1.psm1
-
PS> Import-Module -Force mod1.psm1
... This will make the functions of mod2.psm1 available via mod1, when checked with Get-Module
PS> Import-Module -Force mod2.psm1
... This will make the functions of mod2 available via mod2.
I don't even fully understand the behavior here, but now comes the interesting part:
# This is mod1.psm1
function fn_load_in_mod1 {
Import-Module -Force mod2.psm1
# mod2 stuff can be used here
}
# end mod1.psm1
PS> Import-Module -Force mod1.psm1
PS> # But here, no mod2 stuff is available anymore!
I learned that to make the mod2 stuff available to my console, I need to:
# This is mod1.psm1
function fn_load_in_mod1 {
Import-Module -Force mod2.psm1 -Global
# mod2 stuff can be used here, and with the -Global switch also "outside"
}
# end mod1.psm1
But I can't make head nor tail of that session state stuff.
How am I supposed to do an Import-Module from within a module "correctly"?
I've gotten the following error when installing windirstat and ilmerge on Chocolatey. I'm suspicious that ampersand behavior changed in PowerShell 3.0.
Running powershell -NoProfile -ExecutionPolicy unrestricted -Command "& import-module -name 'C:\NuGet\chocolateyInstall\helpers\chocolateyInstaller.psm1'; & 'C:\NuGet\lib\ilmerge.2.10.526.4\tools\chocolateyInstall.ps1'". This may take awhile and permissions may need to be elevated, depending on the package.
ilmerge did not finish successfully. Boo to the chocolatey gods!
-----------------------
[ERROR] The expression after '&' in a pipeline element produced an invalid object. It must result in a command name, script block or Command
Info object.
-----------------------
The problem was not ampersand behavior I had to replace $oc = Get-Command 'Write-Host' | ?{$_.ModuleName -eq 'Microsoft.PowerShell.Utility'} with $oc = Get-Command 'Write-Host' -Module 'Microsoft.PowerShell.Utility' as well as make a similar replacement for a Write-Error wrapper.
How about starting PowerShell with the -version 1.0 or 2.0 parameter? Then run the above command and see if there is any difference.
At the commandline
PowerShell -version 2.0