Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag for United States of America

asked on

write a PS script to run exe remotely.

I need to write a PS script to run an installation executable/msi again 20 remote domain servers.  

Can someone advise?

Thanks.
Avatar of nav2567
nav2567
Flag of United States of America image

ASKER

I am checking this link http://gallery.technet.microsoft.com/scriptcenter/56962f03-0243-4c83-8cdd-88c37898ccc4

Do I just copy the whole function and save it to a .PS1 and run it?
You could copy the aforementioned function and run it for each serve. Since the function accepts the computer names in the pipeline you could do this:

"comp1", "comp2", "comp3" |  Run-RemoteCMD
You'll need to put the msi on a shared resource that will be assessable to all the computers. The first time you call the function it will prompt you for the command. You'd type in the fully qualified name of the msi file.
Avatar of nav2567

ASKER

Steve,

Can you explain again?  I save this function to a remote.ps1 file.  I have another servers.txt file with a list of server.  

How do I use them together?  I tried to just run the remote.ps1 but nothing happens.
Avatar of becraig
why not use psexec which would work easier :

 Use your list of servers:

for /f %A in (serverlist.txt) do copy <msifile.msi> \\%A\c$\Temp  & psexec \\%A c:\temp\msifile.msi 

Open in new window


You can add flags as needed.,
Here's how you could do it in a ps1 file name Install.ps1 using a file name server.txt that resides in the current directory. You could fully qualify the file name if you'd like:

# ============================================================================================== 
#   
# Script Name : Run Remote cmd.exe Commands 
#  
# AUTHOR: Mohamed Garrana  
# DATE  : 4/12/2010 
#  
# COMMENT:  
# this script invokes whatever command you can use in cmd.exe on one or more computers 
#you input the command name as a screen input when you run the script 
#you can use all cmd.exe command like [del,ipconfig /flushdns,ipconfig /registerdns,gpupdate /force ,notepad.exe,defrag c:, ... 
#..wuauclt /detectnow,powercfg,net start ,net stop,copy,arp,wscript.exe ....] 
#if you can do it from cmd.exe you can do it here on multiple computers at the same time 
# ============================================================================================== 
 
 
function Run-RemoteCMD { 
 
    param( 
    [Parameter(Mandatory=$true,valuefrompipeline=$true)] 
    [string]$compname) 
    begin { 
        $command = Read-Host " Enter command to run" 
        [string]$cmd = "CMD.EXE /C " +$command 
                        } 
    process { 
        $newproc = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ($cmd) -ComputerName $compname 
        if ($newproc.ReturnValue -eq 0 ) 
                { Write-Output " Command $($command) invoked Sucessfully on $($compname)" } 
                # if command is sucessfully invoked it doesn't mean that it did what its supposed to do 
                #it means that the command only sucessfully ran on the cmd.exe of the server 
                #syntax errors can occur due to user input  
     
     
     
     
    } 
    End{Write-Output "Script ...END"} 
                 } 
     
 
#---------------- 
#you can use this script to run any command that can be run on CMD.EXE 
#the following is only to give you an idea how can you use it 
#----------------- 
#for copying files from many remote computers to a single 
# get-content c:\servers.txt | Run-Remotecommand 
#Enter command to run: copy c:\log\log.txt d:\ 
#you only input "copy c:\log\log.txt d:\" 
#--------------------------------------- 
#for forcing group policy update on multiple computers 
# get-content c:\servers.txt | Run-Remotecommand 
#Enter command to run: gpupdate /force 
#-------------------------------------- 
#for stopping the Bits service on multiple computers 
# get-content c:\servers.txt | Run-Remotecommand 
#Enter command to run: Net stop bits 
#--------- 
#you can always run it against a single server using  
#Run-RemoteCommand server1 
#Enter command to run: enter whatever you'd normally enter in cmd.exe shell 

# Here's the code you'd write to perform the installations:
Get-Content "servers.txt" | Run-RemoteCMD 

Open in new window

steveGTR

invoke command require Ps remoting enabled...........by default only 2012 server is enabled

if PS remoting is already turned on.

it will work this way.

example:
$Servers = get-content c:\servers.txt 
foreach ($Server in $Servers)
{
Invoke-command -computerName $Server {your script or any installation msi that the computer can get}
}

Open in new window

Avatar of nav2567

ASKER

Justin, you script does not work.

Steve, your script works but it does not work if I put more than one servers in servers.txt
I know you wanna try using a powershell solution but for my money I usually find psexec works much better in this instance:

for /f %A in (serverlist.txt) do copy <msifile.msi> \\%A\c$\Temp  & psexec \\%A  cmd /c c:\temp\msifile.msi  <msi flags>

Open in new window

what is the error of my script?

and do you have PS remoting turned on?
becraig - I believe the Author is asking for POWERSHELL
ahh in that case :


gc serverlist.txt | % {
copy c:\temp\msifile.msi \\$_\c$\temp
invoke-command -computername $_ -scriptblock { cmd /c c:\temp\msifile.msi}
}

Open in new window

What is displayed when you do the following:

Get-Content "servers.txt"

Of course the "servers.txt" will be the fully qualified path to the servers.

When you say it doesn't work if you put in more than one server. What does that mean exactly? Does it generate an error? You do realized that it will only prompt you once for the command.

What is the output?
Avatar of nav2567

ASKER

thanks.

what is the reason of adding copy c:\temp\msifile.msi \\$_\c$\temp in the script?

Can I just use this?

