You are not logged in.

#1 07 Apr 2016 04:53

Hackoo
Member
Registered: 05 Feb 2015
Posts: 21

[Solved] How to write a long command in multiline with batch file ?

Hi  smile
I'm using this long command in powershell to execute it from a batch file :

@echo off
powershell -Command "& { param ($time,$title,$text,$icon)  [reflection.assembly]::loadwithpartialname('System.Windows.Forms') | Out-Null; [reflection.assembly]::loadwithpartialname('System.Drawing')| Out-Null; $notify = new-object system.windows.forms.notifyicon; $notify.icon = [System.Drawing.SystemIcons]::Information; $notify.visible = $true; $notify.showballoontip($time,$title,$text,$icon) }" 100 'Titre' 'ceci est un test' 'Info'

So my aim is how to write this command using multiline instead for one long command in one line for more readability ?
This is my approch :

@echo off
Set PSCommand="& { param ($time,$title,$text,$icon) [reflection.assembly]::loadwithpartialname('System.Windows.Forms') | Out-Null;" ^
^"[reflection.assembly]::loadwithpartialname('System.Drawing') | Out-Null;" ^
^"$notify = new-object system.windows.forms.notifyicon;" ^
^"$notify.icon = [System.Drawing.SystemIcons]::Information;" ^
^"$notify.visible = $true;" ^
^"$notify.showballoontip($time,$title,$text,$icon) }" ^ 
^"100 'Titre' 'ceci est un test' 'Info'""

echo %PSCommand%
pause

But it did not worked for me ??
Thank you for any help from you !

Last edited by Hackoo (07 Apr 2016 15:56)

Offline

#2 07 Apr 2016 06:04

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: [Solved] How to write a long command in multiline with batch file ?

This is the method I used; it is simpler and clearer, and I like the possibility of directly include the values of Batch variables and parameters in the PS code:

@echo off

call :PS_Sub 100 'Titre' "'ceci est un test'" 'Info'
pause
goto :EOF


:PS_Sub $time $title $text $icon

PowerShell  ^
   [reflection.assembly]::loadwithpartialname('System.Windows.Forms') ^| Out-Null; ^
   [reflection.assembly]::loadwithpartialname('System.Drawing') ^| Out-Null; ^
   $notify = new-object system.windows.forms.notifyicon; ^
   $notify.icon = [System.Drawing.SystemIcons]::Information; ^
   $notify.visible = $true; ^
   $notify.showballoontip(%1,%2,%3,%4)
%End PowerShell%
exit /B

Antonio

Offline

#3 07 Apr 2016 06:32

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: [Solved] How to write a long command in multiline with batch file ?

Couldn't you just build a variable over the course of multiple lines?

@echo off
set "pscmd=param ($time,$title,$text,$icon)"
set "pscmd=%pscmd% [reflection.assembly]::loadwithpartialname('System.Windows.Forms') | Out-Null;"
set "pscmd=%pscmd% [reflection.assembly]::loadwithpartialname('System.Drawing')| Out-Null;"
set "pscmd=%pscmd% $notify = new-object system.windows.forms.notifyicon;"
set "pscmd=%pscmd% $notify.icon = [System.Drawing.SystemIcons]::Information;"
set "pscmd=%pscmd% $notify.visible = $true;"
set "pscmd=%pscmd% $notify.showballoontip($time,$title,$text,$icon) "

powershell -command "&{%pscmd%}" 100 'Titre' 'ceci est un test' 'Info'

Not the most elegant, sure, but it works.

Offline

#4 07 Apr 2016 15:47

Hackoo
Member
Registered: 05 Feb 2015
Posts: 21

Re: [Solved] How to write a long command in multiline with batch file ?

Thank you both for your solutions  smile
I like the first solution from Aacini it is more clear and easy to read, this exactly what i expected, because, i like to use it as function to call it when i want   wink

So, i added some features to this script to become more attractif (Typewriter + Speaking Voice)  tongue

Thank you for your help man the problem is solved wink

@echo off
Color 0A & Mode con cols=80 lines=4
Title ShowBalloonTip with a TypeWriter and Speaking Voice by Hackoo 2016
Call :TypeWriter "This an example with a ShowBalloonTip with an Information message"
Call :Get_Ip_Public
Call :PS_Sub 'Information' 100 '%IP%' "'Your Public IP is %IP%'" 'Info'
echo( & Pause
Call :TypeWriter "This an example with a ShowBalloonTip with a Warning message"
Call :PS_Sub 'Warning' 100 '%IP%' "'Your Public IP is %IP%'" 'Warning'
echo( & Pause
Call :TypeWriter "This an example with ShowBalloonTip with a Error message"
Call :PS_Sub 'Error' 100 '%IP%' "'Your Public IP is %IP%'" 'Error'
echo( & Pause
Exit /B
::*************************************************************
:PS_Sub $notifyicon $time $title $text $icon
PowerShell  ^
  [reflection.assembly]::loadwithpartialname('System.Windows.Forms') ^| Out-Null; ^
  [reflection.assembly]::loadwithpartialname('System.Drawing') ^| Out-Null; ^
  $notify = new-object system.windows.forms.notifyicon; ^
   $notify.icon = [System.Drawing.SystemIcons]::%1; ^
   $notify.visible = $true; ^
   $notify.showballoontip(%2,%3,%4,%5)
%End PowerShell%
exit /B
::*************************************************************
:TypeWriter
Cls
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 150
echo For x = 1 to intTextLen
echo     strTempText = Mid(strText,x,1^)
echo     WScript.StdOut.Write strTempText
echo     WScript.Sleep intPause
echo Next
echo Set Voice=CreateObject("SAPI.SpVoice"^)
echo voice.speak strText
)>%tmp%\%~n0.vbs
@cscript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
Exit /B
::**************************************************************
:Get_Ip_Public
(
	echo Set http = CreateObject("MSXML2.ServerXmlHttp"^)
	echo http.Open "GET","http://icanhazip.com",False
	echo http.Send
	echo Wscript.Echo http.responseText
)>%tmp%\%~n0.vbs
for /f %%a in ('Cscript /nologo %tmp%\%~n0.vbs') do (set IP=%%a)
Exit /B
::***********************************************************************************************  

Last edited by Hackoo (07 Apr 2016 15:58)

Offline

Board footer

Powered by