New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PowerShell ps1 script to manage bluetooth status stopped working #16731
Comments
This
suggests you've cut and pasted something tied to .NET Framework 4 into the .NET 6. version of PowerShell. It simplifies to
And the equivalent in C# would give the same error if compiled with the current .NET. |
Fine, but how to I set inside the PowerShell script to use .NET version 4.x instead? Or just autodetect? |
Lines
and
are not equivalent:
Please make tests before reporting invalid bugs. |
It is know issue. WinRT support was removed from .Net 6.0 and as result from PowerShell 7.2 too. |
How can I tell PowerShell to use .NET 4.x from inside the script instead of "default" 6.x? |
Only way is to run Windows PowerShell. PS: there is separate project to support WinRT in .Net but MSFT PowerShell team want to keep PowerShell as small as possible so we could expect WinRT support will be added as an optional component/module in future. |
Can I run Windows PowerShell from inside the ps1 script? |
Here is your error
What method is being called, and what expression is null-valued ? It's
If I paste those into PowerShell 7.2.1 I get multiple instances of
OK.
OK
OK
.NET 6 is hard wired into PowerShell 7.2 , just as Dot net framework 4 is hardwired into Windows PowerShell 5.1 I can't find where I used it but somewhere I have some code like this
Which will run the same ps1 with the same parameters in the other version of PowerShell. |
You may be able to do this: $session = New-PSSession -UseWindowsPowerShell
Invoke-Command -Session $session {
code here
} |
@SeeminglyScience what happens if I am running that script inside PowerShell 7.x and |
@superbonaci - that is almost never going to happen as it is part of the OS and whilst may become an optional feature, will be supported for at least another 10 years due to the Windows support lifecycle |
Your code works now if the script is run inside PowerShell Core 7.x, however is not backwards compatible with Windows PowerShell. It must be backward compatible:
|
They haven't removed cmd.exe or Wscript yet, so it's a bit early to be worrying about Windows PowerShell being removed. @SeeminglyScience that's good if you know the script will run in 7.2 but it won't run natively on 5. That's why I dropped back @superbonaci see the end of my previous comment for how to do that. |
Yeah for sure, I'm just offering a work around. You'll need to apply it selectively with version checks if you want it to run no matter what executable it's invoked in. The main issue of WinRT support being dropped is discussed in more detail in #13138. |
How can I convert all existing code to a function?
so |
Just insert this checking after param block. |
How would the code look like? |
|
That code asks twice for parameter if no arguments are given, which does not happen on original script. If argument is given then only asks one time. |
Close as dup #13138. |
This issue has been marked as duplicate and has not had any activity for 1 day. It has been closed for housekeeping purposes. |
Prerequisites
Steps to reproduce
Run this script in PowerShell 7.2.1:
https://superuser.com/a/1293303/298707
With either option
.\bluetooth.ps1 -BluetoothStatus On
or.\bluetooth.ps1 -BluetoothStatus Off
.Expected behavior
Run like in older PowerShell versions.
Actual behavior
Does not work.
Error details
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: