I'm using PowerShell to call C# code to turn list alerts on or off on a specific list. When I try to use [AlertsOnOff1.onOff]::Main("off") to call the Main method and pass the arugment of "off"the script runs like everything went fine with zero errors, but when I create an item on the list I still get Alerts. How can I get the C# code to recognize the argument passed by PowerShell?

Add-PSSnapin Microsoft.SharePoint.PowerShell

$path = "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI"

$Assem = @(

$Source = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace AlertsOnOff1
    public class onOff
        public static void Main(string[] args)
            args = new string[1];
            string site = "http://intranet.contoso.com/";
            string web = string.Empty;
            string list = "ListA";
            SPSite tmpSite=null;


                tmpSite = new SPSite(site);
                SPWeb tmpWeb = tmpSite.OpenWeb(web);
                SPList tmpList = tmpWeb.Lists[list];

                foreach (SPAlert tmpAlert in tmpWeb.Alerts)

                    if (args[0] == "on") //Turn it ON
                        if ((tmpAlert.List.Title == tmpList.Title) & (tmpAlert.Status == SPAlertStatus.Off))
                            tmpAlert.Status = SPAlertStatus.On;

                    if (args[0] == "off") //Turn it OFF
                        if ((tmpAlert.List.Title == tmpList.Title) & (tmpAlert.Status == SPAlertStatus.On))
                            tmpAlert.Status = SPAlertStatus.Off;
            catch (Exception ex)
                Console.WriteLine("Error: " + ex.Message);
                System.Diagnostics.EventLog.WriteEntry("Disable/Enable Alerts", ex.Message);
                //Dispose of the Root Site Object

Add-Type -TypeDefinition $Source -ReferencedAssemblies $Assem

Write-Host "Turning alerts off"
Write-Host "Alerts turned off"

#powershell code


Originally when I didn't have args = new string[1]; I would get an error when i passed an argument through the C# code. I have now removed that line of code and passing the argument works.


Your method expects an array of strings, but you are passing in a single string.

Also, the first line of your method overwrites the value that was passed in (perhaps you added this because you were getting an error?)

I would suggest changing the method signature to

public static void Main(string args)

and also removing the following line of code:

args = new string[1];
  • When I change to public static void Main(string args) I get an error stating that 'AlertsOnOff1.onOff.Main(string)' has the wrong signature to be an entry point – zingwing Sep 8 '14 at 16:19
  • I see what mean by `args = string[0]; overwriting the argument. I ended up removing the line and now my code is working. Although before I had the line of code, my code was throwing an error. – zingwing Sep 8 '14 at 16:52
  • Okay - sounds like a requirement that the parameter be an array - good to hear you got it sorted anyway. – Hutch Sep 8 '14 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.