Cuma Eğlencesi - Pop-up Alternatifi
Seksi olmayabilir ama Not Defteri'ni kullanmaya ne dersiniz? İşte nasıl yapılacağı. İlk olarak, görüntülenecek bir içeriğe ihtiyacınız var. Here dizesini kullanmak, çok satırlı bir metin bloğu oluşturmayı kolaylaştırdığından iyi bir yaklaşımdır.
$text=@"
This part of the script has completed.
Close Notepad to continue.
Thank you.
"@Here dizesiyle değişkenler de ekleyebilirsiniz.
$var = "Foo"
$text=@"
$(Get-Date)
Processing has finished on $($env:COMPUTERNAME)
The final value of var is $var
"@Daha sonra metinle geçici bir dosya oluşturun.
$text | Out-File -FilePath d:\temp\mytemp.txt -Encoding asciiMesajı görüntülemek için yapmanız gereken tek şey Not Defteri'ni çalıştırmaktır.
notepad d:\temp\mytemp.txt
Bu, PowerShell isteminizi döndürecektir ancak açılır pencereyi betiğinize daha entegre hale getirmek istiyorsunuz, bu durumda Başlatma Sürecini kullanmanızı öneririm.
Start-Process notepad -ArgumentList d:\temp\mytemp.txt -Wait -noNewWindowEn büyük avantajı, Not Defteri'ni kapattığınızda komut dosyanızın devam etmesi ve böylece geçici dosyayı silebilmenizdir. Hazır geçici dosyadan bahsederken demomu denediyseniz başlık çubuğunda dosya adının görüntülendiğini fark edeceksiniz. Dosyanızın uzantısı olması zorunluluğu yoktur.
[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 -noNewWindowBir dosya adı oluşturmak için join-path kullandığıma dikkat edin. Parçaları bir araya getirmeye çalışmak yerine bu cmdlet'i kullanmanızı öneririm. Ama şimdi biraz daha güzel bir şey alıyorum.
Not Defteri ile dosyayı başka bir yere kaydedebilir, hatta yazdırabilirsiniz. Hatta sizin için yeniden kullanılabilir bir işlev bile oluşturdum.
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 functionBu işlev geçici dosyayı sizin için otomatik olarak silecektir. Komut satırından başlatabileceğiniz başka bir metin dosyası görüntüleyiciniz varsa Not Defteri yerine onu kullanabilirsiniz. Başlangıçta belirttiğim gibi bu pek heyecan verici bir PowerShell değil ama umarım yeni ve kullanışlı bir şey yakalamışsınızdır. Harika bir hafta sonu geçirin.
