Powershell not always running ScriptsToProcess on import - powershell

After using Install-Module to install powershell-yaml I had about a week of use before I came in one morning to it not loading YamlDotNet properly.
Now if I try to use ConvertFrom-Yaml before manually running Import-Module powershell-yaml I get the the following error:
PS C:\Users\user> "---" | ConvertFrom-Yaml
New-Object : Cannot find type [YamlDotNet.RepresentationModel.YamlStream]: verify that the assembly containing this type is loaded.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:24 char:23
+ ... $yamlStream = New-Object "YamlDotNet.RepresentationModel.YamlStream"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:25 char:9
+ $yamlStream.Load([System.IO.TextReader] $stringReader)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Previously (and on most systems) this worked fine. If I run a manual Import-Module before calling the function it works fine:
PS C:\Users\user> Import-Module powershell-yaml
PS C:\Users\user> "---" | ConvertFrom-Yaml
PS C:\Users\user>
Both my and a coworker's machines started this behavior at about the same time. This morning my system started working properly again, but his is still exhibiting the behavior. We haven't been able to duplicate it on other machines.
I've narrowed it down to a script referenced in Powershell-Yaml manifest's ScriptsToProcess not being called during autoload, but runs fine during a manual Import-Module. On working machines the script in ScriptsToProcess is run in both cases. As a workaround we can force a module load by putting an Import-Module in our profile, ideally we'd like to find a root cause.
Import-Module -Verbose doesn't help, because calling Import-Module first always works.

Related

powershell Microsoft.PowerShell.Utility module could not be loaded

I have this problem in a brand new workstation with Windows 10 installed: on powershell, the 5.1, when I call "Write-Host 'test'" I receive
Write-Host : The 'Write-Host' command was found in the module
'Microsoft.PowerShell.Utility', but the module could not
be loaded. For more information, run 'Import-Module Microsoft.PowerShell.Utility'.
At line:1 char:1
+ Write-Host "test"
+ ~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Write-Host:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
And if I try to Import-Module I receive an error.
I installed the powershell 7.2 and in that case I have no problem, but I must use the 5.1 for compatibility reasons.
I already tried to remove/reinstall the powershell windows feature but no way.
Does anyone have a suggestion?
Thx

Get-Help cmdlet not working with implicit remoting

Summary:
I have several functions that are placed on a remote windows server built for implicit remoting. However, I am unable to utilize the Get-Help cmdlet to show the synopsis that I put into each of the functions that I created, so these are not native powershell cmdlets. The get-help cmdlet works fine with the scripts are running locally.
Question:
Is it not possible to use Get-Help with implicit remoting?
Edit 1.
Attempting Briantists fix
PS> $module = Import-Module 'tmp_2c0mhyix.ivb' -PSSession $sessVar -PassThru
Import-Module : Failure from remote command: Import-Module -Name 'tmp_2c0mhyix.ivb': The specified module 'tmp_2c0mhyix.ivb' was not loaded because no valid module file was found in any module directory.
At line:1 char:11
+ $module = Import-Module 'tmp_2c0mhyix.ivb' -PSSession $sessVar-Pa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (tmp_2c0mhyix.ivb:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Tried this as well with out the module name as well.
PS> $module = Import-Module -PSSession $sessVar-PassThru
Import-Module : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:11
+ $module = Import-Module -PSSession $sessVar-PassThru
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Import-Module], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ImportModuleCommand
Implicit remoting is a strange beast. It creates proxy functions in a temporary module, and it's the proxy functions that get called locally.
After you call Import-PSSession, call Get-Module and you'll see one with a weird tmp name.
Alternatively, you can import the module initially using this method $module = Import-Module -PSSession $mySession -PassThru to get the module returned in a variable.
Then you can call Get-Command -Module $module to see the functions, but check out the definition:
Get-Command -Module $module | Select-Object -First 1 -ExpandProperty Definition
Shay Levy goes into detail about proxy functions here, and you can see that they do include instructions for Get-Help so that it can find the right help topic, but when the command is on the other side of the remoting, I don't think those would work.
I don't know if I've ever tried using help for functions imported that way, so maybe it actually does work and it's just a bug you've found, but I feel like this info would still be helpful.

Where is the best place to Add-PSSnapin SqlServerCmdletSnapin* and SqlServerProviderSnapin*?

I created some psm1 files and I put the following lines at the top of every psm1 file,
Set-StrictMode -Version 2
Add-PSSnapin SqlServerCmdletSnapin*
Add-PSSnapin SqlServerProviderSnapin*
However, it got warning of
WARNING: The names of some imported commands from the module 'mymodule' include unapproved verbs that might make them
less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose
parameter. For a list of approved verbs, type Get-Verb.
And it got the following error if import the modules multiple times.
Add-PSSnapin : An item with the same key has already been added.
At line:1 char:1
+ Add-PSSnapin SqlServerCmdletSnapin*
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-PSSnapin], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.AddPSSnapinCommand
Add-PSSnapin : An item with the same key has already been added.
At line:2 char:1
+ Add-PSSnapin SqlServerProviderSnapin*
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-PSSnapin], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.AddPSSnapinCommand
Or is there a way make these snapins loaded for every users when the system is booted?
The warning simply says that authors of SqlServerCmdletSnapin and SqlServerProviderSnapin used some 'unapproved' names for their functions. But these functions will work just fine, though MS thinks they can be 'less discoverable' (when user search commands like writing "Get-" and pressing Tab-Tab-Tab... from PS console).
So you are right to put them on the top, just add -ErrorAction SilentlyContinue to get rid of the warning:
Add-PSSnapin SqlServerCmdletSnapin* -ErrorAction SilentlyContinue
Add-PSSnapin SqlServerProviderSnapin* -ErrorAction SilentlyContinue
As for loading these snapins for every user, there are several options to do that using profiles. Read Understanding the Six PowerShell Profiles article.

Import-Module stops working sporadically

I have a problem where Import-Module -Name "./MyAssembly.dll" stops working sporadically. If I restart the computer, everything seems to work as normal for a while, but it's getting really frustrating.
The reason for importing the same module (in different Powershell sessions) is because I'm currently writing a cmdlet using C#, and I start a powershell instance as part of debugging the cmdlet.
The Import-Module command gives me no feedback whatsoever of whether or not the loading succeeded.
PS C:\Source\MyProject> Import-Module -Name "./MyAssembly.dll"
PS C:\Source\MyProject> Get-Stuffz
Get-Stuffz : The term 'Get-Stuffz' is not recognized as the name of a cmdlet, function, script file, or ope
rable 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
+ Get-Stuffz
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Stuffz:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Any help or suggestions are welcome.

Powershell command not working when run by octopus deploy

I have several steps to figure out this problem and I do't know what else to do. My end goal is to check my "app.exe" file version. when I run it on server manually both script versions work. But when Octopus runs both versions fail with different errors
Version 1:
$file = "C:\Octopus\Applications\AppStartUp\38.2.19.0\app.exe"
$fileVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($file).FileVersion
I get the error:
return [System.Diagnostics.FileVersionInfo]::GetVersionInfo <<<< ($file). FileVersion
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : DotNetMethodException
Version 2:
$file = "C:\Octopus\Applications\AppStartUp\38.2.19.0\app.exe"
(Get-Command "$file").FileVersionInfo.Fileversion
Error Message
Export-ModuleMember : The Export-ModuleMember cmdlet can only be called from in side a module. At C:\Octopus\Applications\CSIS_DEV\ESG.CSIS.StartUp\38.2.19.0\deployment\clickOnceLib.ps1:35 char:20
+ Export-ModuleMember <<<< -function * -alias *
+ CategoryInfo : PermissionDenied: (:) [Export-ModuleMember], InvalidOperationException
+ FullyQualifiedErrorId : Modules_CanOnlyExecuteExportModuleMemberInsideAModule,Microsoft.PowerShell.Commands.ExportModuleMemberCommand
Get-Command : The term 'C:\Octopus\Applications\CSIS_DEV\ESG.CSIS.StartUp\38.2.19.0\ESG.CSIS.StartUp.exe' 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:\Octopus\Applications\CSIS_DEV\ESG.CSIS.StartUp\38.2.19.0\deployment\clickOnceLib.ps1:32 char:24
+ return (Get-Command <<<< "$file").FileVersionInfo.Fileversion
+ CategoryInfo : ObjectNotFound: (C:\Octopus\Appl...SIS.StartUp.exe:String) [Get-Command], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
Any Insite to these messages and why it happens only when running in octopus would be great.
It looks like your script can't access the file, or it doesn't exist. Check to make sure C:\Octopus\Applications\CSIS_DEV\ESG.CSIS.StartUp\38.2.19.0\ESG.CSIS.StartUp.exe exists and is accessible to the user the tentacle runs under (use windows effective permissions).
Also make sure it's there by the time the script runs, you haven't mentioned whether this is a seperate step, PreDeploy.ps1, Deploy.ps1 or PostDeploy.ps1 script