Why does ansible win_shell module use powershell 5 when powershell 7 is set as the default? - powershell

I'm running a packer build of windows using ansible to provision the VM.
I connect to the VM via OpenSHH. I have powershell 7 set as the default shell for OpenSSH. When I connect to the VM over ssh on the command line, the banner says PowerShell 7.1.3.
I run the following code via ansible-playbook:
- name: Check powershell version
ansible.windows.win_shell: |
Get-Host | Select-Object Version
- name: Check powershell version
ansible.windows.win_shell: |
Get-Host | Select-Object Version
args:
executable: pwsh
The first task returns 5.1.19041.1023
The second task returns 7.1.3
So when I don't specify the executable, win_shell is running powershell.exe, aka powershell 5.
When I do specify the executable as pwsh, aka powershell 7, it behaves correctly.
I can't figure out why the win_shell module is ignoring the default set in the registry. Does anyone know what I have done wrong?

Related

The term 'oh-my-posh' is not recognized as a name of a cmdlet

I have configured oh-my-posh to be working on powershell and all is working as expected if I run powershell from windows terminal (which is what the document say clearly)
However I tend to run powershell from my WSL terminal as I have alias like this
psl='/mnt/c/Program\ Files/PowerShell/7/pwsh.exe'
but now when I run that alias I got the following error
oh-my-posh: C:\Users\username\OneDrive\Documents\PowerShell\Microsoft.PowerShell_profile.ps1:19
Line |
19 | oh-my-posh init pwsh --config 'C:\Users\username\OneDrive\Documents\Powe …
| ~~~~~~~~~~
| The term 'oh-my-posh' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the
| name, or if a path was included, verify that the path is correct and try again
Any idea ?
Thanks
I tried to change the path but it seems the terminal is not recognize the oh-my-posh
On Windows, validate the location of the executable using Get-Command oh-my-posh and add that to the PATH when starting PowerShell, preferably by adding that to your $PROFILE.
$env:Path += ";/mnt/c/Program\ Files/exact/location/oh-my-posh.exe"
That said, it's discouraged to use the Windows version of oh-my-posh on WSL as that will be slower than a native linux build. Same goes for the Windows version of PowerShell.

Trying to remove alias when running Powershell with NoProfile parameter doesn't work

I am trying to remove the alias for 'echo -> Write-Output' from Powershell using Remove-Item alias:echo when running command in 'Powershell -NoProfile' but it does not work, it still references the Write-Output alias. I am trying to run it in GitLab CI and GitLab runs all powershell commands using the 'NoProfile' parameter.

.Bat file unable to run powershell 7

I have set up a very simple .bat file to execute a couple of commands to save me typing them out every time, however the processes need to be run in powershell 7.
If i manually run powershell 7.0.3 and then run the commands everything work, however running the .bat script starting
powershell -Version 7.0.3 -Command {XXXXX};
presents me with a message "Cannot start Windows PowerShell version 7.0.3 because it is not installed."
If i try and run it without the version number then it runs in 5.1.x and this then fails as it requires 6+.
tl;dr
As Lee_Dailey notes, you must use pwsh.exe, not powershell.exe, to start a version of PowerShell [Core] v6+ and you must invoke the desired version's specific executable.
In the simplest case:
pwsh -Command "XXXXX"
Note that I've replaced {XXXXX} with "XXXXX", because you cannot directly execute script blocks ({...}) from outside PowerShell - just supply the commands as a string.
Given that - unlike with Windows PowerShell - you can install multiple PowerShell [Core] versions side by side:
Run pwsh -version (sic; see below) to report the version in your system's path (the instance that comes first among the directories listed in the PATH environment variable, $env:PATH).
If it is not the one you want to target, you'll have to invoke it via its full path:
If you want to rely on the standard installation location, you can use the following on Windows for version 7.0: "C:\Program Files\PowerShell\7\pwsh.exe"
To determine the target version's executable location reliably, open an interactive console for it and run (Get-Process -Id $PID).Path
The -Version parameter of powershell.exe, the Windows PowerShell CLI, does not allow you to start just any PowerShell version, only an older version of Windows PowerShell:
In fact, the only supported argument is -Version 2, and even that will only succeed if you have previously installed the required legacy versions of .NET Framework.
Caveat: While versions higher than v5.1 - the latest and last Windows PowerShell version - sensibly result in an error (the one you saw), unsupported lower versions are quietly ignored; in effect, -Version 1 and -Version 2 will both start version 2.0, whereas -Version 3, -Version 4 and -Version 5 are effectively ignored and run v5.1 - verify with $PSVersionTable.PSVersion
While a -Version parameter still exists in pwsh.exe, the PowerShell [Core] v6+ CLI, its meaning has changed:
It now simply reports a version number, namely the targeted executable's own (and therefore takes no argument).

How to force a Powershell Script to run a specific Version

I have both Powershell 5.1 and Powershell 7.0 on my computer and I'm try to run a script that has to be run in Powershell 7.0. How do I make the script run in a specific version.
If it's a script being invoked in a PS shell, you could relaunch it if it's not the correct version.
# At beginning of .ps1
if ($PSVersionTable.PSVersion -ne [Version]"5.1") {
# Re-launch as version 5 if we're not already
powershell -Version 5.1 -File $MyInvocation.MyCommand.Definition
exit
}
# Your script code
If the scripts are launched via Task Scheduler, you could just use the full path to the .exe in the "Actions -> Start a program" section, as they have separate install locations.
another option is to use #Requires statement
#Requires -Version 7.0
here is the reference from Microsoft support
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires

Windows Powershell Command to show PHP process

I'm new in PHP. I'm following PHP with MySQL Beyond the basics Lynda tutorial. They used another command line environment on mac OS. But I'm using Windows powershell. They run the command ps aux grep httpd on those environment. Which is not for windows powershell.
What is the alternate windows power shell command for ps aux grep httpd?
Use the get-process cmdlet
get-process httpd
You can also use wildcards. For example, if you want all the process names that begin with h:
get-process h*