0

I use the following PowerShell code taking command line arguments for creating site collections and it works fine.

However when I convert it into an exe and execute it?

param(
[string]$siteUrl,[string]$siteOwner,[string]$template,[string]$siteTitle,[int]$siteCount
)
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

$ErrorActionPreference = "Stop"
try
{

    Start-SPAssignment -Global

        if ($siteCount -eq 1)
        {
            New-SPSite ($siteUrl + "1") -OwnerAlias $siteOwner -Name $siteTitle -Template $template
        }
        else
            {
                for ($i=1; $i -le $siteCount; $i++)
                    {
                        New-SPSite ($siteUrl + $i) -OwnerAlias $siteOwner -Name ($siteTitle + "-" + $i) -Template $template
                    }
            }



    Stop-SPAssignment -Global
    Exit 0
}
catch 
{
    $ErrorMessage = $_.Exception.Message
    Write-Output $ErrorMessage
    Exit 1
}
0

You can use below snippet to create site collection

SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://WebApp"));
webApp.Sites.Add("/sites/site1", "Description", null, 1033, null, "administrator", "Administrator", "admin@contoso.com"))

You can read command line arguments using

if (args == null)
{
    Console.WriteLine("args is null"); // Check for null array
}
else
{
    Console.Write("args length is ");
    Console.WriteLine(args.Length); // Write array length
    for (int i = 0; i < args.Length; i++) // Loop through array
    {
        Console.Write(argument); // Write string

        //USE THIS argument for creating site collection
    }
}

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.