ACR_ServerMisc.ACR_ServerMisc.RunPowerShellScriptlet C# (CSharp) Method

RunPowerShellScriptlet() private method

Run a PowerShell script and send the results to a player.
private RunPowerShellScriptlet ( string Script, uint PCObjectID ) : bool
Script string Supplies the script source text to /// execute.
PCObjectID uint Supplies the PC object ID of the player /// to notify of the results. If OBJECT_INVALID, then the results are /// written to the server log.
return bool
        private bool RunPowerShellScriptlet(string Script, uint PCObjectID)
        {
            string Result = null;
            bool CompletedOk = true;

            try
            {
                Script = "Param([Parameter()] $s, [Parameter()] [System.UInt32] $OBJECT_SELF, [Parameter()] [System.UInt32] $OBJECT_INVALID, [Parameter()] [System.UInt32] $OBJECT_TARGET, [Parameter()] $sql, [Parameter()] $CreatureAI) try { " + Script + " } catch { $_ }";

                using (PowerShell Shell = PowerShell.Create())
                {
                    Dictionary<string, object> Arguments = new Dictionary<string, object>();
                    object AIServer = GetCreatureAIServer();

                    Arguments["s"] = this;
                    Arguments["OBJECT_SELF"] = PCObjectID;
                    Arguments["OBJECT_INVALID"] = OBJECT_INVALID;
                    Arguments["OBJECT_TARGET"] = GetPlayerCurrentTarget(PCObjectID);
                    Arguments["sql"] = GetDatabase();
                    Arguments["CreatureAI"] = AIServer;

                    Shell.AddScript(Script);
                    Shell.AddParameters(Arguments);
                    Shell.AddCommand("Out-String");

                    foreach (string Line in Shell.Invoke<string>())
                    {
                        if (String.IsNullOrEmpty(Result))
                            Result = "";
                        else
                            Result += "\n";

                        Result += Line;
                    }

                    if (String.IsNullOrWhiteSpace(Result))
                        Result = "<No output returned>";
                }
            }
            catch (Exception e)
            {
                Result = "Exception: " + e.ToString();
                CompletedOk = false;
            }

            if (PCObjectID == OBJECT_INVALID)
                WriteTimestampedLogEntry("ACR_ServerMisc.RunPowerShellScriptlet: Result: " + Result);
            else
                SendMessageToPC(PCObjectID, Result);

            return CompletedOk;
        }