Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - TRYING to use XML Config files

Reply
 
Old 06-01-2006   #1 (permalink)
Singee


 
 

TRYING to use XML Config files

I am trying to use xml configuration files for my scripts so i don't have to
write a parser for some custom configuration file that i would write.

SO. I need to use system.configuration namespace to do this and i need
access to the System.Configuration.Configuration object but this doesn't work:
(New-Object System.Configuration.Configuration).AppSettings["testKey"]

It says New-Object : Constructor not found. Cannot find an appropriate
constructor for type System.Configuration.Configuration

any ideas? i really would like to use these xml config files?

Sidenote: what should I name config files? because there is a standard for
applications that you do appname.exe.config but this isn't really an app and
isn't an exe so i need some way for windows to recognise which config file im
talking about.

thanks


My System SpecsSystem Spec
Old 06-01-2006   #2 (permalink)
Keith Hill [MVP]


 
 

Re: TRYING to use XML Config files

"Singee" <Singee@discussions.microsoft.com> wrote in message news:52A17FFF-A765-43A6-82B2-2C6B943B808B@microsoft.com...
>I am trying to use xml configuration files for my scripts so i don't have to
> write a parser for some custom configuration file that i would write.
>
> SO. I need to use system.configuration namespace to do this and i need
> access to the System.Configuration.Configuration object but this doesn't work:
> (New-Object System.Configuration.Configuration).AppSettings["testKey"]
>


XML support in PowerShell is pretty good so I think you could skip using System.Configuration. This is somewhat problematic anyway because it wants to work with the config associated with the EXE i.e. PowerShell.exe.config. That just feels wrong to me.

Anyway doing this with straight PoSH is pretty simple. Take this config info and put in a file called something like MyScript.config:

<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727" safemode="true"/>
<requiredRuntime version="v2.0.50727" safemode="true"/>
</startup>
<appSettings>
<add key="Editions" value="Standard Edition,Desktop Edition,Desktop Engine,Evaluation Edition,Enterprise Edition,Developer Edition,Personal Edition,Enterprise Edition (64-bit),Developer Edition (64-bit),Enterprise Evaluation Edition,Small Business Server Edition" />
<add key="Categories" value="Backup and Recovery,Configuration Options,Database Design,Database Administration,Deprecation,Full-Text,General Administration,Generic,T-SQL,SQL Server 2005 Readiness" />
<add key="DataTypes" value="string,int,double" />
<add key="Database" value="" />
<add key="SQLServer" value="" />
<add key="WindowsAuthentication" value="" />
<add key="MaxScanDetailRows" value="500" />
<!-- Logging level. 0 - Off, 1 - Error, 2 - Warning, 3 - Info, >=4 - Verbose -->
<add key="LoggingLevel" value="2" />
</appSettings>
</configuration>

Save the following to a file called LoadConfig.ps1 script:

param($path = $(throw "You must specify a config file"))

$global:appSettings = @{}
$config = [xml](get-content $path)
foreach ($addNode in $config.configuration.appsettings.add) {
if ($addNode.Value.Contains(',')) {
# Array case
$value = $addNode.Value.Split(',')
}
else {
# Scalar case
$value = $addNode.Value
}
$global:appSettings[$addNode.Key] = $value
}

Then from your script execute:

..\LoadConfig.ps1 MyScript.config

Now you can reference settings pretty easily:

$appSettings["MaxScanDetailRows"]
500

$appSettings["datatypes"]
string
int
double

$appSettings["datatypes"][0]
string

It's not anything fancy but it works. If there was a way to automatically execute something at the beginning of every script then you could do some $MyInvocation magic to grab the script name, append ..config, test for the file's existance and load it into the $appSettings hashtable automatically for all scripts. AFAIK this "autorun" capability doesn't exist in V1 of PowerShell. It could be a bit of a security risk though.

--
Keith
My System SpecsSystem Spec
Old 06-02-2006   #3 (permalink)
Martin Ehrlich


 
 

Re: TRYING to use XML Config files

Good Evening. ... well at least where I am.

I'm not familiar with the powershell syntax for that, but I'm quite familiar
with managed code in C# concerning the configuration system. In a simple
application (e.g. console-application). I have to do the following steps to
access a specific configuration section.

using System;
using System.Configuration;

.....

Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// for Powershell better explicitly opening a configuration by path
Configuration config =
ConfigurationManager.OpenExeConfiguration(@"C:\Programme\MyProgram\Program.exe.config");

// Working with config, e.g.
AppSettingsSection appSettings =
(AppSettingsSection)config.Sections["appSettings"];
Console.WriteLine(appSettings.Settings["Value"]);

// or shorter (in this case)
Console.WriteLine(config.AppSettings.Settings["Value"]);

// Write-Access is also possible
config.AppSettings.Settings["Value"] = "MyValue";
config.Save();

I'm sure this can be easily translated in powershell-syntax.

Based on that code, it is possible to access almost every section of a
configuration file. Just look for classes that derives from
System.Configuration.ConfigurationSection:

System.Configuration.AppSettingsSection
System.Configuration.ClientSettingsSection
System.Configuration.ConnectionStringsSection
System.Configuration.DefaultSection
System.Configuration.IgnoreSection
System.Configuration.ProtectedConfigurationSection
System.Net.Configuration.AuthenticationModulesSection
System.Net.Configuration.ConnectionManagementSection
System.Net.Configuration.DefaultProxySection
System.Net.Configuration.RequestCachingSection
System.Net.Configuration.SettingsSection
System.Net.Configuration.SmtpSection
System.Net.Configuration.WebRequestModulesSection
System.Transactions.Configuration.DefaultSettingsSection
System.Transactions.Configuration.MachineSettingsSection
System.Web.Configuration.AnonymousIdentificationSection
System.Web.Configuration.AuthenticationSection
System.Web.Configuration.AuthorizationSection
System.Web.Configuration.CacheSection
System.Web.Configuration.ClientTargetSection
System.Web.Configuration.CompilationSection
System.Web.Configuration.CustomErrorsSection
System.Web.Configuration.DeploymentSection
System.Web.Configuration.GlobalizationSection
System.Web.Configuration.HealthMonitoringSection
System.Web.Configuration.HostingEnvironmentSection
System.Web.Configuration.HttpCookiesSection
System.Web.Configuration.HttpHandlersSection
System.Web.Configuration.HttpModulesSection
System.Web.Configuration.HttpRuntimeSection
System.Web.Configuration.IdentitySection
System.Web.Configuration.MachineKeySection
System.Web.Configuration.MembershipSection
System.Web.Configuration.OutputCacheSection
System.Web.Configuration.OutputCacheSettingsSection
System.Web.Configuration.PagesSection
System.Web.Configuration.ProcessModelSection
System.Web.Configuration.ProfileSection
System.Web.Configuration.RoleManagerSection
System.Web.Configuration.SecurityPolicySection
System.Web.Configuration.SessionPageStateSection
System.Web.Configuration.SessionStateSection
System.Web.Configuration.SiteMapSection
System.Web.Configuration.SqlCacheDependencySection
System.Web.Configuration.TraceSection
System.Web.Configuration.TrustSection
System.Web.Configuration.UrlMappingsSection
System.Web.Configuration.WebControlsSection
System.Web.Configuration.WebPartsSection
System.Web.Configuration.XhtmlConformanceSection
System.Web.Mobile.DeviceFiltersSection
System.Web.Services.Configuration.WebServicesSection
System.Web.UI.MobileControls.MobileControlsSection
System.Windows.Forms.WindowsFormsSection
System.Xml.Serialization.Configuration.DateTimeSerializationSection
System.Xml.Serialization.Configuration.SchemaImporterExtensionsSection
System.Xml.Serialization.Configuration.XmlSerializerSection

