简体   繁体   中英

Old working script in powershell doesnt work after upgrade to powershell-core (v 7.2) and remove previous one

This is a script to automatically turn on mobile hotspot if its not (from this answer )

$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
    $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
    $netTask = $asTask.Invoke($null, @($WinRtTask))
    $netTask.Wait(-1) | Out-Null
    $netTask.Result
}
Function AwaitAction($WinRtAction) {
    $asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod })[0]
    $netTask = $asTask.Invoke($null, @($WinRtAction))
    $netTask.Wait(-1) | Out-Null
}

$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation, Windows.Networking.Connectivity, ContentType = WindowsRuntime]::GetInternetConnectionProfile()
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager, Windows.Networking.NetworkOperators, ContentType = WindowsRuntime]::CreateFromConnectionProfile($connectionProfile)
if ($tetheringManager.TetheringOperationalState -eq 1) {
    "Hotspot is already On!"
}
else {
    "Hotspot is off! Turning it on"
    Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult])
}

now after upgrading powershell to version 7.2 and removing previous one it doesnt work by throwing these errors:

MethodInvocationException: D:\Hotspot.ps1:3:78
Line |
   3 |  … ods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1  …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "GetParameters" with "0" argument(s): "Operation is not supported on this platform. (0x80131539)"

InvalidOperation: D:\Hotspot.ps1:3:1
Line |
   3 |  $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods( …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.

InvalidOperation: D:\Hotspot.ps1:16:22
Line |
  16 |  … onProfile = [Windows.Networking.Connectivity.NetworkInformation, Wind …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to find type [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,
     | ContentType=WindowsRuntime].

InvalidOperation: D:\Hotspot.ps1:17:21
Line |
  17 |  … ngManager = [Windows.Networking.NetworkOperators.NetworkOperatorTethe …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to find type
     | [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,
     | ContentType=WindowsRuntime].

Hotspot is off! Turning it on
InvalidOperation: D:\Hotspot.ps1:23:5
Line |
  23 |      Await ($tetheringManager.StartTetheringAsync()) ([Windows.Network …
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Method invocation failed because [System.String] does not contain a method named 'StartTetheringAsync'.

If installing the previous one is not an option, any new updates for this script working in powershell-core?

That is due to running code built for .NET Framework 4.x with powershell 6.x or 7.0.

Wrap your script with the following and it should work.

$session = New-PSSession -UseWindowsPowerShell
Invoke-Command -Session $session {
    enter code here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM