xxxxプログラマのメモ

先人に感謝と敬意:自分の困ったこと調べたことのメモ

Powershell上でC#コードの実行

C#からPowerShellコマンドの実行 - Qiita
yomon.hatenablog.com

$assemblies = (
 "System",
 "System.Xml",
 "System.Linq",
 "System.Xml.Linq"
)

$source = @"
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

public static class UpdateConfig
    {
        public static void UpdateLauncher(string path)
        {
            XDocument xdoc = XDocument.Load(path);
            XElement _target = xdoc.Descendants("add").Single(item => (item.Attribute("key") != null ? item.Attribute("key").Value.ToUpper() : null) == "AGENT");
            _target.SetAttributeValue("value", @"C:\hoge.exe");

            var setting = new XmlWriterSettings()
            {
                NewLineChars = Environment.NewLine,
                Indent = true,
                NewLineOnAttributes = false,
                OmitXmlDeclaration = false
            };

            using (var writer = XmlWriter.Create(path, setting))
            {
                xdoc.Save(writer);
            }

        }
    }
"@

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -Language CSharp
[UpdateConfig]::UpdateLauncher($word)