您可以使用 Windows 窗体或 WPF 创建 PowerShell GUI。你发布的是一个批处理文件。如果这是您所追求的,您仍然可以使用 PowerShell 创建控制台 GUI。
示例控制台应用程序:
Friday Fun – A PowerShell Console Menu
作者的例子:
#Requires -version 2.0
<#
-----------------------------------------------------------------------------
Script: Demo-ConsoleMenu.ps1
Version: 1.0
Author: Jeffery Hicks
http://jdhitsolutions.com/blog
http://twitter.com/JeffHicks
http://www.ScriptingGeek.com
Date: 12/30/2011
Keywords: Read-Host, Menu, Switch
Comments:
The best way to create a menu is to use a here string
Use -ClearScreen if you want to run CLS before displaying the menu
"Those who forget to script are doomed to repeat their work."
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
-----------------------------------------------------------------------------
#>
Function Show-Menu {
Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter your menu text")]
[ValidateNotNullOrEmpty()]
[string]$Menu,
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[string]$Title="Menu",
[switch]$ClearScreen
)
if ($ClearScreen) {Clear-Host}
#build the menu prompt
$menuPrompt=$title
#add a return
$menuprompt+="`n"
#add an underline
$menuprompt+="-"*$title.Length
$menuprompt+="`n"
#add the menu
$menuPrompt+=$menu
Read-Host -Prompt $menuprompt
} #end function
#define a menu here string
$menu=@"
1 Show info about a computer
2 Show info about someones mailbox
3 Restarts the print spooler
Q Quit
Select a task by number or Q to quit
"@
#Keep looping and running the menu until the user selects Q (or q).
Do {
#use a Switch construct to take action depending on what menu choice
#is selected.
Switch (Show-Menu $menu "My Help Desk Tasks" -clear) {
"1" {Write-Host "run get info code" -ForegroundColor Yellow
sleep -seconds 2
}
"2" {Write-Host "run show mailbox code" -ForegroundColor Green
sleep -seconds 5
}
"3" {Write-Host "restart spooler" -ForegroundColor Magenta
sleep -seconds 2
}
"Q" {Write-Host "Goodbye" -ForegroundColor Cyan
Return
}
Default {Write-Warning "Invalid Choice. Try again."
sleep -milliseconds 750}
} #switch
} While ($True)
这里是另一个控制台示例:
Creating Menus in Powershell
否则在整个网络上都有大量的 WinForms 和 WPF GUI 开发示例,
示例:
PowerShell and WPF: Introduction and Building Your First Window
如果您更喜欢视觉学习者,请观看 YouTube 视频:
powershell wpf gui
关于问题的 OP 更新
明白了。
至于……
“基本上,我想在 Xaml 中创建变量,以便我知道
powershell.... $gridrow = 3 ...",
因此,您所追求的更多细节/示例至关重要。然而,您可以在 XAML 中定义变量,这在 MS 文档和问答帖子中有记录。
How can I define a variable in XAML?
Can I declare variable in the XAML code?
根据文章
# After definitition:
<Window x:Class="WpfApplication1_delete_test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="Window1">
<Window.Resources>
<System:String x:Key="myStr">init</System:String>
</Window.Resources>
<TextBlock Name="txtBlock" Text="{Binding Source={StaticResource myStr}}"/>
</Window>
# you can use from code like that:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Resources["myStr"] = "string";
txtBlock.Text = FindResource("myStr").ToString();
}
}