C#C
C#3y ago
Sir Ruggie

✅ PowerShell Method in WinUI C#

So firstly, yes I have to use Powershell and am unable to just do it in C#.

Below will be an example of a basic PowerShell script that I have
My issue is that I'm returning a System.Data.Datarow, but I'm unable to return any data in C#.

PowerShell Console Results
PS R:\> \\ris1\xnet\data\Wan\RestDB_Dependencies\PowerShell_Scripts\POS\Get-POS.ps1 -Site 1124

POS              POSNumber
---              ---------
POS 1 - Cashier  01
POS 2 - Takeout  02
POS 3 - Takeout  03
POS 4 - Bar      04
POS 5 - Takeout  05
POS 6 - Takeout  06
POS 7 - Takeout  07
POS 8 - Takeout  08


PS R:\> \\ris1\xnet\data\Wan\RestDB_Dependencies\PowerShell_Scripts\POS\Get-POS.ps1 -Site 1124 |GM


   TypeName: System.Data.DataRow

Name              MemberType            Definition
----              ----------            ----------
AcceptChanges     Method                void AcceptChanges()
BeginEdit         Method                void BeginEdit()
CancelEdit        Method                void CancelEdit()
ClearErrors       Method                void ClearErrors()
Delete            Method                void Delete()
EndEdit           Method                void EndEdit()
Equals            Method                bool Equals(System.Object obj)
GetChildRows      Method                System.Data.DataRow[] GetChildRows(string relationName), System.Data.DataRow[] GetChildRows(string relationName, ...
GetColumnError    Method                string GetColumnError(int columnIndex), string GetColumnError(string columnName), string GetColumnError(System.Da...
GetColumnsInError Method                System.Data.DataColumn[] GetColumnsInError()
GetHashCode       Method                int GetHashCode()
GetParentRow      Method                System.Data.DataRow GetParentRow(string relationName), System.Data.DataRow GetParentRow(string relationName, Syst...
GetParentRows     Method                System.Data.DataRow[] GetParentRows(string relationName), System.Data.DataRow[] GetParentRows(string relationName...
GetType           Method                type GetType()
HasVersion        Method                bool HasVersion(System.Data.DataRowVersion version)
IsNull            Method                bool IsNull(int columnIndex), bool IsNull(string columnName), bool IsNull(System.Data.DataColumn column), bool Is...
RejectChanges     Method                void RejectChanges()
SetAdded          Method                void SetAdded()
SetColumnError    Method                void SetColumnError(int columnIndex, string error), void SetColumnError(string columnName, string error), void Se...
SetModified       Method                void SetModified()
SetParentRow      Method                void SetParentRow(System.Data.DataRow parentRow), void SetParentRow(System.Data.DataRow parentRow, System.Data.Da...
ToString          Method                string ToString()
Item              ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(string columnName) {get;set;}, System.Object I...
POS               Property              string POS {get;set;}
POSNumber         Property              string POSNumber {get;set;}


Within my application I have a PowerShellHelper.cs where I'm trying to use System.Management.Automation with the below method

        public static Collection<PSObject> GetTableData(string script)
        {
            using (PowerShell powerShellInstance = PowerShell.Create())
            {
                Debug.WriteLine("Script: " + script);
                powerShellInstance.AddScript(script);


                Collection<PSObject> results = powerShellInstance.Invoke();

                if (results == null || results.Count == 0)
                {
                    Debug.WriteLine("No results returned.");
                }
                else
                {
                    Debug.WriteLine("Results: " + results.ToString());
                }

                return results;
            }
        }
Was this page helpful?
✅ PowerShell Method in WinUI C# - C#