当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Friday Fun - 弹出式替代方案

作者:精品下载站 日期:2024-12-14 07:43:48 浏览:13 分类:玩电脑

Friday Fun - 弹出式替代方案


[玩转系统] Friday Fun - 弹出式替代方案

它可能不性感,但是使用记事本怎么样?就是这样。首先,您需要显示一些内容。使用此处字符串是一种很好的方法,因为它可以轻松创建多行文本块。

$text=@"
This part of the script has completed.
Close Notepad to continue.
Thank you.
"@

使用此处字符串,您还可以插入变量。

$var = "Foo"
$text=@"
$(Get-Date)
Processing has finished on $($env:COMPUTERNAME)

The final value of var is $var
"@

接下来,使用文本创建一个临时文件。

$text | Out-File -FilePath d:\temp\mytemp.txt -Encoding ascii

要显示该消息,您只需运行记事本即可。

notepad d:\temp\mytemp.txt

这将返回您的 PowerShell 提示符,但您希望使弹出窗口更加融入您的脚本,在这种情况下,我建议使用 Start-Process。

Start-Process notepad -ArgumentList d:\temp\mytemp.txt -Wait -noNewWindow

主要优点是,当您关闭记事本时,脚本会继续运行,因此您可以删除临时文件。当我们提到临时文件时,如果您尝试过我的演示,您会注意到文件名显示在标题栏中。您的文件没有扩展名的要求。

[string]$Title = "Script Complete"
$tmpFile = Join-Path -Path $env:temp -ChildPath $Title
$text | Out-File -FilePath $tmpFile -Encoding ascii
Start-Process notepad -ArgumentList $tmpFile -Wait -noNewWindow

请注意我使用 Join-Path 来创建文件名。我建议您使用此 cmdlet,而不是尝试将各个片段连接在一起。但现在我得到了一些更好的东西。

[玩转系统] Friday Fun - 弹出式替代方案

使用记事本,您可以将文件保存在其他地方,甚至可以打印它。我什至为你创建了一个可重用的函数。

Function New-NotepadPopup {

<#
.Synopsis
Display a popup message in Notepad
.Description
This command displays a text message in a Notepad window.
.Parameter Text
The text to be displayed in Notepad. Using a here string is recommended.
.Parameter Title
This will be used for the file name. You don't need to include a file extension.
.Parameter Wait
Force the PowerShell to wait until Notepad is closed. 
.Example
PS C:\> $text=@"
This part of the script has completed.
Close Notepad to continue.
Thank you.
"@
PS C:\> New-NotepadPopup $Text -title "Finished" -wait
.Notes
Last Updated: September 10, 2014
Version     : 1

.Link
Start-Process
#>

[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter text to be displayed")]
[ValidateNotNullorEmpty()]
[string]$Text,
[Parameter(Position=1)]
[ValidateNotNullorEmpty()]
[ValidatePattern({^\w+$})]
[string]$Title = "Notice",
[switch]$Wait
)

Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"  

#create a temp filename
$tmpFile = Join-Path -Path $env:temp -ChildPath $Title

#create the temp file
Write-Verbose "Creating $tmpFile"
$text | Out-File -FilePath $tmpFile -Encoding ascii

#launch notepad and wait
Write-Verbose "Displaying Notepad"

#hashtable of values to splat to Start-Process
$paramHash = @{
 Filepath = join-path -path $env:windir -childpath notepad.exe
 argumentList = $tmpFile
 NoNewWindow = $True
}

If ($Wait) {
    $paramHash.Add("Wait",$True)
}

Start-Process @paramHash

#if not waiting sleep for a few seconds to give Notepad a chance to display the file
if (-Not $Wait) {
    Start-Sleep -Seconds 2
}

#delete temp file
Write-Verbose "Deleting $tmpFile"
Remove-Item $tmpFile

Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"


} #end function

此功能将自动为您删除临时文件。如果您有另一个可以从命令行启动的文本文件查看器,则可以使用它来代替记事本。正如我在开始时提到的,这并不是令人兴奋的 PowerShell,但我希望您能学到一些新的有用的东西。周末愉快。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