✅ 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
Within my application I have a PowerShellHelper.cs where I'm trying to use System.Management.Automation with the below method
35 Replies
Also tried
and in my class that is calling it, I'm calling it something like
I was able to get results with a simple test:
and the script:
i think i would use json too
This thing is kicking my butt..
It will not display anything with the above data at all using Json, but it will return something with this, but then it's just a big jumbled together mess of string.
I am using WinUI 3.0
Do you think that would make it any different?
you are not serializing the data
try piping the datatable to ConvertTo-Json
or probably better, first make a list of your object
like @{} with all the fields, the json, the send it to your app
the json usage was just because "toString" on DataRow just outputs the type. You shouldn't need JSON here, the object was visible in the debugger without it
For some reason I'm not able to see any data...
I am using the same code you're using and I made a new WIN UI project just to make it fresh to test it and it keeps coming back as null data
but I can run the script from powershell and return the data
I don't think WinUI will cause any issue, but have you tried with just a commandline app just in case?
othwerwise, the next thing to check is that this returns anything
Collection<PSObject> results = powerShellInstance.Invoke();
If it does, the rest is just failing because of conversion (likely)So this is the result that I'm getting by using this
what's in test.ps1?
Just that one column 1
It's the script you sent above
interesting... what version of .NET are you using? and packages?
is my csproj
is what I see in the debugger
So maybe I need to reference .NET 7
idk, but I'd reduce differences until we figure it out
ok, changing now...
I also wonder if there's some issue with script execution policy... if the script isn't returning anything that could be it? does it have errors on invocation?
I always see this error when changing
The project 'TableTest' ran into a problem during the last operation: A numeric comparison was attempted on "$(TargetPlatformVersion)" that evaluates to "" instead of a number, in condition "'$(TargetPlatformVersion)' < '10.0.18362.0'". C:\Users\gposab7.nuget\packages\microsoft.windowsappsdk\1.2.221109.1\buildTransitive\Microsoft.InteractiveExperiences.Common.targets You may need to reload the solution after fixing the problem.
Execution policy is Bypass
just try a new console project, not winui
ok
That matches now
is there anything in the error stream of the psinstance?
{System.Management.Automation.PSDataCollection<System.Management.Automation.ErrorRecord>}
does it have errors in the error stream?
Is that what you were looking for?
it will be null on that line because it's not initialized yet. Look at
powerShellInstance.Streams.Error
when broken on a line, the line hasn't been executed yetfound the error
But I can run the script outside of this
try setting execution policy to unrestricted?
I'm not quite a powershell expert, but policy is definitely the problem here
Alright, I will have to try this on a non work laptop as I don't have permissions to change policy.
I'll let you know, thank you!
you might be able to do
var sessionState = InitialSessionState.CreateDefault(); sessionState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted; PowerShell powerShellInstance = PowerShell.Create(sessionState);but it's beyond my expertise at that point, just a guess based on API structure 🙂 I know you can invoke ps with a different execution policy than the default
That worked!
even though my execution policy was set to unrestricted it still complained, idk y
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.