Skip to content

Instantly share code, notes, and snippets.

@lennybacon
Created April 29, 2015 11:13
Show Gist options
  • Save lennybacon/dd6b258674f65c817a95 to your computer and use it in GitHub Desktop.
Save lennybacon/dd6b258674f65c817a95 to your computer and use it in GitHub Desktop.
Enable or disable FxCop Code Analysis solution wide in Visual Studio Package Manager Console
function Disable-CodeAnalysis(){
ForEach ($project in $dte.Solution.Projects) {
Set-CodeAnalysis($project, $false)
}
}
function Enable-CodeAnalysis(){
ForEach ($project in $dte.Solution.Projects) {
Set-CodeAnalysis($project, $true)
}
}
function Set-CodeAnalysis($project, $value){
$projectName = $project.Name
$projectFilePath = $project.FullName
if ([System.String]::IsNullOrWhiteSpace($projectFilePath)){
if($proj.Kind -eq "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}"){
ForEach ($item in $proj.ProjectItems) {
if($item.SubProject -ne $null){
Set-CodeAnalysis($item.SubProject, $value)
}
}
}
continue;
}
$xmlDocument = new-object System.Xml.XmlDocument
$action = "Enable"
if($value -ne $true){
$action = "Disable"
}
Write-Host "$action code analysis for $projectName at $projectFilePath"
$xmlDocument.Load([string]$projectFilePath);
$namespaceManager = new-object System.Xml.XmlNamespaceManager($xmlDocument.NameTable);
$namespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003");
$nodes = $xmlDocument.SelectNodes("//ns:RunCodeAnalysis", $namespaceManager);
if ($nodes -eq $null){
continue;
}
foreach ($node in $nodes){
$node.InnerText = "$value";
}
$xmlDocument.Save($projectFilePath);
}
@moxplod
Copy link

moxplod commented Apr 30, 2016

This works partially, enable does not work - always disables.
Also - does not get projects withing solution folders.

Thanks for the starting point to getting this done though. It is a pain to have CA running all the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment