0

I am new to PowerShell scripting. I have been given the task to understand what a PowerShell script is doing.

I am stuck at following line:

& $basePath\CPAU.exe -u $($sqlUser.UserName) -p $(ConvertTo-UnsecureString $sqlUser.Password) -ex "cmd /c (sqlcmd $($sqlArgs -join ' ') > $outputFile && echo TRUE > SUCCESS) || echo TRUE > FAILURE" -wait -nowarn -hide

It feels like the script is trying to execute some sort of sql command. However, I am not sure of starting "&" symbol.

Also, could CPAU.exe be an external in basepath? And what do -p, -u, and -ex enforce?

Next line:

if (!(Test-Path .\SUCCESS))
{
    #some more code
}

In above if statement, what does .\SUCCESS mean? Doesn't Test-Path operate on some sort of path to test whether it exists or not?

8
  • -u username, -p password and -ex is probably execute? Commented Nov 13, 2014 at 16:12
  • Thanks, could you write an answer explaining whole line? I still don't get what is "||" for and TRUE>FAILURE? Commented Nov 13, 2014 at 16:13
  • Kindly give the down vote reason please. Commented Nov 13, 2014 at 16:14
  • I'm not very good at powershell, just pointed out some things. The || is an or condition. Commented Nov 13, 2014 at 16:14
  • 1
    It is an or for the cmd session that is being started. Commented Nov 13, 2014 at 16:17

2 Answers 2

5

& is the PowerShell call operator, which "runs a command, script, or script block". In your case it's running the external command CPAU.exe located in $basePath with the parameters -u, -p, -ex, -wait, -nowarn, and -hide. That command runs another command (the argument of the parameter -ex):

cmd /c (sqlcmd $($sqlArgs -join ' ') > $outputFile && echo TRUE > SUCCESS) || echo TRUE > FAILURE

The above uses cmd.exe (the Windows Command Prompt) to run sqlcmd $($sqlArgs -join ' '), redirect its output to the file $outputFile, and write "TRUE" to either the file SUCCESS if the command succeeded or to the file FAILURE otherwise.

|| and && are cmd operators for command chaining. || means "run the next command if the previous command failed". && means "run the next command if the previous command succeeded".

Sign up to request clarification or add additional context in comments.

Comments

0
& $basePath\CPAU.exe -u $($sqlUser.UserName) -p $(ConvertTo-UnsecureString $sqlUser.Password) -ex "cmd /c (sqlcmd $($sqlArgs -join ' ') > $outputFile && echo TRUE > SUCCESS) || echo TRUE > FAILURE" -wait -nowarn -hide

This is some Powershell code, calling the executable CPAU.exe stored in $basePath passing a username and password and making it execute some CMD code:

cmd /c (sqlcmd $($sqlArgs -join ' ') > $outputFile && echo TRUE > SUCCESS) || echo TRUE > FAILURE

The cmd executes a SQL command given some predefined $sqlArgs, joined by a space. The whole command is redirected to $outputFile which is also expected to be predefined.

If the output of the command AND the ability to Echo TRUE on the CMD console is true, a file called SUCCESS is created receiving the redirected output. Else, if something fails but the echo TRUE happens, a FAILURE file is created.

The whole thing waits for everything to be complete, gives no warning and ...hides....

Whoever wrote this, needs a vacation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.