1

I want to add content to a web config, but only if that content does not already exist. This is what I do:

$webconfig = Get-Content F:\whatever\web.config
if ($webconfig -notlike "*WebGrease*" ) {
  // do stuff here
}

Now my problem is: Even if the web.config contains something like

<dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>

it still enters the code at "do stuff here". What is the problem? Doesn't "notlike" work with multi-line - strings?

Ole Albers
  • 8,715
  • 10
  • 73
  • 166

1 Answers1

2

You can easily find the string using Select-String (which accepts regex for -Pattern):

if(-not(Select-String -Pattern "WebGrease" -Path "F:\whatever\web.config")) {
    // do stuff here
}

Since you are using an XML file, as suggested by @Mathias R. Jessen you could directly target the node with Select-Xml instead:

if(-not(Select-Xml -Xml "F:\whatever\web.config" -XPath '//assemblyIdentity[@name = "WebGrease"]')) {
    // do stuff here
}
Community
  • 1
  • 1
sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • If OP is always looking for a specific configuration element, `Select-Xml -Xml $wc -XPath '//assemblyIdentity[@name = "WebGrease"]'` might be a safer option – Mathias R. Jessen Dec 08 '15 at 09:20