Link to home
Start Free TrialLog in
Avatar of NicksonKoh
NicksonKohFlag for Singapore

asked on

How do execute powershell from dos command

Hi Experts,

I've the below powershell script, Print-File.ps1, which I want to execute from dos command as below. It is returning error and I am not able to figure the correct syntax.  Can someone advice? Thx.

powershell.exe -command "& 'E:\Batch Jobs\PowerShell Scripts\Print-File.ps1 -file CIS.pdf' "

function print-file($file) 
{ 
 begin  { 
    function internal-printfile($thefile) 
    { 
        if ($thefile -is [string]) {$filename = $thefile } 
        else { 
                if ($thefile.FullName -is [string] ) { $filename = $THEfile.FullName } 
             } 
        $start = new-object System.Diagnostics.ProcessStartInfo $filename 
        $start.Verb = "print" 
        [System.Diagnostics.Process]::Start($start) 
    } 

if ($file -ne $null) { 
                $filespecified = $true; 
                internal-printfile $file 
            } 
       } 
process{if (!$filespecified) { write-Host process ; internal-printfile $_ } } 

}

Open in new window

SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NicksonKoh

ASKER

Thx Qlemo. It works.

Can explain what does the dot in
       -command ". 'E:\Batch Jobs
means?

Also, how can the powershell script be improved to accept parameters?
Is it add
param([string]$f)
print-file $f
function print-file($file) 
...

Open in new window

Using a dot in front of a script is called "dot-sourcing", If you just call a script without the dot, the script runs, and all variables and function definitions etc. are dismissed after that (to keep the results "local"). With dot, changes are "exported" to the environment of the calling script.
So, just calling the script would not allow to use the function defined in that script outside of that script.

If you want to use it that way, with a script parameter, it is better to remove the function definition. That is, you replace the script with
param($file) 
{ 
 begin  { 
    function internal-printfile($thefile) 
    { 
        if ($thefile -is [string]) {$filename = $thefile } 
        else { 
                if ($thefile.FullName -is [string] ) { $filename = $THEfile.FullName } 
             } 
        $start = new-object System.Diagnostics.ProcessStartInfo $filename 
        $start.Verb = "print" 
        [System.Diagnostics.Process]::Start($start) 
    } 

if ($file -ne $null) { 
                $filespecified = $true; 
                internal-printfile $file 
            } 
       } 
process{if (!$filespecified) { write-Host process ; internal-printfile $_ } } 

}

Open in new window

and name it like the function, "Print-File.ps1" (as it is already).
Then all you need is to use the script, and supply parameters. No dot-sourceing required.
powershell -command "& 'E:\Batch Jobs\PowerShell Scripts\Print-File.ps1' -file CIS.pdf "

Open in new window

However, you should do that only if do not need to call the function repeatedly, as the script gets compiled each time you call it, while a function is compiled only once and reused.
And, as you can see, we now need to use the ampersand operator, as the script path contains spaces. Without ampersand, the string describing the path would be exactly that - a string.
Thx Qlemo for explaining.

I tried as explained but when the script does not seem to be doing anything except re-printing the entire code?? I execute the script and it just return :

"param {.... }"
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thx Qlemo for the help.