How to call a command line with parameter in powershell

Hi Experts,

I have the below code where I am trying to execute a exe with parameters in powershell on multiple computers to change a setting.


I am trying to get the return code of the executable, based on which I write result to the console.
when executed the console shows that the task is done successfully, however when I actually check the server the change is not affected.

Please help me in getting the script work and let me know what is that I am doing wrong.








$FullPath = $MyInvocation.MyCommand.Path
$Scriptpath = Split-Path -Path $FullPath -Parent
cd $Scriptpath

$ServerList = '.\serverlist.txt'

[string[]]$Computers = Get-Content "$ServerList"

	foreach($Computer in $Computers) 
{

write-host "Doing the repoint in $Computer"
caf.exe setserveraddress mphewicas112"

$a =  $LASTEXITCODE
write-host "$a"

                   if ($a -eq 0)
                      {
                        write-host " Repoint in $Computer is done successfully" -ForegroundColor Green
                      }
                                    else
                      {
                        write-host " Repoint in $Computer is not done successfully" -ForegroundColor Green
                      }


}

Open in new window

karkou12Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
I have no idea what line 13 is supposed to do, but I see nowhere which has a command to act on a remote machine.  The code above would just run the same line repeatedly, depending on how many lines are in the file 'serverlist.txt'.  So it looks like caf.exe would just run on the local machine a number of times.
karkou12Author Commented:
caf.exe is the executable which will set the server name mentioned.

caf.exe setserveraddress mphewicas112

the above command line should be on remote servers mentioned in the serverlist.txt
footechCommented:
Does the caf.exe program have the ability to specify which computer it is affecting?  If not, then you need to run a command which will work on remote computers.  Examples are psexec (Sysinternals) or Invoke-Command (PowerShell).  To call caf.exe on the remote machine, it has to be on the remote machine (unless you're calling it from a UNC path, which can bring its own issues).  To use Invoke-Command you need to have PS Remoting configured on all the target machines.  You can use this as an opportunity to research PS Remoting, or if you're after a quicker solution then go for psexec.  Something like:
psexec @serverlist.txt -c caf.exe setserveraddress mphewicas112

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

karkou12Author Commented:
when we run the command "caf.exe setserveraddress mphewicas112" on a remote server, that server will be registered to mphewicas112.

I want to do this on multiple computers.. computers names, I will mention in serverlist.txt.

I have a question on the command you posted... how does the powershell script loop into the serverlist.txt and run the command line.
footechCommented:
The command I posted is not PowerShell, it is using the Sysinternals PsExec utility.  You can download it from Microsoft.  It will try to run the command on each machine in the serverlist.txt file.  Run psexec /? to get help on the syntax.
karkou12Author Commented:
Thank you footech.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.