问题
PowerShell v1.0 is obviously a console based administrative shell. It doesn't really require a GUI interface. If one is required, like the Exchange 2007 management GUI, it is built on top of PowerShell. You can create your own GUI using Windows Forms in a PowerShell script. My question is, "What sort of PowerShell scripts or management tasks do you think would be best served with the addition of even a simple graphical interface? What have you created winforms to accomplish?"
回答1:
In answer to monkut's suggestion, here's a simple function to get file paths using the WindowsForms OpenFileDialog
:
[void] [Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )
function Select-File( [string]$initialDirectory=$pwd, [switch]$multiselect ) {
$dialog = New-Object Windows.Forms.OpenFileDialog
$dialog.ShowHelp = $true # http://tinyurl.com/6cnmrr
$dialog.InitialDirectory = $initialDirectory
$dialog.Multiselect = $multiselect
if( $dialog.ShowDialog( ) -eq 'OK' ) { $dialog.FileNames }
$dialog.Dispose( )
}
I also tried creating a similar Select-Directory
function, but FolderBrowserDialog's STA thread requirement is rather difficult to achieve in PowerShell v1.
Edit: Thanks to Gordon, here's a workaround to show the FolderBrowserDialog
using COM:
function Select-Directory( ) {
$app = New-Object -COM Shell.Application
$directory = $app.BrowseForFolder( 0, "Select Directory", 0 )
$path = $directory.Self.Path
if( $path ) { return $path }
}
回答2:
My favorite old gui tool for a command line shell was a select directory tool that when exectued from the command line would open a gui directory selection dialog. Very help full when you have to cd to some other directory with a long path name.
I couldn't find the old exe tool I used to use unfortunalty. (It was pre-powershell anyway) Also, I'm not familiar enough with powershell to figure out how to call the dialog from powershell directly and make this a cmd-let or whatever, but here's what I'm talking about with some python mixed in.
Here it is:
PS D:\> $dir = & "C:\python25\python.exe" "C:\python25\selectdir.pyw"; cd $dir;
# Directory selection dialog opens here, user selects the directory to goto.
PS D:\NewDirectory>
And the python code:
import Tkinter
import tkFileDialog
root = Tkinter.Tk()
root.withdraw()
dirname = tkFileDialog.askdirectory(parent=root)
print dirname
I'd like to have this tool again, if anyone knows how to clean this up so I can just call a command like, "cdir" or something please comment.
回答3:
Isnt this mainly used by powershell guys who want to make there scripts pretty before handing to users or people who know nothing about a console and like pretty pictures ?!
回答4:
I would head over to the PowerGUI site which I'm sure you are already aware of Jeff. The sort of things that users are doing there are what I would consider exemplars of the fusion of powershell and a GUI. The ability to generate a list of objects then drill down into the hierarchical structure of the objects, or run scripts on an object. This sort of thing is where I would see a GUI interface shine.
回答5:
I'm hopeful for some examples here because I'm sure there's loads of tasks I would be able to delegate down if they had a pretty UI on them and were usable with a mouse.
But as far as adding a user interface, I'm having trouble of thinking of a spot where there's not one. Most of the examples I can think of are either just dumbing down or exposing a selected section of an existing UI. They are typically just a different view to the existing data that's not there, or hard to see in the provided management console. Or they extend some functionality with looping or pipelining (you can still use the mouse to change the names of 200 files if you want). Almost everything I've done that's still in use is either one-shot scripts, or sitting as a scheduled task somewhere.
I'm waiting for ideas :)
回答6:
Actually, thinking more about this, In the examples I have seen that use a GUI they seem to be multiple input front ends, something that is done alot easier than a command line with several switches which may be quite long and harder to visualise.
来源:https://stackoverflow.com/questions/182573/powershell-cli-or-gui-which-do-you-need-or-prefer