在Windows 8 RTM上的.NET应用程序中嵌入Powershell v2.0

key*_*oke 23 .net powershell

我在尝试从Windows 7升级之前运行托管的PowerShell脚本时遇到以下错误我从未遇到此错误.

在加载扩展类型数据文件时出现以下错误:Microsoft.PowerShell.Core,C:\ WINDOWS\Syswow64资料\ WindowsPowerShell\V1.0\types.ps1xml(2977):在输入 "System.Security.AccessControl.ObjectSecurity" 错误:异常:getter方法应该是public,non void,static,并且有一个PSObject类型的参数.Microsoft.PowerShell.Core,C:\ Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(2984):类型"System.Security.AccessControl.ObjectSecurity"出错:异常:getter方法应该是public,非void ,static,并且有一个PSObject类型的参数.Microsoft.PowerShell.Core,C:\ Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(2991):类型"System.Security.AccessControl.ObjectSecurity"出错:异常:getter方法应该是公共的,非void ,static,并且有一个PSObject类型的参数.Microsoft.PowerShell.Core,C:\ Windows\SysWOW64\WindowsPowerShell\v1.0\types.ps1xml(2998):类型"System.Security.AccessControl.ObjectSecurity"出错:异常:getter方法应该是public,非void ,static,并且有一个PSObject类型的参数.Microsoft.PowerShell.Core,C:\的Windows\Syswow64资料\ WindowsPowerShell\V1.0\types.ps1xml(3005):异常:在类型 "System.Security.AccessControl.ObjectSecurity" 错误该吸气剂的方法应该是公共的,非空隙,static,并且有一个PSObject类型的参数.

我在App.config中应用了以下内容:

<dependentAssembly>
  <assemblyIdentity name="System.Management.Automation" publicKeyToken="31bf3856ad364e35" />
  <publisherPolicy apply="no" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

问题可能是什么?

key*_*oke 37

解决方案是执行以下操作,而不是仅按照我读过的帖子的建议为System.Management.Automation添加块,您需要为所有引用的PS程序集添加一个.

  <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Management.Automation" publicKeyToken="31bf3856ad364e35" />
            <publisherPolicy apply="no" />
          </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Microsoft.PowerShell.Commands.Utility" publicKeyToken="31bf3856ad364e35" />
          <publisherPolicy apply="no" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Microsoft.PowerShell.ConsoleHost" publicKeyToken="31bf3856ad364e35" />
          <publisherPolicy apply="no" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Microsoft.PowerShell.Commands.Management" publicKeyToken="31bf3856ad364e35" />
          <publisherPolicy apply="no" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Microsoft.PowerShell.Security" publicKeyToken="31bf3856ad364e35" />
          <publisherPolicy apply="no" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Microsoft.PowerShell.Commands.Diagnostics" publicKeyToken="31bf3856ad364e35" />
          <publisherPolicy apply="no" />
        </dependentAssembly>
        </assemblyBinding>
    </runtime>
Run Code Online (Sandbox Code Playgroud)

  • 在VS2015中我失败了,这个建议对我有所帮助.只需打开`c:\ Users \%username%\ AppData\Local\Microsoft\VisualStudio\14.0\devenv.exe.config`并添加相同的行. (12认同)
  • 使用Windows 8.1上的Visual Studio 2013,将这些行添加到`%APPDATA%\ ..\Local\Microsoft\VisualStudio\12.0\devenv.exe.config`的`<runtime>`部分可以解决问题. (7认同)
  • 不幸的是,Visual Studio不时会重新生成此文件并且更改丢失,因此您必须将其重新放回. (3认同)

小智 7

我在VS 2015 Update 3中遇到了同样的问题.

我必须删除该文件夹:

%APPDATA%\Local\Microsoft\VisualStudio\
Run Code Online (Sandbox Code Playgroud)

然后加入:

c:\Users\%username%\AppData\Local\Microsoft\VisualStudio\14.??0\devenv.exe.config
Run Code Online (Sandbox Code Playgroud)

在部分:

<runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
Run Code Online (Sandbox Code Playgroud)

以下几行:

<dependentAssembly>
  <assemblyIdentity name="System.Management.Automation" publicKeyToken="31bf3856ad364e35" />
  <publisherPolicy apply="no" />
</dependentAssembly>

<dependentAssembly>
  <assemblyIdentity name="Microsoft.PowerShell.Commands.Utility" publicKeyToken="31bf3856ad364e35" />
  <publisherPolicy apply="no" />
</dependentAssembly>

<dependentAssembly>
  <assemblyIdentity name="Microsoft.PowerShell.ConsoleHost" publicKeyToken="31bf3856ad364e35" />
  <publisherPolicy apply="no" />
</dependentAssembly>

<dependentAssembly>
  <assemblyIdentity name="Microsoft.PowerShell.Commands.Management" publicKeyToken="31bf3856ad364e35" />
  <publisherPolicy apply="no" />
</dependentAssembly>

<dependentAssembly>
  <assemblyIdentity name="Microsoft.PowerShell.Security" publicKeyToken="31bf3856ad364e35" />
  <publisherPolicy apply="no" />
</dependentAssembly>

<dependentAssembly>
  <assemblyIdentity name="Microsoft.PowerShell.Commands.Diagnostics" publicKeyToken="31bf3856ad364e35" />
  <publisherPolicy apply="no" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

我失去了两天寻找解决方案......谢谢MS: - /