gc c:\serverlist.txt | % {
invoke-command -computername $_ -scriptblock { cmd /c msiexec /I c:\temp\msifile.msi /qn
}

this is a quiet installation.
you can do just run the msi in a shared location within the script block

{ \\server\shared\msi.msi /qn}

{c:\windows\system32\msiexec.exe /i \\server\shared\msi.msi /qn}
Avatar of nav2567

ASKER

So,  is the below the final script?

gc c:\serverlist.txt | % {
invoke-command -computername $_ -scriptblock { cmd /c c:\windows\system32\msiexec.exe /i \\server\shared\msi.msi /qn
}
$Servers = Get-Content "c:\serverlist.txt" 
foreach ($Server in $Servers)
{
Invoke-Command -ComputerName $Server {c:\windows\system32\msiexec.exe /i "\\server\shared\msi.msi" /qn}
}

Open in new window


i don't know why you need cmd it............. powershell takes docs command as well.

invoke command means to run the {the script} on the remote machine....

you basically the same as running that command in the machine powershell console
I only added the copy command in the event the msi might have some restrictions against install from a remote path (I have seen that a lot).

Otherwise what you have and what Justin have (which are pretty much the same)
should work with no issue.
Since I have received no response to my questions, I'll be leaving the thread. Good luck :)
Avatar of nav2567

ASKER

failed with the following message:

[servera] Connecting to remote server failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If
the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken
PS remoting is not turned on on your servers.
This was why I was suggesting a psexec solution, in the even psremoting was not enabled on the computers you wanted to run this on.


You will need to enable winrm on the client computers, how many computers are we looking at  ?

Do you have control over setting group policy ?
Avatar of nav2567

ASKER

how do I turn that on?
you can run enable-psremoting -force on the computer you need to enable it on, but there are a lot of considerations.

That is why I asked the two questions:
How many computers
Can you define GPO
Avatar of nav2567

ASKER

I have a lot of computers.  

I do not have any luck in getting it to work on PS even enable-psremoting -force  is run on the target server.

I am looking at the PSExec command.  I have tried the following but it is not working.   Becraig, can you help again?

psexec \\server1 -i msiexec.exe /i "\\server2\share\wintech-windows-2.1.1-0.x64.msi" /quiet /norestart
psexec -u domain\user -p password \\server1 cmd /c msiexec.exe /i "\\server2\share\wintech-windows-2.1.1-0.x64.msi" /quiet /norestart


The above should work
Avatar of nav2567

ASKER

I am getting this message.  Do you know how to fix it?

T h e   s y s t e m   a d m i n i s t r a t o r   h a s   s e t   p o l i c i e
s   t o   p r e v e n t   t h i s   i n s t a l l a t i o n .
 cmd exited on server1 with error code 1625.
The installation requires elevated mode
Or do you have any gpo prevent installation
The MSI you want to install is not natively allowed:

My suspicion is:
 User Account Control: Behavior of the elevation prompt for administrators in Admin Approval Mode      
ConsentPromptBehaviorAdmin      
Prompt for consent for non-Windows binaries


If UAC is enabled on the machines you are trying to install on, then this would be your issue you can probably work around it via Group Policy

See the link below for more details:
http://technet.microsoft.com/en-us/library/dd835564%28v=ws.10%29.aspx
Avatar of nav2567

ASKER

I do not have any GPO that prevents the installation.  

I have disabled UAC on the target server but I am still getting the 1625 error.  

I will do some research myself.  If you have other idea to try, please advise.

Thanks, everyone!
Did you disable that "Specific value" ?

Check:
 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System

EnableInstallerDetection


set to
0 = Disabled (default for enterprise)

Reboot before retrying the msi install.
Avatar of nav2567

ASKER

Nevermind.  I have finally got it to work.

One last question, I need to create a script to read a server.txt file that contains list of servers and run the install, and start the service.  I tried the following but it does not work.  Can someone please correct it?

for /f %%i in (servers.txt) do
{
psexec -u domain\user -p password \\%%i cmd /c msiexec.exe /i "\\server2\share\wintech-windows-2.1.1-0.x64.msi" /quiet /norestart
sc \\%%i start wintech
}
What is the specific problem you are having when you run it  ?
If you save as .bat or .cmd file:
for /f %%i in (servers.txt) do psexec -u domain\user -p password \\%%i cmd /c msiexec.exe /i "\\server2\share\wintech-windows-2.1.1-0.x64.msi" /quiet /norestart & sc \\%%i start wintech


If running directly from the command line:
for /f %i in (servers.txt) do psexec -u domain\user -p password \\%i cmd /c msiexec.exe /i "\\server2\share\wintech-windows-2.1.1-0.x64.msi" /quiet /norestart & sc \\%i start wintech

No curly braces :)
Avatar of nav2567

ASKER

So far, the following works - Thanks becraig.

for /f %%i in (servers.txt) do psexec -u domain\user -p password \\%%i cmd /c msiexec.exe /i "\\server2\share\wintech-windows-2.1.1-0.x64.msi" /quiet /norestart & sc \\%%i start wintech

for /f %%i in (servers.txt) do sc \\%%i start wtech_agent

There are some server which shows errors during installation.  Can you help me make the bat better so it display which server(s) has issue?

Thanks again.
Something like this should work, my batch is really rusty PS wiped all that knowledge away :)

for /f %%i in (servers.txt) do (
psexec -u domain\user -p password \\%%i cmd /c msiexec.exe /i "\\server2\share\wintech-windows-2.1.1-0.x64.msi" /quiet /norestart 
IF '%ERRORLEVEL%'=='0' (
  echo installed on %%1 
) else (
  echo %%i failed !!
)
sc \\%%i start wintech
)

Open in new window

Avatar of nav2567

ASKER

Thanks, becraig.

Can you make you the code to display only the following:

server1 install/server1 failed
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America 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 nav2567

ASKER

Thanks, everyone.