Thursday, December 19, 2019

Powershell script to Move folders/files from one location to another

Nice and handy Powershell script to Move folders/files from one location to another. This can be used as archival script. It will be good to add Compression and zip functionality.


Add-Type -assembly 'system.io.compression.filesystem'

$source = "\\sourceServer\Folders\*"
$dest = "\\destinationServer\Archive"

if (Test-Path -path $dest) {
  Write-Host "Move Item"
  Move-Item -Path $source -Destination $dest -force
}

  Write-Host "Completed"

Saturday, November 23, 2019

Logging Exceptions to Fileshare C# .NET

Logging exceptions to Fileshare using C#.NET. Sample code.

try
{
 string filePath = @"\\sharepointFix\Error.txt";
 .............
.............
}
catch
{
                using (StreamWriter writer = new StreamWriter(filePath, true))
              {
                    writer.WriteLine("Message: " + exp.Message + Environment.NewLine + "Stack Trace : " + exp.StackTrace
                        + Environment.NewLine + "Inner Exception: " + exp.InnerException
                        + Environment.NewLine + "Date :" + DateTime.Now.ToString());
                }
}

How to load an Assembly (DLL) programatically

Sample code to load Assembly. (DLL) programatically

Assembly assembly = Assembly.LoadFile(Properties.Settings.Default.AssemblyPath);

            Type myClassType =
            (from type in assembly.GetExportedTypes()
             where typeof(InterfaceBulkLoad).IsAssignableFrom(type)
             select type).Single();

            var instance = (InterfaceBulkLoad)Activator.CreateInstance(myClassType);
            if (args != null)
            {
                var isLoaded = instance.LoadFiles(new LoadFileRequest
                {
                    Environment = Properties.Settings.Default.Environment,
                    ProcessName = args[0],
                    ConnectionString = Properties.Settings.Default.ConnectionString
                });

                Console.WriteLine(isLoaded);
            }
            else
            {
                Console.WriteLine("Arguments were not supplied");
            }