Executing .bat file from powershell button click event - batch-file

I was working with some powershell button that calls a .bat file. I have looked around but it only appears to work the other way around. I'm trying to figure out the syntax to call a .bat file from powershell button. This seems to work from command line, but I'm having issues with using it within my button. Any sugestions?
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Size(170, 450)
$StartButton.Size = New-Object System.Drawing.Size(125,43)
$StartButton.Font = New-Object System.Drawing.Font("Courier New", "10.5")
$StartButton.Text = "Start Tuner"
$StartButton.Add_Click(invoke-command{C:\temp\Map.bat})
$tunerForm.Controls.Add($StartButton)
Thanks anything is highly appreciated.

You can also use Start-Process and -wait
Start-Process myBatch.bat -wait
That way, your script does not do anything else while that script runs / completes.

Sorry guys.. I found the answer.. Invoke-Expression "cmd /c c:\path\to\batch\file.bat"
Sorry...

Related

cmd agent job shows success but does nothing to the file

I have a powershell script that launches an Excel macro to refresh pivot tables and data sources, it works fine when run manually or from cmd.
when run from Sql Agent job it succeeds without performing the Excel refresh.
I call the script with this command in the job step:
powershell -command "&'C:\rapport_declaratif\REFRESH_3.ps1'"
I tried calling it with powershell, with cmd, with xp_cmdshell but nothing seems to work.
I even called it differently, for exp when I call it with this command:
cmd.exe /c "C:\rapport_declaratif\REFRESH_3.ps1"
it runs forever.
I also googled and found that I have to create this folder:
C:\Windows\SysWOW64\config\systemprofile\DeskTop
I did and it didn't solve the problem.
Below is my powershell script:
# Open Excel file
$excel = new-object -comobject excel.application
$filePath = "C:\rapport_declaratif\Rapport_Declaratif.xlsm"
$workbook = $excel.Workbooks.Open($FilePath)
$excel.Visible = $true
$excel.DisplayAlerts = $false
$worksheet = $workbook.worksheets.item(1)
#Write-Host "Running macro in excel to refresh data."#
$excel.Run("Refresh_ALL")
Start-Sleep -s 30
#Write-Host "data refreshed."#
$workbook.save()
# Write-Host "saved."#
$workbook.close()
$excel.quit()
#Write-Host "Closed Excel"#

How to open the Run dialog box with a file already in the box

Crappy title, but the thing I want to do is this:
Open the windows dialog box from cmd
I already know how to do this
But I was wandering if I could open it and have a set file selected
E.g. powershell -c (New-Object -ComObject "Shell.Application").FileRun() opens the dialog box
But I want:
powershell -c (New-Object -ComObject "Shell.Application").FileRun("C:\<file_location>") opens the dialog box with "C:\" automatically inserted
This seems to work. I am by no means a powershell guy. I just Google searched.
powershell -c (New-Object -ComObject "Shell.Application").FileRun();$wshell = New-Object -ComObject wscript.shell;$wshell.SendKeys('C:\temp')

write robocopy output to console and log file

I have an interactive PowerShell using windows forms, meaning a PowerShell script displays some controls on a form asking for input and finally runs robocopy, when the user clicks the button.
This is a simple script displaying the problem.
You can copy the code, save it to a Power Sell script and run. It needs a:\tmp\tmp folder to successfully run, see the CopyFolder function.
function createdMainForm {
$mainForm = New-Object System.Windows.Forms.Form
$btn = New-Object System.Windows.Forms.Button
$btn.Text = "RoboCopy"
#register click event
$btn.add_click({Add_click})
$mainForm.Controls.Add($btn)
$mainForm.ShowDialog()
}
#click event
function Add_click() {
CopyFolder
}
function CopyFolder() {
$sourseFolder = "C:\tmp\tmp"
$targetFolder = "C:\tmp\tmp2"
$Logfile = "c:\tmp\a.log"
robocopy $sourseFolder $targetFolder /tee /log:$Logfile
}
function ReadMode() {
Write-Host Mode [1 for GUI mode]
$mode = Read-Host
if ($mode -eq 1) {
createdMainForm
} else {
CopyFolder
}
}
ReadMode
I want to capture the robocopy progress in a log file as well as in the console.
However only the log file capture the output, while the console "hangs" until end.
I found that is works smoothly when PowerShell does not display form, but simply runs the command.
The script works in two modes. enter 1 for "form mode" anything else for console mode, where the log actually written to console and file.
How can I use the form, and display the progress in the console?
Use the proper robocopy parameters:
/log:<LogFile> Writes the status output to the log file (overwrites the existing log file).
…
/tee Writes the status output to the console window, as well as to the log file.
robocopy $s $t /tee /log:C:\path\to\your.log
As for updating the output of your embedded control, you probably need to refresh your form on a timer:
$timer = New-Object Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_tick({$form.Refresh()})
Start the timer when calling robocopy and stop it after the command completes. Details will vary depending on your actual code (which you chose to omit).
Edit: I think I finally understood what you're trying to achieve and what problem you have getting there.
You're running robocopy from a scriptblock that is associated with a form element. Because of that STDOUT output doesn't go to the console. You need to take the robocopy output (the success output stream) and write it to the host console yourself:
function CopyFolder() {
...
robocopy $sourseFolder $targetFolder /tee /log:$Logfile | Write-Host
}

