powershell中的旧工作脚本在升级到powershell核心(v7.2)并删除以前的脚本后不起作用



这是一个脚本,如果移动热点不是,它会自动打开

$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])
}

现在,在将powershell升级到7.2版本并删除之前的版本后,它不会因为抛出以下错误而工作:

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'.

如果安装上一个脚本不是一个选项,那么在powershell核心中对此脚本有任何新的更新吗?

这是由于运行了为带有powershell 6.x或7.0的.NET Framework 4.x构建的代码。

用以下内容包装您的脚本,它应该可以工作。

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

最新更新