Remotely Install Software Using WMI And Powershell

I am newbie in powershell…

i want to install software over the my network. I got this script from this site in this script i have some issues that was as follow

  1. script can create a folder on remote system but child-items of that source folder can’t be copied to remote system.

  2. This script is created for install MSI packages i want to install EXE packages.

is it possible to do some changes in this script or i am doing something wrong

$colComputer = Get-Content c:\targets.txt
 
Foreach ($strComputer in $colComputer) {
 
 $service = Get-Service 'RemoteRegistry' -cn $strComputer
 
 If ($service.Status -match 'Stopped') {
 $service.Start()
 }
 
 #If the target directory doesn't exist, create it
 If (!(Test-Path \\$strComputer\c$\temp\MyInstall\)) {
 mkdir \\$strComputer\c$\temp\MyInstall\
 }
 
 #Get all the files for the setup program. This can be on a network share or your local machine.
 $files = Get-ChildItem "\\path\to\software\"
 
 Foreach ($file in $file) {
 Copy-Item $file.fullname "\\$strComputer\c$\temp\MyInstall\"
 }
 
 #Start the process on the remote machine
 $process = Invoke-WMIMethod win32_process -name create -cn $strComputer -ArgumentList 'msiexec /i "c:\temp\MyInstall\install.msi" /qb /norestart'
 
 #Get the process ID for the newly created process
 $id = $process.processid
 
 #Create a DO WHILE loop to monitor for process exit
 $a = ""
 
 do {
 $a = Get-Process -cn $strComputer | Where{$_.ID -match "$ID"}
 }
 
 While ($a -ne $null)
 
 #Delete the MyInstall directory on the remote machine
 del \\$strComputer\c$\temp\MyInstall -force -recurse
 
 #Lets leave the machine the way we found it in regards to running services
 If ($service.status -match 'Stopped') {
 $service.Stop()
 }
}

Yes, it could be done. The exe will have to be a silent install. Why not use GPO?

Hello Perez,

Thanks for the replay…

what should i need to do for install EXE instead of MSI??

What exe are you install.

  1. You will need to copy the file the remote machine to a temp file.
  2. You can even create a batch file that has the commands to do a silent install.
  3. once the install has been down you can you WMI to query add and remove apps.
  4. Once you confirm the app has been installed, you can delete the exe files.

i want to install malwarebytes antimalware software over the lan

ok i will create batch file for the silent installation but how can i call that batch file in powershell??

is it after running that batch file powershell detect installation complete or not?

with following script detects application install or not

#Get the process ID for the newly created process
 $id = $process.processid
 
 #Create a DO WHILE loop to monitor for process exit
 $a = ""
 
 do {
 $a = Get-Process -cn $strComputer | Where{$_.ID -match "$ID"}
 }
 
 While ($a -ne $null)

but what if i call batch file?

what exactly i need to do could you please make changes in script please

silent switches as follow

“mbam-setup-2.2.0.1024.exe” /VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-

copy the file to the remote machine using copy -item and the below script will start the install. you will have to change the path. You can also log the installation

(Start-Process -FilePath C:\Software\mbam-setup-2.2.0.1024.exe -ArgumentList '/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-' -Wait -Passthru).ExitCode

Here is another one with the log created:

(Start-Process -FilePath C:\Software\mbam-setup-2.2.0.1024.exe -ArgumentList '/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP- /log=c:\Software\mbamlog.txt' -Wait -Passthru).ExitCode

From the log:

2016-03-10 12:10:00.727 Creating the icon.
2016-03-10 12:10:00.732 Successfully created the icon.
2016-03-10 12:10:00.733 – Icon entry –
2016-03-10 12:10:00.733 Dest filename: C:\Users\Public\Desktop\Malwarebytes Anti-Malware.lnk
2016-03-10 12:10:00.734 Creating the icon.
2016-03-10 12:10:00.739 Successfully created the icon.
2016-03-10 12:10:00.781 Installation process succeeded. <----
2016-03-10 12:10:01.243 Need to restart Windows? No
2016-03-10 12:10:01.601 Deinitializing Setup.
2016-03-10 12:10:01.628 Log closed.

ok as you know i am newbie in PS

could you please tell where i have to past this in my script??

Do you still need help?

i done some changes to install Lync .EXE format that was as follow…

$colComputer = Read-Host "Enter Computer Name To Install X86 App"
 
Foreach ($strComputer in $colComputer) {
 
 $service = Get-Service 'RemoteRegistry' -cn $strComputer
 
 If ($service.Status -match 'Stopped') {
 $service.Start()
 }

 If (!(Test-Path \\$strComputer\c$\temp\MyInstall\)) {
 mkdir \\$strComputer\c$\temp\MyInstall\
 }

 $file = Get-ChildItem "\\computer02\c$\SoftwareToBeShare\LYNC32"


 Foreach ($file in $file) {
 Copy-Item $file.fullname "\\$strComputer\c$\temp\MyInstall\LYNC32" -Recurse -Force -Verbose
 }

 $process = Invoke-Command -ComputerName $strComputer -ScriptBlock { Start-Process -FilePath C:\temp\MyInstall\LYNC32\setup.exe -Wait -Passthru } 
 
 del \\$strComputer\c$\temp\MyInstall -force -recurse -Verbose
  
 }  

With above script i can install x86 App to computer…!
But i dont know how can i get whether setup is completed or not this script
this script didn’t tell about setup is comleted or not… :frowning: could you please help me regarding this…PLEASEEEEEEEEE