Powershell passing named parameters from file

i have a problem that is troubling my mind.
I want to automatically execute powershell scripts with named arguments but within another powershell script (that will act as a script deamon).
For example:
One of the scripts that get called has this parameters
param(
[int]$version,
[string]$user,
[string]$pass,
[string]$domain,
)
The powershell script deamon now loads the file and arguments like this
$argumentsFromScript = [System.IO.File]::ReadAllText("C:\params.txt") $job = Start-Job { & "ps1file" $arguments}
The params.txt contains the data like this
-versionInfo 2012 -user admin -pass admin -domain Workgrup
But when i try to execute this code obviously the whole $argumentsFromScript variable will be seen as parameter 1 (version) and i end up with an error, that "-versionInfo 2012 -user admin -pass admin -domain Workgrup" cannot be converted to Int32...
Do you guys have any idea how i can accomplish this task?
The Powershell deamon does not know anything about the parameters. He just needs to execute scripts with given named parameters. The params.txt is just an example. Any other file (csv,ps1,xml,etc) would be fine, i just want to automatically get the named parameters passed to the script.
Thank you in advance for any help or advice..
Try this:
#'
param ([string]$logname,[int]$newest)
get-eventlog -LogName $logname -Newest $newest
'# | sc c:\testfiles\testscript.ps1
'-logname:application -newest:10' | sc c:\testfiles\params.txt
$script = 'c:\testfiles\testscript.ps1'
$arguments = 'c:\testfiles\params.txt'
$sb = [scriptblock]::Create("$script $(get-content $argumentlist)")
Start-Job -ScriptBlock $sb
I guess you want this:
$ps1 = (Resolve-Path .\YourScript.ps1).ProviderPath
$parms = (Resolve-Path .\YourNamedParameters.txt).ProviderPath
$job = sajb -ScriptBlock {
param($ps1,$parms)
iex "$ps1 $parms"
} -ArgumentList #(
$ps1,
[string](gc $parms)
)
# if you wanna see the outcome
rcjb $job -Wait

Hide empty console window in a all GUI Powershell script?

I have made a very simple Powershell script with WinForms GUI.
Everything works as intended but, when I run the .ps1 script with PowerShell a black empty console window appears at first and then the GUI shows.
Anyway to make the console window dissapear?
Best regards
I wrote a small article on this subject (sorry in french) one year ago.
Here is the common solution using a small VBS script to start PowerShell hidding his window (the trick is in the last ,0).
Set Args = Wscript.Arguments
'MsgBox "Chemin LDAP: " & Args(0)
'MsgBox "Classe: " & Args(1)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file c:\SlxRH\RhModif.ps1 " & chr(34) & Args(0) & chr(34) , 0
I also embeded PowerShell in an executable with no console called slxPShell2.EXE.
I found the above didn't work for me. I used this:
Set objShell = CreateObject("WScript.Shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -file " & "YourScript.ps1", 0, False
Set objShell = Nothing
Hope that helps.
This solution Minimizes Powershell window after it starts. Powershell window opens, then disapears, without using any outside code. Put at beginning of your script.
$t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
add-type -name win -member $t -namespace native
[native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
This is how I got this working:
Have the Winforms GUI script in one ScriptOne.ps1 file
Create another LaunchScriptOne.ps1 file with the content:
powershell.exe -WindowStyle Hidden -File "C:\path\to\ScriptOne.ps1".
The solution was provided in another thread on the same topic: Hide or Minimize the powershell prompt after Winform Launch
I hope someone will find a way to put this into one single script as well. The answers above in this thread did not help me, but maybe I did something wrong, idk.
I'm nube so no rep so can't comment inline... though wrt #Ipse's solution which I'm a fan of, I also make sure to close the hidden window when the script is done... not sure if PS gets around to this sort of auto-garbage collection, but suspect it's good best practice.
eg. at end of your script I'd suggest doing:
stop-process -Id $PID
(which should terminate that hidden window v. just leave it lurking around and tying up those resources).

Resources