PoshCode Logo PowerShell Code Repository

sqlps2 by Chad Miller 3 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1885"></script>download | new post

A C# source file created by running make-shell to create a SQL Server minishell

  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Resources;
  7. using System.Globalization;
  8. using System.Management.Automation;
  9. using System.Management.Automation.Runspaces;
  10. using System.Reflection;
  11.  
  12. [assembly:RunspaceConfigurationType("Community.CustomRunspaceConfiguration")]
  13.  
  14. namespace Community
  15. {
  16.     /// <summary>
  17.     /// CustomRunspaceConfiguration implements runspace configuration for current
  18.     /// minishell.
  19.     /// </summary>
  20.     public class CustomRunspaceConfiguration : RunspaceConfiguration
  21.     {
  22.         /// <summary>
  23.         /// Constructor is made private intentionally so that each instance of
  24.         /// CustomRunspaceConfiguration will be created through Create() function
  25.         /// </summary>
  26.         private CustomRunspaceConfiguration()
  27.         {
  28.         }
  29.  
  30.         /// <summary>
  31.         /// This is the static function to create an instance of this type.
  32.         /// </summary>
  33.         /// <returns>runspace configuration instance created</returns>
  34.         public new static RunspaceConfiguration Create()
  35.         {
  36.             CustomRunspaceConfiguration runspaceConfig = new CustomRunspaceConfiguration();
  37.  
  38.             return runspaceConfig;
  39.         }
  40.  
  41.         private const string _shellId = "Community.sqlps2";
  42.  
  43.         /// <summary>
  44.         /// Shell Id
  45.         /// </summary>
  46.         /// <value>Shell Id</value>
  47.         public override string ShellId
  48.         {
  49.             get
  50.             {
  51.                 return _shellId;
  52.             }
  53.         }
  54.  
  55.         #region Cmdlets, Providers, Assemblies
  56.        
  57.         private CmdletConfigurationEntry[] _cmdletConfigurationEntries = new CmdletConfigurationEntry[] { new CmdletConfigurationEntry("Invoke-Sqlcmd", typeof(Microsoft.SqlServer.Management.PowerShell.GetScriptCommand), "Microsoft.SqlServer.Management.PSSnapins.dll-Help.xml"),
  58.             new CmdletConfigurationEntry("Invoke-PolicyEvaluation", typeof(Microsoft.SqlServer.Management.PowerShell.InvokePolicyEvaluationCommand), "Microsoft.SqlServer.Management.PSSnapins.dll-Help.xml"),
  59.             new CmdletConfigurationEntry("Encode-SqlName", typeof(Microsoft.SqlServer.Management.PowerShell.EncodeSqlName), "Microsoft.SqlServer.Management.PSProvider.dll-Help.xml"),
  60.             new CmdletConfigurationEntry("Decode-SqlName", typeof(Microsoft.SqlServer.Management.PowerShell.DecodeSqlName), "Microsoft.SqlServer.Management.PSProvider.dll-Help.xml"),
  61.             new CmdletConfigurationEntry("Convert-UrnToPath", typeof(Microsoft.SqlServer.Management.PowerShell.ConvertUrnToPath), "Microsoft.SqlServer.Management.PSProvider.dll-Help.xml"),
  62.             new CmdletConfigurationEntry("Start-Transcript", typeof(Microsoft.PowerShell.Commands.StartTranscriptCommand), "Microsoft.PowerShell.ConsoleHost.dll-Help.xml"),
  63.             new CmdletConfigurationEntry("Stop-Transcript", typeof(Microsoft.PowerShell.Commands.StopTranscriptCommand), "Microsoft.PowerShell.ConsoleHost.dll-Help.xml"),
  64.             new CmdletConfigurationEntry("Get-Command", typeof(Microsoft.PowerShell.Commands.GetCommandCommand), "System.Management.Automation.dll-Help.xml"),
  65.             new CmdletConfigurationEntry("Get-Help", typeof(Microsoft.PowerShell.Commands.GetHelpCommand), "System.Management.Automation.dll-Help.xml"),
  66.             new CmdletConfigurationEntry("Get-History", typeof(Microsoft.PowerShell.Commands.GetHistoryCommand), "System.Management.Automation.dll-Help.xml"),
  67.             new CmdletConfigurationEntry("Invoke-History", typeof(Microsoft.PowerShell.Commands.InvokeHistoryCommand), "System.Management.Automation.dll-Help.xml"),
  68.             new CmdletConfigurationEntry("Add-History", typeof(Microsoft.PowerShell.Commands.AddHistoryCommand), "System.Management.Automation.dll-Help.xml"),
  69.             new CmdletConfigurationEntry("ForEach-Object", typeof(Microsoft.PowerShell.Commands.ForEachObjectCommand), "System.Management.Automation.dll-Help.xml"),
  70.             new CmdletConfigurationEntry("Where-Object", typeof(Microsoft.PowerShell.Commands.WhereObjectCommand), "System.Management.Automation.dll-Help.xml"),
  71.             new CmdletConfigurationEntry("Set-PSDebug", typeof(Microsoft.PowerShell.Commands.SetPSDebugCommand), "System.Management.Automation.dll-Help.xml"),
  72.             new CmdletConfigurationEntry("Add-Content", typeof(Microsoft.PowerShell.Commands.AddContentCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  73.             new CmdletConfigurationEntry("Clear-Content", typeof(Microsoft.PowerShell.Commands.ClearContentCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  74.             new CmdletConfigurationEntry("Clear-ItemProperty", typeof(Microsoft.PowerShell.Commands.ClearItemPropertyCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  75.             new CmdletConfigurationEntry("Join-Path", typeof(Microsoft.PowerShell.Commands.JoinPathCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  76.             new CmdletConfigurationEntry("Convert-Path", typeof(Microsoft.PowerShell.Commands.ConvertPathCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  77.             new CmdletConfigurationEntry("Copy-ItemProperty", typeof(Microsoft.PowerShell.Commands.CopyItemPropertyCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  78.             new CmdletConfigurationEntry("Get-EventLog", typeof(Microsoft.PowerShell.Commands.GetEventLogCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  79.             new CmdletConfigurationEntry("Get-ChildItem", typeof(Microsoft.PowerShell.Commands.GetChildItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  80.             new CmdletConfigurationEntry("Get-Content", typeof(Microsoft.PowerShell.Commands.GetContentCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  81.             new CmdletConfigurationEntry("Get-ItemProperty", typeof(Microsoft.PowerShell.Commands.GetItemPropertyCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  82.             new CmdletConfigurationEntry("Get-WmiObject", typeof(Microsoft.PowerShell.Commands.GetWmiObjectCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  83.             new CmdletConfigurationEntry("Move-ItemProperty", typeof(Microsoft.PowerShell.Commands.MoveItemPropertyCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  84.             new CmdletConfigurationEntry("Get-Location", typeof(Microsoft.PowerShell.Commands.GetLocationCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  85.             new CmdletConfigurationEntry("Set-Location", typeof(Microsoft.PowerShell.Commands.SetLocationCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  86.             new CmdletConfigurationEntry("Push-Location", typeof(Microsoft.PowerShell.Commands.PushLocationCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  87.             new CmdletConfigurationEntry("Pop-Location", typeof(Microsoft.PowerShell.Commands.PopLocationCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  88.             new CmdletConfigurationEntry("New-PSDrive", typeof(Microsoft.PowerShell.Commands.NewPSDriveCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  89.             new CmdletConfigurationEntry("Remove-PSDrive", typeof(Microsoft.PowerShell.Commands.RemovePSDriveCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  90.             new CmdletConfigurationEntry("Get-PSDrive", typeof(Microsoft.PowerShell.Commands.GetPSDriveCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  91.             new CmdletConfigurationEntry("Get-Item", typeof(Microsoft.PowerShell.Commands.GetItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  92.             new CmdletConfigurationEntry("New-Item", typeof(Microsoft.PowerShell.Commands.NewItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  93.             new CmdletConfigurationEntry("Set-Item", typeof(Microsoft.PowerShell.Commands.SetItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  94.             new CmdletConfigurationEntry("Remove-Item", typeof(Microsoft.PowerShell.Commands.RemoveItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  95.             new CmdletConfigurationEntry("Move-Item", typeof(Microsoft.PowerShell.Commands.MoveItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  96.             new CmdletConfigurationEntry("Rename-Item", typeof(Microsoft.PowerShell.Commands.RenameItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  97.             new CmdletConfigurationEntry("Copy-Item", typeof(Microsoft.PowerShell.Commands.CopyItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  98.             new CmdletConfigurationEntry("Clear-Item", typeof(Microsoft.PowerShell.Commands.ClearItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  99.             new CmdletConfigurationEntry("Invoke-Item", typeof(Microsoft.PowerShell.Commands.InvokeItemCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  100.             new CmdletConfigurationEntry("Get-PSProvider", typeof(Microsoft.PowerShell.Commands.GetPSProviderCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  101.             new CmdletConfigurationEntry("New-ItemProperty", typeof(Microsoft.PowerShell.Commands.NewItemPropertyCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  102.             new CmdletConfigurationEntry("Split-Path", typeof(Microsoft.PowerShell.Commands.SplitPathCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  103.             new CmdletConfigurationEntry("Test-Path", typeof(Microsoft.PowerShell.Commands.TestPathCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  104.             new CmdletConfigurationEntry("Get-Process", typeof(Microsoft.PowerShell.Commands.GetProcessCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  105.             new CmdletConfigurationEntry("Stop-Process", typeof(Microsoft.PowerShell.Commands.StopProcessCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  106.             new CmdletConfigurationEntry("Remove-ItemProperty", typeof(Microsoft.PowerShell.Commands.RemoveItemPropertyCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  107.             new CmdletConfigurationEntry("Rename-ItemProperty", typeof(Microsoft.PowerShell.Commands.RenameItemPropertyCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  108.             new CmdletConfigurationEntry("Resolve-Path", typeof(Microsoft.PowerShell.Commands.ResolvePathCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  109.             new CmdletConfigurationEntry("Get-Service", typeof(Microsoft.PowerShell.Commands.GetServiceCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  110.             new CmdletConfigurationEntry("Stop-Service", typeof(Microsoft.PowerShell.Commands.StopServiceCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  111.             new CmdletConfigurationEntry("Start-Service", typeof(Microsoft.PowerShell.Commands.StartServiceCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  112.             new CmdletConfigurationEntry("Suspend-Service", typeof(Microsoft.PowerShell.Commands.SuspendServiceCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  113.             new CmdletConfigurationEntry("Resume-Service", typeof(Microsoft.PowerShell.Commands.ResumeServiceCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  114.             new CmdletConfigurationEntry("Restart-Service", typeof(Microsoft.PowerShell.Commands.RestartServiceCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  115.             new CmdletConfigurationEntry("Set-Service", typeof(Microsoft.PowerShell.Commands.SetServiceCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  116.             new CmdletConfigurationEntry("New-Service", typeof(Microsoft.PowerShell.Commands.NewServiceCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  117.             new CmdletConfigurationEntry("Set-Content", typeof(Microsoft.PowerShell.Commands.SetContentCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  118.             new CmdletConfigurationEntry("Set-ItemProperty", typeof(Microsoft.PowerShell.Commands.SetItemPropertyCommand), "Microsoft.PowerShell.Commands.Management.dll-Help.xml"),
  119.             new CmdletConfigurationEntry("Format-Default", typeof(Microsoft.PowerShell.Commands.FormatDefaultCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  120.             new CmdletConfigurationEntry("Format-List", typeof(Microsoft.PowerShell.Commands.FormatListCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  121.             new CmdletConfigurationEntry("Format-Custom", typeof(Microsoft.PowerShell.Commands.FormatCustomCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  122.             new CmdletConfigurationEntry("Format-Table", typeof(Microsoft.PowerShell.Commands.FormatTableCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  123.             new CmdletConfigurationEntry("Format-Wide", typeof(Microsoft.PowerShell.Commands.FormatWideCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  124.             new CmdletConfigurationEntry("Out-Null", typeof(Microsoft.PowerShell.Commands.OutNullCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  125.             new CmdletConfigurationEntry("Out-Default", typeof(Microsoft.PowerShell.Commands.OutDefaultCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  126.             new CmdletConfigurationEntry("Out-Host", typeof(Microsoft.PowerShell.Commands.OutHostCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  127.             new CmdletConfigurationEntry("Out-File", typeof(Microsoft.PowerShell.Commands.OutFileCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  128.             new CmdletConfigurationEntry("Out-Printer", typeof(Microsoft.PowerShell.Commands.OutPrinterCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  129.             new CmdletConfigurationEntry("Out-String", typeof(Microsoft.PowerShell.Commands.OutStringCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  130.             new CmdletConfigurationEntry("Out-LineOutput", typeof(Microsoft.PowerShell.Commands.OutLineOutputCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  131.             new CmdletConfigurationEntry("Add-Member", typeof(Microsoft.PowerShell.Commands.AddMemberCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  132.             new CmdletConfigurationEntry("Compare-Object", typeof(Microsoft.PowerShell.Commands.CompareObjectCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  133.             new CmdletConfigurationEntry("ConvertTo-Html", typeof(Microsoft.PowerShell.Commands.ConvertToHtmlCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  134.             new CmdletConfigurationEntry("Export-Csv", typeof(Microsoft.PowerShell.Commands.ExportCsvCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  135.             new CmdletConfigurationEntry("Import-Csv", typeof(Microsoft.PowerShell.Commands.ImportCsvCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  136.             new CmdletConfigurationEntry("Export-Alias", typeof(Microsoft.PowerShell.Commands.ExportAliasCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  137.             new CmdletConfigurationEntry("Invoke-Expression", typeof(Microsoft.PowerShell.Commands.InvokeExpressionCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  138.             new CmdletConfigurationEntry("Get-Alias", typeof(Microsoft.PowerShell.Commands.GetAliasCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  139.             new CmdletConfigurationEntry("Get-Culture", typeof(Microsoft.PowerShell.Commands.GetCultureCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  140.             new CmdletConfigurationEntry("Get-Date", typeof(Microsoft.PowerShell.Commands.GetDateCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  141.             new CmdletConfigurationEntry("Get-Host", typeof(Microsoft.PowerShell.Commands.GetHostCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  142.             new CmdletConfigurationEntry("Get-Member", typeof(Microsoft.PowerShell.Commands.GetMemberCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  143.             new CmdletConfigurationEntry("Get-UICulture", typeof(Microsoft.PowerShell.Commands.GetUICultureCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  144.             new CmdletConfigurationEntry("Get-Unique", typeof(Microsoft.PowerShell.Commands.GetUniqueCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  145.             new CmdletConfigurationEntry("Import-Alias", typeof(Microsoft.PowerShell.Commands.ImportAliasCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  146.             new CmdletConfigurationEntry("Select-String", typeof(Microsoft.PowerShell.Commands.SelectStringCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  147.             new CmdletConfigurationEntry("Measure-Object", typeof(Microsoft.PowerShell.Commands.MeasureObjectCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  148.             new CmdletConfigurationEntry("New-Alias", typeof(Microsoft.PowerShell.Commands.NewAliasCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  149.             new CmdletConfigurationEntry("New-TimeSpan", typeof(Microsoft.PowerShell.Commands.NewTimeSpanCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  150.             new CmdletConfigurationEntry("Read-Host", typeof(Microsoft.PowerShell.Commands.ReadHostCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  151.             new CmdletConfigurationEntry("Set-Alias", typeof(Microsoft.PowerShell.Commands.SetAliasCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  152.             new CmdletConfigurationEntry("Set-Date", typeof(Microsoft.PowerShell.Commands.SetDateCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  153.             new CmdletConfigurationEntry("Start-Sleep", typeof(Microsoft.PowerShell.Commands.StartSleepCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  154.             new CmdletConfigurationEntry("Tee-Object", typeof(Microsoft.PowerShell.Commands.TeeObjectCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  155.             new CmdletConfigurationEntry("Measure-Command", typeof(Microsoft.PowerShell.Commands.MeasureCommandCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  156.             new CmdletConfigurationEntry("Update-TypeData", typeof(Microsoft.PowerShell.Commands.UpdateTypeDataCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  157.             new CmdletConfigurationEntry("Update-FormatData", typeof(Microsoft.PowerShell.Commands.UpdateFormatDataCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  158.             new CmdletConfigurationEntry("Write-Host", typeof(Microsoft.PowerShell.Commands.WriteHostCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  159.             new CmdletConfigurationEntry("Write-Progress", typeof(Microsoft.PowerShell.Commands.WriteProgressCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  160.             new CmdletConfigurationEntry("New-Object", typeof(Microsoft.PowerShell.Commands.NewObjectCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  161.             new CmdletConfigurationEntry("Select-Object", typeof(Microsoft.PowerShell.Commands.SelectObjectCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  162.             new CmdletConfigurationEntry("Group-Object", typeof(Microsoft.PowerShell.Commands.GroupObjectCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  163.             new CmdletConfigurationEntry("Sort-Object", typeof(Microsoft.PowerShell.Commands.SortObjectCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  164.             new CmdletConfigurationEntry("Get-Variable", typeof(Microsoft.PowerShell.Commands.GetVariableCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  165.             new CmdletConfigurationEntry("New-Variable", typeof(Microsoft.PowerShell.Commands.NewVariableCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  166.             new CmdletConfigurationEntry("Set-Variable", typeof(Microsoft.PowerShell.Commands.SetVariableCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  167.             new CmdletConfigurationEntry("Remove-Variable", typeof(Microsoft.PowerShell.Commands.RemoveVariableCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  168.             new CmdletConfigurationEntry("Clear-Variable", typeof(Microsoft.PowerShell.Commands.ClearVariableCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  169.             new CmdletConfigurationEntry("Export-Clixml", typeof(Microsoft.PowerShell.Commands.ExportClixmlCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  170.             new CmdletConfigurationEntry("Import-Clixml", typeof(Microsoft.PowerShell.Commands.ImportClixmlCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  171.             new CmdletConfigurationEntry("Write-Debug", typeof(Microsoft.PowerShell.Commands.WriteDebugCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  172.             new CmdletConfigurationEntry("Write-Verbose", typeof(Microsoft.PowerShell.Commands.WriteVerboseCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  173.             new CmdletConfigurationEntry("Write-Warning", typeof(Microsoft.PowerShell.Commands.WriteWarningCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  174.             new CmdletConfigurationEntry("Write-Error", typeof(Microsoft.PowerShell.Commands.WriteErrorCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  175.             new CmdletConfigurationEntry("Write-Output", typeof(Microsoft.PowerShell.Commands.WriteOutputCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  176.             new CmdletConfigurationEntry("Get-TraceSource", typeof(Microsoft.PowerShell.Commands.GetTraceSourceCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  177.             new CmdletConfigurationEntry("Set-TraceSource", typeof(Microsoft.PowerShell.Commands.SetTraceSourceCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  178.             new CmdletConfigurationEntry("Trace-Command", typeof(Microsoft.PowerShell.Commands.TraceCommandCommand), "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"),
  179.             new CmdletConfigurationEntry("Get-Acl", typeof(Microsoft.PowerShell.Commands.GetAclCommand), "Microsoft.PowerShell.Security.dll-Help.xml"),
  180.             new CmdletConfigurationEntry("Set-Acl", typeof(Microsoft.PowerShell.Commands.SetAclCommand), "Microsoft.PowerShell.Security.dll-Help.xml"),
  181.             new CmdletConfigurationEntry("Get-PfxCertificate", typeof(Microsoft.PowerShell.Commands.GetPfxCertificateCommand), "Microsoft.PowerShell.Security.dll-Help.xml"),
  182.             new CmdletConfigurationEntry("Get-Credential", typeof(Microsoft.PowerShell.Commands.GetCredentialCommand), "Microsoft.PowerShell.Security.dll-Help.xml"),
  183.             new CmdletConfigurationEntry("Get-ExecutionPolicy", typeof(Microsoft.PowerShell.Commands.GetExecutionPolicyCommand), "Microsoft.PowerShell.Security.dll-Help.xml"),
  184.             new CmdletConfigurationEntry("Set-ExecutionPolicy", typeof(Microsoft.PowerShell.Commands.SetExecutionPolicyCommand), "Microsoft.PowerShell.Security.dll-Help.xml"),
  185.             new CmdletConfigurationEntry("Get-AuthenticodeSignature", typeof(Microsoft.PowerShell.Commands.GetAuthenticodeSignatureCommand), "Microsoft.PowerShell.Security.dll-Help.xml"),
  186.             new CmdletConfigurationEntry("Set-AuthenticodeSignature", typeof(Microsoft.PowerShell.Commands.SetAuthenticodeSignatureCommand), "Microsoft.PowerShell.Security.dll-Help.xml"),
  187.             new CmdletConfigurationEntry("ConvertFrom-SecureString", typeof(Microsoft.PowerShell.Commands.ConvertFromSecureStringCommand), "Microsoft.PowerShell.Security.dll-Help.xml"),
  188.             new CmdletConfigurationEntry("ConvertTo-SecureString", typeof(Microsoft.PowerShell.Commands.ConvertToSecureStringCommand), "Microsoft.PowerShell.Security.dll-Help.xml") };
  189.  
  190.         private RunspaceConfigurationEntryCollection<CmdletConfigurationEntry> _cmdlets;
  191.  
  192.         /// <summary>
  193.         /// Cmdlets for this minishell
  194.         /// </summary>
  195.         /// <value>a collection of cmdlet configuration entries</value>
  196.         public override RunspaceConfigurationEntryCollection<CmdletConfigurationEntry> Cmdlets
  197.         {
  198.             get
  199.             {
  200.                 if (_cmdlets == null)
  201.                 {
  202.                     _cmdlets = new RunspaceConfigurationEntryCollection<CmdletConfigurationEntry>(_cmdletConfigurationEntries);
  203.                 }
  204.  
  205.                 return _cmdlets;
  206.             }
  207.         }
  208.  
  209.         private ProviderConfigurationEntry[] _providerConfigurationEntries = new ProviderConfigurationEntry[] { new ProviderConfigurationEntry("SqlServer", typeof(Microsoft.SqlServer.Management.PowerShell.SqlServerProvider), "Microsoft.SqlServer.Management.PSProvider.dll-Help.xml"),
  210.             new ProviderConfigurationEntry("Alias", typeof(Microsoft.PowerShell.Commands.AliasProvider), "System.Management.Automation.dll-Help.xml"),
  211.             new ProviderConfigurationEntry("Environment", typeof(Microsoft.PowerShell.Commands.EnvironmentProvider), "System.Management.Automation.dll-Help.xml"),
  212.             new ProviderConfigurationEntry("FileSystem", typeof(Microsoft.PowerShell.Commands.FileSystemProvider), "System.Management.Automation.dll-Help.xml"),
  213.             new ProviderConfigurationEntry("Function", typeof(Microsoft.PowerShell.Commands.FunctionProvider), "System.Management.Automation.dll-Help.xml"),
  214.             new ProviderConfigurationEntry("Registry", typeof(Microsoft.PowerShell.Commands.RegistryProvider), "System.Management.Automation.dll-Help.xml"),
  215.             new ProviderConfigurationEntry("Variable", typeof(Microsoft.PowerShell.Commands.VariableProvider), "System.Management.Automation.dll-Help.xml"),
  216.             new ProviderConfigurationEntry("Certificate", typeof(Microsoft.PowerShell.Commands.CertificateProvider), "Microsoft.PowerShell.Security.dll-Help.xml") };
  217.  
  218.         private RunspaceConfigurationEntryCollection<ProviderConfigurationEntry> _providers;
  219.  
  220.         /// <summary>
  221.         /// Providers for this minishell
  222.         /// </summary>
  223.         /// <value>a collection of provider configuration entries</value>
  224.         public override RunspaceConfigurationEntryCollection<ProviderConfigurationEntry> Providers
  225.         {
  226.             get
  227.             {
  228.                 if (_providers == null)
  229.                 {
  230.                     _providers = new RunspaceConfigurationEntryCollection<ProviderConfigurationEntry>(_providerConfigurationEntries);
  231.                 }
  232.  
  233.                 return _providers;
  234.             }
  235.         }
  236.  
  237.         private AssemblyConfigurationEntry[] _assemblyConfigurationEntries = new AssemblyConfigurationEntry[] { new AssemblyConfigurationEntry("Microsoft.SqlServer.Management.PSSnapins, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.Management.PSSnapins.dll"),
  238.             new AssemblyConfigurationEntry("Microsoft.SqlServer.Management.PSProvider, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.Management.PSProvider.dll"),
  239.             new AssemblyConfigurationEntry("Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.Smo.dll"),
  240.             new AssemblyConfigurationEntry("Microsoft.SqlServer.Dmf, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.Dmf.dll"),
  241.             new AssemblyConfigurationEntry("Microsoft.SqlServer.SqlWmiManagement, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.SqlWmiManagement.dll"),
  242.             new AssemblyConfigurationEntry("Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.ConnectionInfo.dll"),
  243.             new AssemblyConfigurationEntry("Microsoft.SqlServer.SmoExtended, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.SmoExtended.dll"),
  244.             new AssemblyConfigurationEntry("Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.Management.Sdk.Sfc.dll"),
  245.             new AssemblyConfigurationEntry("Microsoft.SqlServer.SqlEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.SqlEnum.dll"),
  246.             new AssemblyConfigurationEntry("Microsoft.SqlServer.RegSvrEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.RegSvrEnum.dll"),
  247.             new AssemblyConfigurationEntry("Microsoft.SqlServer.WmiEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.WmiEnum.dll"),
  248.             new AssemblyConfigurationEntry("Microsoft.SqlServer.ServiceBrokerEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.ServiceBrokerEnum.dll"),
  249.             new AssemblyConfigurationEntry("Microsoft.SqlServer.ConnectionInfoExtended, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.ConnectionInfoExtended.dll"),
  250.             new AssemblyConfigurationEntry("Microsoft.SqlServer.Management.Collector, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.Management.Collector.dll"),
  251.             new AssemblyConfigurationEntry("Microsoft.SqlServer.Management.CollectorEnum, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91", "Microsoft.SqlServer.Management.CollectorEnum.dll"),
  252.             new AssemblyConfigurationEntry("Microsoft.PowerShell.ConsoleHost, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "Microsoft.PowerShell.ConsoleHost.dll"),
  253.             new AssemblyConfigurationEntry("System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "System.Management.Automation.dll"),
  254.             new AssemblyConfigurationEntry("Microsoft.PowerShell.Commands.Management, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "Microsoft.PowerShell.Commands.Management.dll"),
  255.             new AssemblyConfigurationEntry("Microsoft.PowerShell.Commands.Utility, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "Microsoft.PowerShell.Commands.Utility.dll"),
  256.             new AssemblyConfigurationEntry("Microsoft.PowerShell.Security, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", "Microsoft.PowerShell.Security.dll") };
  257.  
  258.         private RunspaceConfigurationEntryCollection<AssemblyConfigurationEntry> _assemblies;
  259.  
  260.         /// <summary>
  261.         /// Type resolution assemblies for this minishell
  262.         /// </summary>
  263.         /// <value>a collection of assembly configuration entries</value>
  264.         public override RunspaceConfigurationEntryCollection<AssemblyConfigurationEntry> Assemblies
  265.         {
  266.             get
  267.             {
  268.                 if (_assemblies == null)
  269.                 {
  270.                     _assemblies = new RunspaceConfigurationEntryCollection<AssemblyConfigurationEntry>(_assemblyConfigurationEntries);
  271.                 }
  272.  
  273.                 return _assemblies;
  274.             }
  275.         }
  276.  
  277.         #endregion
  278.  
  279.         #region Types and Formats
  280.        
  281.         private TypeConfigurationEntry[] _typeConfigurationEntries = new TypeConfigurationEntry[] { new TypeConfigurationEntry(@"SQLProvider.Types.ps1xml"), new TypeConfigurationEntry(@"types.ps1xml") };
  282.  
  283.         private RunspaceConfigurationEntryCollection<TypeConfigurationEntry> _types;
  284.  
  285.         /// <summary>
  286.         /// Types for this minishell
  287.         /// </summary>
  288.         /// <value>a collection of type configuration entries</value>
  289.         public override RunspaceConfigurationEntryCollection<TypeConfigurationEntry> Types
  290.         {
  291.             get
  292.             {
  293.                 if (_types == null)
  294.                 {
  295.                     _types = new RunspaceConfigurationEntryCollection<TypeConfigurationEntry>(_typeConfigurationEntries);
  296.                 }
  297.  
  298.                 return _types;
  299.             }
  300.         }
  301.  
  302.         private FormatConfigurationEntry[] _formatConfigurationEntries = new FormatConfigurationEntry[] { new FormatConfigurationEntry(@"SQLProvider.Format.ps1xml"), new FormatConfigurationEntry(@"DotNetTypes.format.ps1xml"), new FormatConfigurationEntry(@"PowerShellCore.format.ps1xml"), new FormatConfigurationEntry(@"PowerShellTrace.format.ps1xml"), new FormatConfigurationEntry(@"FileSystem.format.ps1xml"), new FormatConfigurationEntry(@"Registry.format.ps1xml"), new FormatConfigurationEntry(@"Certificate.format.ps1xml"), new FormatConfigurationEntry(@"Help.format.ps1xml") };
  303.  
  304.         private RunspaceConfigurationEntryCollection<FormatConfigurationEntry> _formats;
  305.  
  306.         /// <summary>
  307.         /// Formats for this minishell
  308.         /// </summary>
  309.         /// <value>a collection of format configuration entries</value>
  310.         public override RunspaceConfigurationEntryCollection<FormatConfigurationEntry> Formats
  311.         {
  312.             get
  313.             {
  314.                 if (_formats == null)
  315.                 {
  316.                     _formats = new RunspaceConfigurationEntryCollection<FormatConfigurationEntry>(_formatConfigurationEntries);
  317.                 }
  318.  
  319.                 return _formats;
  320.             }
  321.         }
  322.  
  323.         #endregion
  324.  
  325.        
  326.  
  327.         #region scripts and initscript
  328.  
  329.         private RunspaceConfigurationEntryCollection<ScriptConfigurationEntry> _scripts;
  330.  
  331.         /// <summary>
  332.         /// Pre-defined scripts for this minishell
  333.         /// </summary>
  334.         /// <value>a collection of script configuration entries</value>
  335.         public override RunspaceConfigurationEntryCollection<ScriptConfigurationEntry> Scripts
  336.         {
  337.             get
  338.             {
  339.                 if (_scripts == null)
  340.                 {
  341.                     _scripts = new RunspaceConfigurationEntryCollection<ScriptConfigurationEntry>();
  342.  
  343.                     string[] scriptNames = null;
  344.                     string[] scripts = null;
  345.  
  346.                     GetResources(ScriptResourceName, ref scriptNames, ref scripts);
  347.  
  348.                     if (scripts != null)
  349.                     {
  350.                         for (int i = 0; i < scripts.Length; i++)
  351.                         {
  352.                             ScriptConfigurationEntry data = new ScriptConfigurationEntry(scriptNames[i], scripts[i]);
  353.                             _scripts.Append(data);
  354.                         }
  355.                     }
  356.                 }
  357.  
  358.                 return _scripts;
  359.             }
  360.         }
  361.  
  362.         private RunspaceConfigurationEntryCollection<ScriptConfigurationEntry> _initializationScripts;
  363.  
  364.         /// <summary>
  365.         /// Initialization scripts to be run at minishell start up.
  366.         /// </summary>
  367.         /// <value>a collection of initialization scripts</value>
  368.         public override RunspaceConfigurationEntryCollection<ScriptConfigurationEntry> InitializationScripts
  369.         {
  370.             get
  371.             {
  372.                 if (_initializationScripts == null)
  373.                 {
  374.                     _initializationScripts = new RunspaceConfigurationEntryCollection<ScriptConfigurationEntry>();
  375.  
  376.                     Dictionary<string, object> profile = GetResources(ProfileResourceName, CultureInfo.InvariantCulture);
  377.  
  378.                     if (profile == null)
  379.                         return null;
  380.  
  381.                     if (profile != null && profile.ContainsKey(ProfileResourceName))
  382.                     {
  383.                         _initializationScripts.Append(new ScriptConfigurationEntry(ProfileResourceName, (string)profile[ProfileResourceName]));
  384.                     }
  385.                 }
  386.  
  387.                 return _initializationScripts;
  388.             }
  389.         }
  390.  
  391.         #endregion
  392.  
  393.         #region Minishell Help
  394.  
  395.         static private string _shellHelp = null;
  396.         static private string ShellHelp
  397.         {
  398.             get
  399.             {
  400.                 if(_shellHelp == null)
  401.                     LoadHelpResource();
  402.  
  403.                 return _shellHelp;
  404.             }
  405.         }
  406.  
  407.         static private string _shellBanner = null;
  408.         static private string ShellBanner
  409.         {
  410.             get
  411.             {
  412.                 if(_shellBanner == null)
  413.                     LoadHelpResource();
  414.  
  415.                 return _shellBanner;
  416.             }
  417.         }
  418.  
  419.         static private void LoadHelpResource()
  420.         {
  421.             Dictionary<string,object> helpResources = GetResources(HelpResourceName, CultureInfo.CurrentUICulture);
  422.  
  423.             if(helpResources != null)
  424.             {
  425.                 if(helpResources.ContainsKey(ShellHelpResourceKey))
  426.                 {
  427.                     _shellHelp = (string) helpResources[ShellHelpResourceKey];
  428.                 }
  429.  
  430.                 if(helpResources.ContainsKey(ShellBannerResourceKey))
  431.                 {
  432.                     _shellBanner = (string) helpResources[ShellBannerResourceKey];
  433.                 }
  434.             }
  435.  
  436.             return;
  437.         }
  438.  
  439.         #endregion
  440.  
  441.         #region Resource Loading
  442.  
  443.         private const string ScriptResourceName = "script";
  444.         private const string ProfileResourceName = "initialization";
  445.         private const string HelpResourceName = "help";
  446.         private const string ShellHelpResourceKey = "ShellHelp";
  447.         private const string ShellBannerResourceKey = "ShellBanner";
  448.         private const string ResourceListKey = "__resourceList__";
  449.  
  450.         /// <summary>
  451.         /// Return all resources in ordered name/value pairs. Order of resources are
  452.         /// defined in resourceListKey. This will look for resources for invariant
  453.         /// culture.
  454.         /// </summary>
  455.         private static void GetResources(string resourceType, ref string[] names, ref string[] values)
  456.         {
  457.             names = null;
  458.             values = null;
  459.  
  460.             Dictionary<string, object> resources = GetResources(resourceType, CultureInfo.InvariantCulture);
  461.  
  462.             if(resources == null)
  463.                 return;
  464.  
  465.             if(!resources.ContainsKey(ResourceListKey))
  466.             {
  467.                 return;
  468.             }
  469.  
  470.             names = (string[])resources[ResourceListKey];
  471.  
  472.             if(names == null || names.Length == 0)
  473.                 return;
  474.  
  475.             values = new string[names.Length];
  476.  
  477.             for (int i = 0; i < names.Length; i++)
  478.             {
  479.                 values[i] = (string)resources[names[i]];
  480.             }
  481.  
  482.             return;
  483.         }
  484.  
  485.         /// <summary>
  486.         /// Return all resources in defined in current assembly in the format of a string/object dictionary.
  487.         /// </summary>
  488.         private static Dictionary<string, object> GetResources(string resourceType, CultureInfo culture)
  489.         {
  490.             ResourceManager resMgr = new ResourceManager(resourceType, Assembly.GetExecutingAssembly());
  491.             if (resMgr == null)
  492.                 return null;
  493.  
  494.             ResourceSet resSet = null;
  495.             try
  496.             {
  497.                 resSet = resMgr.GetResourceSet(culture, true, true);
  498.             }
  499.             catch (MissingManifestResourceException)
  500.             {
  501.                 return null;
  502.             }
  503.  
  504.             if (resSet == null)
  505.                 return null;
  506.  
  507.             Dictionary<string, object> result = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
  508.  
  509.             IDictionaryEnumerator id = resSet.GetEnumerator();
  510.             while (id.MoveNext())
  511.             {
  512.                 result[(string)id.Key] = id.Value;
  513.             }
  514.  
  515.             return result;
  516.         }
  517.  
  518.         #endregion
  519.  
  520.         /// <summary>
  521.         /// This is the main function for minishell. It will start msh console shell with
  522.         /// a runspace configuration instance for current minishell.
  523.         /// </summary>
  524.         /// <param name="args">args to main</param>
  525.         /// <returns>minishell execution status</returns>
  526.         [LoaderOptimization(LoaderOptimization.MultiDomain)]
  527.         public static int Main(string[] args)
  528.         {
  529.             RunspaceConfiguration configuration = Create();
  530.  
  531.             return Microsoft.PowerShell.ConsoleShell.Start(configuration, ShellBanner, ShellHelp, args);
  532.         }
  533.     }
  534. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me