I'm trying to emulate powershell version 2.0 in version 4.0
The following just freezes the application:
PS H:\> powershell.exe -version 2.0
I do get the following text but and the console continues to run and I am unable to execute further script.
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
As far as I'm aware, the command window in ISE is not a real console window. It's just pretending that it is. In reality, it's running the commands in the background and copying the output to the command window.
Note that when you try to run cmd.exe, you get this:
Cannot start "cmd". Interactive console applications are not supported.
To run the application, use the Start-Process cmdlet or use "Start PowerShell.exe" from the File menu.
To view/modify the list of blocked console applications, use $psUnsupportedConsoleApplications, or consult online help.
At line:0 char:0
Similarly, running powershell.exe alone gives me this:
Cannot start "powershell". Interactive console applications are not supported.
To run the application, use the Start-Process cmdlet or use "Start PowerShell.exe" from the File menu.
To view/modify the list of blocked console applications, use $psUnsupportedConsoleApplications, or consult online help.
At line:0 char:0
I'm not sure why powershell -version 2.0 works. The ISE must think you're calling it non-interactively.
Note that it's not freezing. It's waiting for the process to end. You can end it by pressing Ctrl+C.
Using Start-Process powershell.exe -ArgumentList "-Version 2.0" opens a new console window.
Related
I am working on a scheduled restart toasts using the Burnt Toast module. To add a selection box to have them schedule a time to do the restart(1 hour, 2 hours, etc..) and handle the action events, I need Powershell 7.1 or above. I have code that works when I run it directly from powershell 7.2.
The issue I have is that when we send this to our user's computers, the script will run in powershell 5.1, not 7.2. No way we can change that. I get a message saying "Toast events are only supported on Powershell 7.1.0 and above. Your notification will still be displayed, but the actions will be ignored." And like it says, it displays the toast but does not do the action.
What I am trying to do now is use either powershell 5.1 or command script to call powershell 7.2 so the script can run that way.
In powershell 5.1, I am running this command:
Invoke-Expression -Command "C:'Program Files'\PowerShell\7\pwsh.exe -Command C:\Setup\Scripts\ScheduledRestartToast.ps1"
In command script, I am running this command:
"c:\Program Files\PowerShell\7\pwsh.exe" -command "c:\Setup\Scripts\ScheduledRestartToast.ps1"
Both of these display the toast, but neither of them actually does the action.
Is it possible to have Powershell 7 run actionable scripts this way?
Thanks
I found that If I do this instead:
$ConsoleCommand = "C:\Setup\Scripts\ScheduledRestartToast.ps1"
Start-Process pwsh -ArgumentList "-NoExit","-Command &{ $ConsoleCommand }" -WindowStyle Hidden
it will keep the window open until they perform the action.
I am trying to create an executable (.exe) at windows that will perform some actions at first, and then open an interactive shell. In my case the interactive shell I want to be a wsl, but the problem is shown with every type of shells created.
For example:
Supposing that we have the file
test.ps1
Write-Host "Hello from my test program"
Invoke-Expression "powershell"
If I run it with the command: ./test.ps1, the outcome will be to print the message and start a new powershell instance.
If i run the command: Invoke-ps2exe .\test.ps1 test.exe, and then run the test.exe, the outcome will be:
Hello from my test program
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
but I cannot write anything at the window.
Can anyone explain me why this is happening, and how I could overcome this?
Modify your test.ps1 file as follows:
Write-Host "Hello from my test program"
Start-Process -Wait -NoNewWindow powershell.exe
This change ensures that PowerShell's usual interactive host, ConsoleHost, in the newly launched PowerShell session launched from your script doesn't run on top of the simplified host that is built into the .exe files that ps2exe generates, which appears to cause your problems.
This is really basic, but I can't find the answer. The installer sets up my path so that I can just type the command:
ng serve
at the command prompt and the script runs. I don't want to wait for this program to finish (it's a server, after all). How do I launch the same script (it's a CMD script as far as I can tell) from Powershell without waiting for it to finish (and without having to find the source directory for the script)?
If it's acceptable to terminate the server when the PowerShell session exits, use a background job:
In PowerShell (Core) 7+
ng server &
In Windows PowerShell, explicit use of Start-Job is required:
Start-Job { ng server }
Both commands return a job-information object, which you can either save in a variable ($jb = ...) or discard ($null = ...)
If the server process produces output you'd like to monitor, you can use the Receive-Job cmdlet.
See the conceptual about_Jobs topic for more information.
If the server must continue to run even after the launching PowerShell session exits, use the Start-Process cmdlet, which on Windows launches an independent process in a new console window (by default); use the -WindowStyle parameter to control the visibility / state of that window:
Start-Process ng server # short for: Start-Process -FilePath ng -ArgumentList server
Note: On Unix-like platforms, where Start-Process doesn't support creating independent new terminal windows, you must additionally use nohup - see this answer.
When using PowerShell ISE with Chocolatey to install applications, if I forget the -y switch, it hangs waiting on some sort of "confirmation" that's not popping up anywhere?
I have to Ctrl+Alt+Del to kill PowerShell ISE and Chocolatey and it leaves things in half-way state.
This is what it looks like below:
In addition to the comments to the OP above, regarding PowerShell ISE not supporting (most) interactive console applications...
It is worth remembering that the REPL window in PowerShell_ISE.exe is not just some sort of docked PowerShell.exe console. Most of the time the user experience is the same, but this hides a number of differences:
https://blogs.msdn.microsoft.com/powershell/2009/04/17/differences-between-the-ise-and-powershell-console/
Both these executables are host applications that run a PowerShell runspace (engine). You can even write your own application that "hosts" PowerShell. It is the host application that determines the user experience.
PowerShell.org: The Shell vs The host
Spiceworks.com: The Shell vs The Host
Writing a Windows PowerShell Host
And finally, for the most curious:
How PowerShell works
I think I wrote this answer more for my own benefit; it's a useful refresher for me as I get asked this by colleagues every now and again...
It's simply because PoSH ISE is not a thing to use for user interactive .exe commands.
If you .exe or whatever expects a response, when in the ISE you have to provide it.
You can easily prove this is not a Chocolatey thing by trying any other .exe that kicks out interactive stuff. For example, just type:
nslookup in the script pane and F8 to run it, or type it in the console pane and hit enter
Either way, the console will just hang, waiting for a interactive response that you cannot provide.
You can still use interactive commands like nslookup in the PoSH ISE, but you have to provide all parameters. For example:
nslookup microsoft.com
nslookup -type=mx microsoft.com
nslookup -q=soa microsoft.com
PS 5.1 even kicks out an error message now.
nslookup
Cannot start "nslookup". Interactive console applications are not supported.
To run the application, use the Start-Process cmdlet or use "Start PowerShell.exe" from the File menu.
To view/modify the list of blocked console applications, use $psUnsupportedConsoleApplications, or consult online help.
At line:0 char:0
You can easily shell out to the PowerShell console host temporarily this way.
Here is a function I have in my profile for such efforts.
Function Start-ConsoleCommand
{
[CmdletBinding()]
[Alias('scc')]
Param
(
[string]$ConsoleCommand,
[switch]$PoSHCore
)
If ($PoSHCore)
{Start-Process pwsh -ArgumentList "-NoExit","-Command &{ $ConsoleCommand }" -Wait}
Else
{Start-Process powershell -ArgumentList "-NoExit","-Command &{ $ConsoleCommand }" -Wait}
}
So, just type
scc -ConsoleCommand choco install winmerge
It'll pop the console host and stay open until you close it.
Update
As per request of - Alex Kwitny
PoSHGet default has only two repositories,
nuget
PSGallery
but you can add your own or another.
You use the below cmdlets to make this happen.
I have not had to use Chocolatey in a while, but taking a quick look and my archives, the below is what I used
Set up chocolatey repository
Find-Module
Get-Module
Find-Package
Get-Package
Get-PackageProvider
Get-PackageSource
Get-PackageSource -Provider chocolatey
Register-PackageSource -Name chocolatey -Provider Chocolatey -Trusted -Location http://chocolatey.org/api/v2/ -Verbose
Find-Module
Get-Module
Find-Package
Get-Package
Invoke-SQLCMD cmdlet does not work when executed through powershell script (.ps1)
Following error is logged:
[error]Windows PowerShell is in NonInteractive mode. Read and Prompt
functionality is not available. [error]Process completed with exit
code 0 and had 1 error(s) written to the error stream.
When executed through interactive window it asks for user confirmation.
Is there a work around to pass the default value Yes when executed through scripts?
Install 'Windows PowerShell Extensions for SQL Server' from the Microsoft® SQL Server® 2008 R2 Feature Pack page (it's about halfway down the page). Make sure you pick the correct flavor for your instance (32 or 64 bit).
Run these two commands before calling invoke-sqlcmd in your script:
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100