Hacker News new | ask | show | jobs
by nathanaldensr 1029 days ago
Windows PowerShell targets the old .NET Framework, which is Windows-dependent. Modern PowerShell targets .NET [Core] and is cross-platform. Also, it's not true that you need to use "CallMethod;" PowerShell can invoke methods with syntax nearly identical to C#'s.
1 comments

edited my comment above for clarity - I didn't mean literally CallMethod, I meant that normally Powershell uses 'shell' syntax where parameters are separated by spaces

Do-Something withthisfile.txt -AdditionalParam 23

But if you're calling a .NET method you switch to C# syntax with brackets and commas:

$str.Trim('-')

I mean thats kindof obvious but its one of the many things that makes Powershell's learning curve a little steeper. But hey we can always just google it which is par for the course anyway nowadays

Not sure, but I think the above commentor was meaning to tell that powershell supports .NET reflection.

That is, while you can call .NET methods on objects like:

$Object.SomeMethod('arguments')

You can also do:

[Namespace.ClassName]::SomeMethod('arguments')

And your Powershell scripts can even save you a lot of typing with the ```using``` function of Powershell to load namespaces: https://learn.microsoft.com/en-us/powershell/module/microsof...

Edit: As to why you would want to do this, usually it's because you want to do some scripting but you need to pull data out of some .NET application or out of Windows and there isn't a fully fleshed out cmdlet built for it. In a pinch, you can usually just use reflection to get it. For example, I needed to pull data from a Shared Outlook calendar that not everyone had access to and then do some date-math to share a time-sensitive schedule with a team living in quite a few timezones. Reflection to load the necessary Outlook and MSExchange elements, then the rest in Powershell. Now I have a script that is easy to pass around, can do this if I'm unavailable, and we can easily add/remove time zones/persons, and it just needed 5-ish lines of .NET inside of the Powershell script.

Or if you just want to parse date time in a script.