如何为PowerShell指定非默认的powershell.exe.config配置文件?
问题描述
需要启动PowerShell时指定非默认目录的powershell.exe.config配置文件,避免覆盖默认配置。自定义配置文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> <section name="BestMatchServiceUrlsConfig" type="Sdl.BestMatchServiceStudioIntegration.Common.UrlsConfig, Sdl.BestMatchServiceStudioIntegration.Common" /> <section name="BestMatchServiceSettings" type="Sdl.BestMatchService.Common.Configuration.BestMatchServiceSettingsSection, Sdl.BestMatchService.Common" /> <section name="AppStoreConfiguration" type="Sdl.AppStoreIntegration.AppStoreConfiguration, Sdl.AppStoreIntegrationPlugin" /> <section name="CommunityNotificationsConfiguration" type="Sdl.TranslationStudio.Plugins.CommunityInside.Notifications.Configuration.CommunityNotificationsConfiguration, Sdl.TranslationStudio.Plugins.CommunityInside" /> <section name="LanguageCloudSyncConfig" type="Sdl.TranslationStudio.LanguageCloud.Config.SyncConfigurationSection, Sdl.TranslationStudio.LanguageCloud" /> </configSections> <runtime> <NetFx40_PInvokeStackResilience enabled="1" /> <legacyCorruptedStateExceptionsPolicy enabled="true" /> <ThrowUnobservedTaskException enabled="true" /> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="icu.net" publicKeyToken="416fdd914afa6b66" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.7.0.0" newVersion="2.7.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.AspNetCore.Http.Connections.Common" publicKeyToken="adb9793829ddae60" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.2.0" newVersion="6.0.2.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.11.0" newVersion="4.0.11.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Sdl.ApiClientSdk.Core" publicKeyToken="c28cdb26c445c888" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.4.3.0" newVersion="1.4.3.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
已尝试的方法
- 命令行启动指定配置:
执行以下命令后PowerShell卡住:START %SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe $ConfigFilePath - 启动后修改配置路径:
运行以下脚本能成功设置配置文件路径,但PowerShell并未实际加载该配置,仍使用默认的powershell.exe.config,疑似缓存未清除:[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $ConfigFilePath) [Console]::WriteLine([System.AppDomain]::CurrentDomain.GetData("APP_CONFIG_FILE")) Add-Type -AssemblyName System.Configuration [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0) [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null) ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null) [Configuration.ConfigurationManager]::ConnectionStrings[0].Name
求助
有没有可行方案,能在启动PowerShell时直接指定自定义配置文件,或者启动后让PowerShell生效该配置?
内容的提问来源于stack exchange,提问作者Prinzphilipp2000
相关产品推荐
相关产品推荐

