我在这里看到了一些问题,但没有看到任何回应。我有一个显示脚本进度窗口的类文件,它在PowerShell版本5.1.15063.966中工作正常但在PWSH版本6.0.2中失败。
我不确定它是否相关,但由于我在一个类中使用它,我必须预先加载PresentationFramework,然后(点)源包含我的类文件。
测试(V5):
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
PS C:\Users\fake> [void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework');
PS C:\Users\fake> $newRunspace = [RunspaceFactory]::CreateRunspace();
PS C:\Users\fake> $newRunspace.ThreadOptions = "ReuseThread";
PS C:\Users\fake> $newRunspace.ApartmentState = "STA";
PS C:\Users\fake>
测试(V6):
PowerShell v6.0.2
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS C:\Temp\PS\PowerShell-6.0.2-win-x64> [void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework');
PS C:\Temp\PS\PowerShell-6.0.2-win-x64> $newRunspace = [RunspaceFactory]::CreateRunspace();
PS C:\Temp\PS\PowerShell-6.0.2-win-x64> $newRunspace.ThreadOptions = "ReuseThread";
PS C:\Temp\PS\PowerShell-6.0.2-win-x64> $newRunspace.ApartmentState = "STA";
The property 'ApartmentState' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1
+ $newRunspace.ApartmentState = "STA";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
PS C:\Temp\PS\PowerShell-6.0.2-win-x64>
我在两个版本中都做了一个GM,它们真的不同:
V5:
PS C:\Users\Fake> $newRunspace = [RunspaceFactory]::CreateRunspace();
PS C:\Users\Fake> $newRunSpace | gm
TypeName: System.Management.Automation.Runspaces.LocalRunspace
Name MemberType Definition
---- ---------- ----------
AvailabilityChanged Event System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceAvailab...
StateChanged Event System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceStateEv...
ClearBaseTransaction Method void ClearBaseTransaction()
Close Method void Close()
CloseAsync Method void CloseAsync()
Connect Method void Connect()
ConnectAsync Method void ConnectAsync()
CreateDisconnectedPipeline Method System.Management.Automation.Runspaces.Pipeline CreateDisconnectedPipeline()
CreateDisconnectedPowerShell Method powershell CreateDisconnectedPowerShell()
CreateNestedPipeline Method System.Management.Automation.Runspaces.Pipeline CreateNestedPipeline(), Syst...
CreatePipeline Method System.Management.Automation.Runspaces.Pipeline CreatePipeline(), System.Man...
Disconnect Method void Disconnect()
DisconnectAsync Method void DisconnectAsync()
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
GetApplicationPrivateData Method psprimitivedictionary GetApplicationPrivateData()
GetCapabilities Method System.Management.Automation.Runspaces.RunspaceCapability GetCapabilities()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
Open Method void Open()
OpenAsync Method void OpenAsync()
ResetRunspaceState Method void ResetRunspaceState()
SetBaseTransaction Method void SetBaseTransaction(System.Transactions.CommittableTransaction transacti...
ToString Method string ToString()
ApartmentState Property System.Threading.ApartmentState ApartmentState {get;set;}
ConnectionInfo Property System.Management.Automation.Runspaces.RunspaceConnectionInfo ConnectionInfo...
Debugger Property System.Management.Automation.Debugger Debugger {get;}
DisconnectedOn Property System.Nullable[datetime] DisconnectedOn {get;}
Events Property System.Management.Automation.PSEventManager Events {get;}
ExpiresOn Property System.Nullable[datetime] ExpiresOn {get;}
Id Property int Id {get;}
InitialSessionState Property initialsessionstate InitialSessionState {get;}
InstanceId Property guid InstanceId {get;}
JobManager Property System.Management.Automation.JobManager JobManager {get;}
LanguageMode Property System.Management.Automation.PSLanguageMode LanguageMode {get;set;}
Name Property string Name {get;set;}
OriginalConnectionInfo Property System.Management.Automation.Runspaces.RunspaceConnectionInfo OriginalConnec...
RunspaceAvailability Property System.Management.Automation.Runspaces.RunspaceAvailability RunspaceAvailabi...
RunspaceConfiguration Property System.Management.Automation.Runspaces.RunspaceConfiguration RunspaceConfigu...
RunspaceIsRemote Property bool RunspaceIsRemote {get;}
RunspaceStateInfo Property System.Management.Automation.Runspaces.RunspaceStateInfo RunspaceStateInfo {...
SessionStateProxy Property System.Management.Automation.Runspaces.SessionStateProxy SessionStateProxy {...
ThreadOptions Property System.Management.Automation.Runspaces.PSThreadOptions ThreadOptions {get;set;}
Version Property version Version {get;}
V6:
PS C:\Temp\PS\PowerShell-6.0.2-win-x64> $newRunspace = [RunspaceFactory]::CreateRunspace();
PS C:\Temp\PS\PowerShell-6.0.2-win-x64> $newRunSpace | gm
TypeName: System.Management.Automation.Runspaces.LocalRunspace
Name MemberType Definition
---- ---------- ----------
AvailabilityChanged Event System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceAvailab...
StateChanged Event System.EventHandler`1[System.Management.Automation.Runspaces.RunspaceStateEv...
Close Method void Close()
CloseAsync Method void CloseAsync()
Connect Method void Connect()
ConnectAsync Method void ConnectAsync()
CreateDisconnectedPipeline Method System.Management.Automation.Runspaces.Pipeline CreateDisconnectedPipeline()
CreateDisconnectedPowerShell Method powershell CreateDisconnectedPowerShell()
CreateNestedPipeline Method System.Management.Automation.Runspaces.Pipeline CreateNestedPipeline(), Syst...
CreatePipeline Method System.Management.Automation.Runspaces.Pipeline CreatePipeline(), System.Man...
Disconnect Method void Disconnect()
DisconnectAsync Method void DisconnectAsync()
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
GetApplicationPrivateData Method psprimitivedictionary GetApplicationPrivateData()
GetCapabilities Method System.Management.Automation.Runspaces.RunspaceCapability GetCapabilities()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
Open Method void Open()
OpenAsync Method void OpenAsync()
ResetRunspaceState Method void ResetRunspaceState()
ToString Method string ToString()
ConnectionInfo Property System.Management.Automation.Runspaces.RunspaceConnectionInfo ConnectionInfo...
Debugger Property System.Management.Automation.Debugger Debugger {get;}
DisconnectedOn Property System.Nullable[datetime] DisconnectedOn {get;}
Events Property System.Management.Automation.PSEventManager Events {get;}
ExpiresOn Property System.Nullable[datetime] ExpiresOn {get;}
Id Property int Id {get;}
InitialSessionState Property initialsessionstate InitialSessionState {get;}
InstanceId Property guid InstanceId {get;}
JobManager Property System.Management.Automation.JobManager JobManager {get;}
LanguageMode Property System.Management.Automation.PSLanguageMode LanguageMode {get;set;}
Name Property string Name {get;set;}
OriginalConnectionInfo Property System.Management.Automation.Runspaces.RunspaceConnectionInfo OriginalConnec...
RunspaceAvailability Property System.Management.Automation.Runspaces.RunspaceAvailability RunspaceAvailabi...
RunspaceIsRemote Property bool RunspaceIsRemote {get;}
RunspaceStateInfo Property System.Management.Automation.Runspaces.RunspaceStateInfo RunspaceStateInfo {...
SessionStateProxy Property System.Management.Automation.Runspaces.SessionStateProxy SessionStateProxy {...
ThreadOptions Property System.Management.Automation.Runspaces.PSThreadOptions ThreadOptions {get;set;}
Version Property version Version {get;}
我的问题是如何在V6中执行等效操作?