I am seeking help on how to call cmd with specific parameters from a powershell script. So far what I have written is below, but it gives me an error message saying $_cmd is not recognized.
I am trying to pass the from date and to date to a an exe... The from date as you can see needs to be today - 1 and the to date should be now. The path of the executable is D:\DataService and that's why I am setting the path early in the script.
Write-Host "Get data from service"
$path ="D:\DataService"
Push-Location $path
$Date = Get-Date
$DateFrom = $Date.ToString("yyyy-MM-dd HH:mm:ss")
$DateTo = $Date.AddDays(-1).ToString("yyyy-MM-dd")
$_cmd = "ReportGen.exe -ReportType Data -DateFrom $DateFrom $DateTo"
%$_cmd%
Any suggestions please?
Don't make a command string. Simply use the call operator (&):
Write-Host 'Get data from service'
$path = 'D:\DataService'
Push-Location $path
$Date = Get-Date
$DateFrom = $Date.ToString('yyyy-MM-dd HH:mm:ss')
$DateTo = $Date.AddDays(-1).ToString('yyyy-MM-dd')
& ReportGen.exe -ReportType Data -DateFrom $DateFrom $DateTo
%$_cmd% looks like a mixture of PowerShell and cmd syntax. The %'s have no special significance. PowerShell interprets that as the literal name of a command, which of course is not recognized. To execute the contents of the string, use
Invoke-Expression $_cmd
However, that will only work if ReportGen.exe is in the path, and cmd doesn't even get involved, because you're not calling it anywhere. If for some reason, as you say, you specifically want to execute that command with cmd, you should add cmd /c or cmd /k at the beginning. However, you don't even need to assign to a string, you can just call cmd directly:
cmd /c ReportGen.exe -ReportType Data -DateFrom $DateFrom $DateTo
/c means that cmd will exit after executing the command. /k means the cmd prompt will remain open after the command is executed. You probably want /c so that you'll return to the PowerShell prompt after executing the script.
You want to use Invoke-Expression as per the comments below.
Invoke-Expression $_cmd
Original comment, which is wrong:
Did you try putting an ampersand in front. e.g.:
& $_cmd
Not sure why you are using the % characters.
A couple things. One, stop using Write-Host. Honestly, let this be the final time you ever use it (unless you truly need it - and know why you need it). Instead, use Write-Output - even when you know your script is going to run in the console. Second, you could also consider using the Start-Process cmdlet and its parameter -ArgumentList.
Your $DateFrom and $DateTo seem to be backwards, since you are making $DateTo set to yesterday. So unless you are going from today to yesterday you may want to adjust that. Also, you specify the from date, but just tack the to date on the end, not sure if you need to specify what it is, or if both dates are a part of the same parameter. As for running a command with arguments, use Invoke-Command with arguments comma delimited as such:
Invoke-Command -FilePath "D:\DataService\ReportGen.exe" -ArgumentList '-ReportType Data','-DateFrom $DateFrom','$DateTo'
I'd do it something like this.
$CustomProcess = New-Object System.Diagnostics.ProcessStartInfo
$CustomProcess.FileName = "ReportGen.exe"
$CustomProcess.arguments = "-ReportType Data -DateFrom $DateFrom $DateTo"
[System.Diagnostics.Process]::Start($CustomProcess)
Related
I wrote a Powershell script that uses Steam's command line tool to login and check for updates for a community server I am running. See below:
$steamcmdFolder = 'C:\download\steam'
$steamcmdExec = $steamcmdFolder+"\steamcmd.exe"
$forceinstall = 'force_install_dir'+$steamcmdFolder
$appupdate = 'app_update 258550'
$cmdOutput = "$steamcmdExec +login anonymous"
do {
Write-Host Checking for an update....
Invoke-Expression $cmdOutput
Invoke-expression $forceinstall
Invoke-expression $appupdate
}
while ($Update = 1)
The Invoke-Expression lines are individual command-line statements I want executed in the order I have them. For some reason, the first Invoke-Expression works fine but the others do not -- everything just stops. I can type in the value of $forceinstall on the PowerShell command-line and it works as expected. But why can't I do this using PowerShell? Any suggestions are welcome!
If you convert the other two lines down to what they are, it seems like they are not real commands.
#Invoke-expression $forceinstall
Invoke-Expression "force_install_dirC:\download\steam"
#Invoke-expression $appupdate
Invoke-Expression "app_update 258550"
Looking into the SteamCMD documents, it appears that you might want to change it to be a single line command.
Invoke-Expression "steamcmd +login anonymous +force_install_dir C:\download\steam +app_update 258550 +quit"
I want to use PowerShell to connect to a PuTTY "saved session" and then specify a file that contains some batch commands. Using CMD this would look like
d:\putty\psftp 'Saved Session Name' -b d:\location.txt.
I think that the PS equivalent should look like
Start-Process d:\putty\psftp.exe 'Saved Session Name'
(and then a call to pass a 'get' script) i.e. cd Outgoing get <date>.txt
However, I get the following error:
a positional parameter cannot be found that accepts the argument
How can I accomplish this using PowerShell?
All you need is plink:
plink 'Saved Session Name'
You don't necessarily need Start-Process.
What happens when you try to run d:\putty\psftp.exe 'Saved Session Name' -b d:\location.txt from Powershell? The first thing I do is try it exactly like I'd run it from the command line.
The biggest catch is if you have spaces in path names. You might need to use quotes and the call operator (ampersand): &"d:\putty\psftp.exe" 'Saved Session Name' -b "d:\location.txt".
If you do need to use Start-Process, you can do this:
Start-Process -FilePath "d:\putty\psftp.exe" `
-ArgumentList "'Saved Session Name' -b d:\location.txt" -Wait
Or like this:
Start-Process -FilePath "d:\putty\psftp.exe" `
-ArgumentList 'Saved Session Name', '-b', "d:\location.txt" -Wait
Note that the argument list in the first is a single string with every argument in it, and in the second it is an array of strings with one string for every argument. Everything needs to be in the same order that they'd be on the command line, and it's not uncommon for it to be a bit flaky. Usually one method or the other works better, but it depends on the application you're calling. Usually with quotes and spaces in path names because you're going through multiple levels of escaping depending on the program you're calling (noticing a theme?).
I added the -Wait parameter to my code above because, by default, Start-Process continues to the next line without waiting since it actually spawns a separate process. -Wait forces Powershell to, well, wait, which is what people usually want in a non-interactive script.
See Get-Help about_Operators or Get-Help "call operator" for more topics for help with the call operator. See Get-Help Start-Process for help with that.
Adding the below Technet Wiki link which contains various ways to run executables in PowerShell.
PowerShell: Running Executables
Try this:
$Path = "d:\putty\psftp.exe"
$Prm1 = 'Saved Session Name'
$Prm2 = "-b"
$Prm3 = "d:\location.txt"
&$Path $Prm1 $Prm2 $Prm3
I have a problem running a powershell script from within another powershell script, passing parameters and capturing the output. I have tried using the ampersand operator, calling it via powershell.exe -Command but nothing seems to work.
What seems to work is using fixed parameter and values stored in a variable like this C:\path\to\script.ps1 -arg1 $value.
This may present a solution if nothing else works, but I would like to run the command similar to this & $pathToScript $params 2>&1 (the 2>&1is for capturing error output as well as standard).
Sometimes the construct prints just the path to the script,
sometimes it says Cannot run file in the middle of pipeline and sometimes it complains about it cannot find the mentioned script file (I sometimes have spaces in my path, but I thought quoting it would suffice: quoting was done like this $path = "`"C:\path\with space\to\script.ps1`"").
This is the simplified function I want to use this in:
Function captureScriptOutput
{
#the function receives the script path and parameters
param($path, $params)
#this works if no params are passed, but I need params!
$output = & $path $params 2>&1 | Out-String
}
I solved the problem with the help of a colleague.
We went a little indirection and included a cd into the respective directory and ran the command afterwards. This works like a charm.
Solution source code:
Function captureScriptOutput
{
param($fileName, $params)
cd "C:\into\path with\space"
$output = & .\$fileName $params 2>&1 | Out-String
}
This works and even captures the error output, I hope some other folks encountering this kind of problem can use this to fix their problems.
Cheerioh and thanks for reply!
Try with invoke-expression but you need test how many quote needed
Invoke-expression "$path $param"
My PowerShell script should start an external executable with specified parameters. I have two strings: The file name, and the arguments. This is what process starting APIs usually want from me. PowerShell however fails at it.
I need to keep the executable and arguments in a separate strings because these are configured elsewhere in my script. This question is just about using these strings to start the process. Also, my script needs to put a common base path in front of the executable.
This is the code:
$execFile = "SomeSetup.exe"
$params = "/norestart /verysilent"
& "$basePath\$execFile" $params | Out-Host
# Pipe to the console to wait for it to finish
This is the actual result (does not work with this program):
Process file name: "C:\My\Base path\SomeSetup.exe"
Process command line: "/norestart /verysilent"
This is what I'd expect to have (this would work):
Process file name: "C:\My\Base path\SomeSetup.exe"
Process command line: /norestart /verysilent
The problem is that the setup recognises the extra quotes and interprets the two arguments as one - and doesn't understand it.
I've seen Start-Process but it seems to require each parameter in a string[] which I don't have. Splitting these arguments seems like a complicated shell task, not something I'd do (reliably).
What could I do now? Should I use something like
& cmd /c "$execFile $params"
But what if $execFile contains spaces which can well happen and usually causes much more headache before you find it.
You can put your parameters in an array:
$params = "/norestart", "/verysilent"
& $basepath\$execFile $params
When you run a legacy command from Powershell it has to convert the powershell variables into a single string that is the legacy command line.
The program name is always enclosed in quotes.
Any parameters that contain a space character are enclosed in double
quotes (this is of course the source of your problem)
Each element of an array forms a separate argument.
So given:
$params = "/norestart /verysilent"
& "$basePath\$execFile" $params
Powershell will run the command:
"\somepath\SomeSetup.exe" "/norestart /verysilent"
The solution is to store separate arguments in an array:
$params = "/norestart","/verysilent"
& "$basePath\$execFile" $params
will run:
"\somepath\SomeSetup.exe" /norestart /verysilent
Or if you already have a single string:
$params = "/norestart /verysilent"
& "$basePath\$execFile" ($params -split ' ')
will work as well.
$execFile = "SomeSetup.exe"
$params = "/norestart /verysilent"
Invoke-Expression ($basePath + "\" + $execFile + " " +$params)
Try it this way:
& $execFile /norestart /verysilent
Bill
Just use single quotes:
$execFile = "SomeSetup.exe"
$params = "/norestart /verysilent"
& "'$basePath\$execFile' $params" | Out-Host
# Pipe to the console to wait for it to finish
Also I would use join-path instead of concatenating the two strings:
$path = Join-Path $basePath $execFile
& "$path $params" | out-host
I'm new to these awesome Power shell world. I have a problem with script and really apreciate your help.
I have a script "cmd4.ps1" that needs to run another script "Transfer.ps1" that needs to receive 3 strings params and it needs to be run as other process thead different to "cmd4.ps1".
cmd4.ps1:
$Script="-File """+$LocalDir+"\Remote\Transfer.ps1"" http://"+$ServerIP+"/Upload/"+$FileName+" "+$ServerIP+" "+$LocalIP
Start-Process powershell.exe -ArgumentList $Script
After ejecution, the $Script cointain a value similar to
-File "c:\temp re\Remote\Transfer.ps1" http://10.1.1.1/Upload/file.txt 10.1.1.1 10.1.1.10
containing the syntax to use -File parameter to run a script of Powershell.exe, and three parameters that Transfer.ps1 needs ("http://10.1.1.1/Upload/file.txt", 10.1.1.1, 10.1.1.10).
When I write these instructions in PowerShell ISE I can see every values are right and PowerShell.exe is executed with right values, everything work fine!, but if I put these instructions inside "cmd4.ps1" script it doesn't work, I mean something is not right with parameters because I can see it start powershell but it never ends.
-ArgumentList is expecting an array of string arguments instead of a single string. Try this instead:
$ScriptArgs = #(
'-File'
"$LocalDir\Remote\Transfer.ps1"
"http://$ServerIP/Upload/$FileName $ServerIP $LocalIP"
)
Start-Process powershell.exe -ArgumentList $ScriptArgs
Note that you can simplify the string construction of the args as shown above.
Why don't you put this in cmd4.ps1?
& "c:\temp re\Remote\Transfer.ps1" "http://10.1.1.1/Upload/file.txt" "10.1.1.1" "10.1.1.10"