And that's not all. You can implement your own configurationSections and
sub-elements very easily by deriving and applying some attributes. You get
read/write access, configuration inheritance (e.g. machine.config to
app.config) you can encrypt sections, move parts into external files ... and
everything without writing a single "<tag>" or using any of the
xml(de)serializer-classes. Well not directly. ... But that's more a
developer topic.

-Martin Ehrlich



"Singee" <Singee@discussions.microsoft.com> schrieb im Newsbeitrag
news:52A17FFF-A765-43A6-82B2-2C6B943B808B@microsoft.com...
>I am trying to use xml configuration files for my scripts so i don't have
>to
> write a parser for some custom configuration file that i would write.
>
> SO. I need to use system.configuration namespace to do this and i need
> access to the System.Configuration.Configuration object but this doesn't
> work:
> (New-Object System.Configuration.Configuration).AppSettings["testKey"]
>
> It says New-Object : Constructor not found. Cannot find an appropriate
> constructor for type System.Configuration.Configuration
>
> any ideas? i really would like to use these xml config files?
>
> Sidenote: what should I name config files? because there is a standard for
> applications that you do appname.exe.config but this isn't really an app
> and
> isn't an exe so i need some way for windows to recognise which config file
> im
> talking about.
>
> thanks
>



My System SpecsSystem Spec
Old 06-02-2006   #4 (permalink)
/\\/\\o\\/\\/


 
 

Re: TRYING to use XML Config files

to get you going in powershell you use the static methods of the type
like this
MowPS>([System.Configuration.ConfigurationManager]::OpenExeConfiguration("$pshome\powershell.application.config")).AppSettings


Settings : {}
File :
SectionInformation : System.Configuration.SectionInformation
LockAttributes : {}
LockAllAttributesExcept : {}
LockElements : {}
LockAllElementsExcept : {}
LockItem : False
ElementInformation : System.Configuration.ElementInformation



MowPS>([System.Configuration.ConfigurationManager]::OpenExeConfiguration("$pshome\powershell.application.config")).AppSettings
| gm


TypeName: System.Configuration.AppSettingsSection

Name MemberType Definition
---- ---------- ----------


gr /\/\o\/\/

