Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
|
Lista Forumurilor Pe Tematici
|
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login
POZE MRRRR'S FORUM (VIEW ONLY)
Nu sunteti logat.
|
Nou pe simpatie: lovely_pink pe Simpatie
 | Femeie 24 ani Bucuresti cauta Barbat 26 - 49 ani |
|
Mrrrr
AdMiN
 Inregistrat: acum 19 ani
Postari: 2386
|
|
Option 1 - hybrid .bat file containing powershell
The block colored in this color is a Polyglot trick (a script valid in two different programming languages simultaneously).
How Windows CMD Sees It - CMD treats <# : as a command line, but because of the colon :, it treats <# as a valid label marker and simply skips to the next line without throwing an error. - It executes @echo off. - It runs the powershell command line. - %~f0 resolves to the full path of the .bat file itself. - Get-Content '%~f0' reads the entire file content into PowerShell. - Invoke-Expression evaluates that content as native PowerShell code. - It hits exit /b and exits the batch process immediately, so CMD never reaches the raw PowerShell lines below.
How PowerShell Sees It - PowerShell evaluates the file via Invoke-Expression: - It sees <# at the very top, which opens a multi-line comment block. - It treats @echo off, the powershell... command, and exit /b as comment text and ignores them completely. - It reaches #>. The comment block closes. - It proceeds to execute all your WinForms / PowerShell GUI code starting on line 6 as normal code.
<# : @echo off powershell -NoProfile -ExecutionPolicy Bypass -Command "Invoke-Expression (Get-Content '%~f0' | Out-String)" exit /b #>
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing
# --- WinForms UI Setup --- $form = New-Object System.Windows.Forms.Form $form.Text = 'Input Requested' $form.Size = New-Object System.Drawing.Size(350,170) $form.StartPosition = 'CenterScreen' $form.FormBorderStyle = 'FixedDialog' $form.MaximizeBox = $false
$label = New-Object System.Windows.Forms.Label $label.Location = New-Object System.Drawing.Point(15,15) $label.Size = New-Object System.Drawing.Size(300,20) $label.Text = 'Enter your details below:' $form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox $textBox.Location = New-Object System.Drawing.Point(15,40) $textBox.Size = New-Object System.Drawing.Size(300,20) $form.Controls.Add($textBox)
$okButton = New-Object System.Windows.Forms.Button $okButton.Location = New-Object System.Drawing.Point(155,80) $okButton.Size = New-Object System.Drawing.Size(75,25) $okButton.Text = 'OK' $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $okButton $form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button $cancelButton.Location = New-Object System.Drawing.Point(240,80) $cancelButton.Size = New-Object System.Drawing.Size(75,25) $cancelButton.Text = 'Cancel' $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $cancelButton $form.Controls.Add($cancelButton)
# --- Show Form & Output --- $result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) { [System.Windows.Forms.MessageBox]::Show("You entered: " + $textBox.Text, "Result") } |
If there are more multiline comments in the PowerShell code enclosed between <# #>, it might break the code. An alternative for that is to replace the whole block <# #> in the code above with the following one (make sure you leave 1 blank line after the code, as powershell skips the first 2 lines (which run in batch when you open the bat file).
@setlocal EnableDelayedExpansion & set "batchFile=%~f0" & powershell -NoProfile -ExecutionPolicy Bypass -Command "Invoke-Expression ((Get-Content -Path $env:batchFile | Select-Object -Skip 2) -join [Environment]::NewLine)" & exit /b
Add-Type -AssemblyName System.Windows.Forms ... |
Option 2 - run .ps1 script through .bat
bat to run the ps1 script below:
| @powershell.exe -NoProfile -ExecutionPolicy Bypass -File "ps custom inputbox.ps1" |
ps1 script:
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing
# Create Form $form = New-Object System.Windows.Forms.Form $form.Text = 'Input Requested' $form.Size = New-Object System.Drawing.Size(350,170) $form.StartPosition = 'CenterScreen' $form.FormBorderStyle = 'FixedDialog' $form.MaximizeBox = $false
# Label $label = New-Object System.Windows.Forms.Label $label.Location = New-Object System.Drawing.Point(15,15) $label.Size = New-Object System.Drawing.Size(300,20) $label.Text = 'Enter your details below:' $form.Controls.Add($label)
# Text Box $textBox = New-Object System.Windows.Forms.TextBox $textBox.Location = New-Object System.Drawing.Point(15,40) $textBox.Size = New-Object System.Drawing.Size(300,20) # Un-comment the line below if you want a password field: # $textBox.UseSystemPasswordChar = $true $form.Controls.Add($textBox)
# OK Button $okButton = New-Object System.Windows.Forms.Button $okButton.Location = New-Object System.Drawing.Point(155,80) $okButton.Size = New-Object System.Drawing.Size(75,25) $okButton.Text = 'OK' $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $okButton $form.Controls.Add($okButton)
# Cancel Button $cancelButton = New-Object System.Windows.Forms.Button $cancelButton.Location = New-Object System.Drawing.Point(240,80) $cancelButton.Size = New-Object System.Drawing.Size(75,25) $cancelButton.Text = 'Cancel' $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $cancelButton $form.Controls.Add($cancelButton)
# Show Dialog $result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) { Write-Host "User entered: $($textBox.Text)" } else { Write-Host "User cancelled." } |
Source: Gemini
_______________________________________

|
|
| pus acum 3 saptamani |
|