PowerShell:CLI或GUI - 你需要或喜欢什么? [英] PowerShell: CLI or GUI - which do you need or prefer?
问题描述
在回答 monkut的建议,这里有一个简单的函数来获取文件路径使用WindowsForms OpenFileDialog
:
[void] [Reflection.Assembly] :: LoadWithPartialName('System.Windows.Forms')
function Select-File $ 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()
}
我还尝试创建一个类似的 Select-Directory
函数,但 FolderBrowserDialog
的STA线程需求在PowerShell v1中很难实现。
修改:由于 Gordon ,这里有一个解决方法以显示使用COM的 FolderBrowserDialog
:
{
$ app = New-Object -COM Shell.Application
$ directory = $ app.BrowseForFolder(0,Select Directory,0)
$ path = $ directory.Self.Path
if($ path){return $ path}
}
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?"
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 }
}
这篇关于PowerShell:CLI或GUI - 你需要或喜欢什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!