Martin Ehrlich wrote:
> Good Evening. ... well at least where I am.
>
> I'm not familiar with the powershell syntax for that, but I'm quite familiar
> with managed code in C# concerning the configuration system. In a simple
> application (e.g. console-application). I have to do the following steps to
> access a specific configuration section.
>
> using System;
> using System.Configuration;
>
> ....
>
> Configuration config =
> ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
>
> // for Powershell better explicitly opening a configuration by path
> Configuration config =
> ConfigurationManager.OpenExeConfiguration(@"C:\Programme\MyProgram\Program.exe.config");
>
> // Working with config, e.g.
> AppSettingsSection appSettings =
> (AppSettingsSection)config.Sections["appSettings"];
> Console.WriteLine(appSettings.Settings["Value"]);
>
> // or shorter (in this case)
> Console.WriteLine(config.AppSettings.Settings["Value"]);
>
> // Write-Access is also possible
> config.AppSettings.Settings["Value"] = "MyValue";
> config.Save();
>
> I'm sure this can be easily translated in powershell-syntax.
>
> Based on that code, it is possible to access almost every section of a
> configuration file. Just look for classes that derives from
> System.Configuration.ConfigurationSection:
>
> System.Configuration.AppSettingsSection
> System.Configuration.ClientSettingsSection
> System.Configuration.ConnectionStringsSection
> System.Configuration.DefaultSection
> System.Configuration.IgnoreSection
> System.Configuration.ProtectedConfigurationSection
> System.Net.Configuration.AuthenticationModulesSection
> System.Net.Configuration.ConnectionManagementSection
> System.Net.Configuration.DefaultProxySection
> System.Net.Configuration.RequestCachingSection
> System.Net.Configuration.SettingsSection
> System.Net.Configuration.SmtpSection
> System.Net.Configuration.WebRequestModulesSection
> System.Transactions.Configuration.DefaultSettingsSection
> System.Transactions.Configuration.MachineSettingsSection
> System.Web.Configuration.AnonymousIdentificationSection
> System.Web.Configuration.AuthenticationSection
> System.Web.Configuration.AuthorizationSection
> System.Web.Configuration.CacheSection
> System.Web.Configuration.ClientTargetSection
> System.Web.Configuration.CompilationSection
> System.Web.Configuration.CustomErrorsSection
> System.Web.Configuration.DeploymentSection
> System.Web.Configuration.GlobalizationSection
> System.Web.Configuration.HealthMonitoringSection
> System.Web.Configuration.HostingEnvironmentSection
> System.Web.Configuration.HttpCookiesSection
> System.Web.Configuration.HttpHandlersSection
> System.Web.Configuration.HttpModulesSection
> System.Web.Configuration.HttpRuntimeSection
> System.Web.Configuration.IdentitySection
> System.Web.Configuration.MachineKeySection
> System.Web.Configuration.MembershipSection
> System.Web.Configuration.OutputCacheSection
> System.Web.Configuration.OutputCacheSettingsSection
> System.Web.Configuration.PagesSection
> System.Web.Configuration.ProcessModelSection
> System.Web.Configuration.ProfileSection
> System.Web.Configuration.RoleManagerSection
> System.Web.Configuration.SecurityPolicySection
> System.Web.Configuration.SessionPageStateSection
> System.Web.Configuration.SessionStateSection
> System.Web.Configuration.SiteMapSection
> System.Web.Configuration.SqlCacheDependencySection
> System.Web.Configuration.TraceSection
> System.Web.Configuration.TrustSection
> System.Web.Configuration.UrlMappingsSection
> System.Web.Configuration.WebControlsSection
> System.Web.Configuration.WebPartsSection
> System.Web.Configuration.XhtmlConformanceSection
> System.Web.Mobile.DeviceFiltersSection
> System.Web.Services.Configuration.WebServicesSection
> System.Web.UI.MobileControls.MobileControlsSection
> System.Windows.Forms.WindowsFormsSection
> System.Xml.Serialization.Configuration.DateTimeSerializationSection
> System.Xml.Serialization.Configuration.SchemaImporterExtensionsSection
> System.Xml.Serialization.Configuration.XmlSerializerSection
>
> And that's not all. You can implement your own configurationSections and
> sub-elements very easily by deriving and applying some attributes. You get
> read/write access, configuration inheritance (e.g. machine.config to
> app.config) you can encrypt sections, move parts into external files ... and
> everything without writing a single "<tag>" or using any of the
> xml(de)serializer-classes. Well not directly. ... But that's more a
> developer topic.
>
> -Martin Ehrlich
>
>
>
> "Singee" <Singee@discussions.microsoft.com> schrieb im Newsbeitrag
> news:52A17FFF-A765-43A6-82B2-2C6B943B808B@microsoft.com...
>> I am trying to use xml configuration files for my scripts so i don't have
>> to
>> write a parser for some custom configuration file that i would write.
>>
>> SO. I need to use system.configuration namespace to do this and i need
>> access to the System.Configuration.Configuration object but this doesn't
>> work:
>> (New-Object System.Configuration.Configuration).AppSettings["testKey"]
>>
>> It says New-Object : Constructor not found. Cannot find an appropriate
>> constructor for type System.Configuration.Configuration
>>
>> any ideas? i really would like to use these xml config files?
>>
>> Sidenote: what should I name config files? because there is a standard for
>> applications that you do appname.exe.config but this isn't really an app
>> and
>> isn't an exe so i need some way for windows to recognise which config file
>> im
>> talking about.
>>
>> thanks
>>

>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Automatic merge of config files .NET General
Can't Config Permission To Xfr Files On LAN Vista file management
RE: search folders for .config files .NET General
Config files Vista performance & maintenance
config files PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46