PowerShell:CLI或GUI - 你需要或喜欢什么? [英] PowerShell: CLI or GUI - which do you need or prefer?

查看:237
本文介绍了PowerShell:CLI或GUI - 你需要或喜欢什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerShell v1.0显然是一个基于控制台的管理shell。它并不需要GUI界面。如果需要,如Exchange 2007管理GUI,它是建立在PowerShell之上。您可以在PowerShell脚本中使用Windows窗体创建自己的GUI。我的问题是,什么样的PowerShell脚本或管理任务,你认为最好是加上甚至一个简单的图形界面?你创建了winforms完成?

解决方案

在回答 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