Generating Powershell scripts by working in Windows GUI
Asked Answered
G

3

7

My understanding is that virtually all "real work" done by the various GUI components in Windows Server 2012 (for example, in Disk Management) is performed by invoking Powershell. (That is, there's nothing that can be done in the GUI that can't be done with Powershell.)

The ability to see and/or collect the various Powershell actions taken by Windows as you're using the GUI would be terribly valuable. You would turn this feature on, do the configuration work you need to do, and turn it off, resulting in one or more Powershell scripts that automate what you just did by hand. (Of course, the script(s) likely could be parameterized in various ways.) The time saved by not having to figure out which cmdlets are used to do various tasks, and what options are passed to them, would be enormous -- even just knowing what cmdlets are used would give you quite a leg up on figuring out how to automate things.

This capability would be just like turning on the Macro Recorder in Excel, doing your operation, and turning it off -- examining the resulting macro tells you what underlying objects are being manipulated, and how, to obtain the result that you got easily using the Excel GUI.

Does this capability exist? How do you turn it on and off? (I've googled and binged myself silly but have not seen anything that says how to do it -- or even says explicitly that it's possible -- even though I could swear I've read/heard about it on some blog or podcast, back at a time when I didn't care about Powershell.)

Any information would be appreciated.

Gambill answered 3/7, 2013 at 22:22 Comment(4)
I believe what you heard of is the opposite: automating the UI with Powershell.Halfsole
Some products (System Center for instance) allow you to display the generated PowerShell code, but AFAIK there is no system-wide macro recorder. Generated code also requires quite a bit of tuning if you want to turn it into a useful script.Clank
I think the vast majority of Windows components/products "host" powershell. They compile in powershell support into the component using .NET classes in system.management.automation. So, a macro capability would have to built into the (moderately) low level Powershell APIs. (At this level there is no Powershell command line or ISE GUI, it's just APIs) Since the component is "driving" the API, it would have to be the one to turn on a macro recorder. So even if there was macro recording it would still lead to what Ansgar said - support would be on a component by component basis.Kwangtung
This doesn't mean the GUI achieves what it needs by calling PowerShell. It just means Powershell and the GUI can use the same common underlying library (i.e. .Net) to do the same things.Cheng
R
1


I don't believe the feature you're describing is available in Windows - yet. I believe it will be, since Microsoft has mandated that all new features must be configurable via PowerShell. Since the OS should soon be at the point that all functionality is executable via PowerShell, it makes sense that they would include the option to translate UI actions into a list of PowerShell commands that achieve the same functionality.

Romelda answered 15/12, 2014 at 16:32 Comment(0)
M
1

If a GUI-tool is truly using PowerShell cmdlets, you can just enable PowerShell Script Block Logging in a group policy and look in the EventViewer for the corresponding commands executed.

However, the only GUI I've run into that truly runs PowerShell cmdlets is that of MS Exchange, where you can actually have the GUI reveal the cmdlet it's using.
(Hopefully there are more examples.)

Militant answered 13/5 at 8:26 Comment(0)
M
0

I don't have the exact answer to your question, but the things you may have read/heard about on some blog or podcast are :

first : The Start-Transcript and Stop-Transcrip CmdLets that allow you to record all the command lines you type at the PowerShell prompt.

Second : As you are using PowerShell 3 you are using ISE which is now, a not so bas PowerShell editor. But it was not so at the begining and I was using a free editor from Quest Software called PowerGUI. When you download this editor it comes with an other soft called administrative console. This administrative console allow you generate PowerShell for basic operations and comes with a WMI browser that also generate PowerShell scripts.

Third : It exists also a PowerShell Scriptomatic, but I just never use it.

Magnific answered 4/7, 2013 at 4:22 Comment(1)
Quest that wrote PowerGUI was bought by DELL in 2021 that later discontinued the product. It can however still be download from SoftPedia at your own risk. It can also be downloaded from WayBack Machine.Militant

© 2022 - 2024 — McMap. All rights reserved.