NicksonKoh
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' "
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 $_ } }
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Then all you need is to use the script, and supply parameters. No dot-sourceing required.
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.
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 $_ } }
}
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 "
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.
ASKER
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 {.... }"
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thx Qlemo for the help.
ASKER
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
Open in new window