running a .ps1 from a .bat file hanging

Hi all,

I have a script which I am running from within a .bat file.

.bat file contents are:

@echo off
powershell.exe -executionpolicy bypass -command "& 'c:\temp\McAfee_PL8_update_all.ps1'"

exit

for some reason the .bat file does not exit therefor in my main script the for each loop cannot continue…

contents below of: McAfee_PL8_update_all.ps1

# 01 McAfee PL8 fresh install locations for x64 machines:
$x64_agent_1 = Test-Path 'c:\Program Files\McAfee\Agent\CmdAgent.exe'
$x64_update_1 = Test-Path 'c:\Program Files (x86)\McAfee\VirusScan Enterprise\mcupdate.exe'

# 02 McAfee PL8 upgrade install locations for x64 machines:
$x64_agent_2 = Test-Path 'c:\Program Files (x86)\McAfee\Common Framework\CmdAgent.exe'
$x64_update_2 = Test-Path 'c:\Program Files (x86)\McAfee\VirusScan Enterprise\mcupdate.exe'

# 03 McAfee PL7 install locations for x86 machines:
$x86_agent_1 = Test-Path 'c:\Program Files\McAfee\Common Framework\CmdAgent.exe'
$x86_update_1 = Test-Path 'c:\Program Files\McAfee\VirusScan Enterprise\mcupdate.exe'

# 04 McAfee PL8 upgrade from PL7 locations for x86 machines:
$x86_agent_2 = Test-Path 'c:\Program Files\McAfee\agent\CmdAgent.exe'
$x86_update_2 = Test-Path 'c:\Program Files\McAfee\VirusScan Enterprise\mcupdate.exe'

# 05 not used yet
#$x86_x64_1 = Test-Path 'C:\Program Files\McAfee\Agent\CmdAgent.exe'



#01 Variables used (Fresh PL8 install for x64):
if (($x64_agent_1 -eq 'True') -and ($x64_update_1 -eq 'True'))
{
cd 'c:\Program Files (x86)\McAfee\VirusScan Enterprise'
.\mcupdate.exe /update /quiet
wait-event -Timeout 30

cd 'C:\Program Files\McAfee\Agent'
.\cmdagent.exe -p
wait-event -Timeout 15
.\cmdagent.exe -f
wait-event -Timeout 15
.\cmdagent.exe -c
wait-event -Timeout 15
.\cmdagent.exe -e

}

#02 Variables used (PL8 upgrade for x64):
Elseif (($x64_agent_2 -eq 'True') -and ($x64_update_2 -eq 'True'))
{
cd 'c:\Program Files (x86)\McAfee\VirusScan Enterprise'
.\mcupdate.exe /update /quiet
wait-event -Timeout 30

cd 'c:\Program Files (x86)\McAfee\Common Framework'
.\cmdagent.exe /p
wait-event -Timeout 15
.\cmdagent.exe /f
wait-event -Timeout 15
.\cmdagent.exe /c
wait-event -Timeout 15
.\cmdagent.exe /e

}

#03 Variables used (Fresh PL7 install for x86):
Elseif (($x86_agent_1 -eq 'True') -and ($x86_update_1 -eq 'True'))
{
cd 'c:\Program Files\McAfee\VirusScan enterprise'
.\mcupdate.exe /update /quiet
wait-event -Timeout 30

cd 'c:\Program Files\McAfee\Common Framework'
.\cmdagent.exe /p
wait-event -Timeout 15
.\cmdagent.exe /f
wait-event -Timeout 15
.\cmdagent.exe /c
wait-event -Timeout 15
.\cmdagent.exe /e

}

#04 Variables used (PL8 upgrade for x86):
Elseif (($x86_agent_2 -eq 'True') -and ($x86_update_2 -eq 'True'))
{
cd 'c:\Program Files\McAfee\VirusScan enterprise'
.\mcupdate.exe /update /quiet
wait-event -Timeout 30

cd 'c:\Program Files\McAfee\Agent'
.\cmdagent.exe -p
wait-event -Timeout 15
.\cmdagent.exe -f
wait-event -Timeout 15
.\cmdagent.exe -c
wait-event -Timeout 15
.\cmdagent.exe -e

}
break

main script that I run for the above:

# variables listed below 
$remotecomputers = Get-Content c:\temp\remotecomputers1.txt
$sourcefile1 = "C:\Temp\mcafee_update.bat"
$sourcefile2 = "C:\temp\PsExec.exe"
$sourcefile3 = "C:\Temp\McAfee_PL8_update_all.ps1"

# This section will Run the batch File
foreach ($computer in $remotecomputers) 
{
  $destination = "\\$computer\c$\Temp"

# This Section will Ping machine to ensure its online  
  if((Test-Connection -Computername $computer -BufferSize 16 -Count 1 -quiet))
    
# This will Copy files to machine and create the destination folder if it does not exist
    {if (!(Test-Path -path $destination))
       {
       New-Item $destination -Type Directory
       }

    Copy-Item -Path $sourcefile1, $sourcefile2, $sourcefile3 -Destination $destination -Force

    Invoke-Command -scriptblock {C:\temp\PsExec.exe -h -s \\$computer cmd /c "C:\Temp\McAfee_update.bat"}
    }
 }

I do apologies if im doing something completely wrong but i’m still learning this all.

ok so in all my wisdom i have it running without using the .bat file.

I have changed the main script to the below:

cls

# variables listed below 
$remotecomputers = Get-Content c:\temp\remotecomputers.txt


# This section will Run the script File
foreach ($computer in $remotecomputers) 
{
  $destination = "\\$computer\c$\Temp"

# This Section will Ping machine to ensure its online  
  if((Test-Connection -Computername $computer -BufferSize 16 -Count 1 -quiet))
    {
    Write-host "Running on updates on $computer"
    Invoke-Command -scriptblock {start-process powershell.exe "c:\temp\McAfee_PL8_update_all.ps1 -ComputerName $computer"}
    }

    else {
    Write-host "$computer is Offline"
    }
 }

this runs the script but does it in a separate powershell window for each machine. (I know I can hide the powershell windows using -windowstyle hidden but i like to see the output) is there any way I can get this to run in the ISE window or run this as a job?

Using invoke-command inside a foreach loop will run in serial (slow). Just use the following to run scripts in parallel.

$remotecomputers = Get-Content c:\temp\remotecomputers.txt
Invoke-Command -FilePath 'c:\temp\McAfee_PL8_update_all.ps1' -ComputerName $remotecomputers -AsJob -ErrorVariable MyError

# Get the progress or output of each running job
Get-Job | Receive-Job

Hi random commandline,

thanks for this but for some reason the output i get from the script is failing when run using invoke-command.

for example:

PS C:\windows\system32> Invoke-Command -FilePath 'C:\Temp\McAfee_PL8_update_all.ps1' -ComputerName hostname1
2017-01-25 10:15:13.695 cmdagent(15204.11796) cmdagent.Error: Failed to get ma info, error code .
2017-01-25 10:15:13.695 cmdagent(15204.11796) cmdagent.Error: Get agent mode failed. Operation failed
2017-01-25 10:15:28.773 cmdagent(5864.14396) cmdagent.Error: Failed to get ma info, error code .
2017-01-25 10:15:28.773 cmdagent(5864.14396) cmdagent.Error: Get agent mode failed. Operation failed.
2017-01-25 10:15:43.867 cmdagent(12816.13396) cmdagent.Error: Failed to get ma info, error code .
2017-01-25 10:15:43.867 cmdagent(12816.13396) cmdagent.Error: Get agent mode failed. Operation failed
2017-01-25 10:15:58.946 cmdagent(14548.4768) cmdagent.Error: Policy enforcement command initiated by cmdagent failed, 508.

but if i run without invoke-command the output shows it successfully runs:

PS C:\windows\system32> C:\Temp\McAfee_PL8_update_all.ps1 -ComputerName hostname1
2017-01-25 10:19:02.599 cmdagent(24260.29572) cmdagent.Info: Properties collect and send command initiated by cmdagent.
2017-01-25 10:19:18.001 cmdagent(27204.30176) cmdagent.Info: Events send to epo, command initiated by cmdagent
2017-01-25 10:19:33.270 cmdagent(19692.25156) cmdagent.Info: Refresh policy command initiated by cmdagent.
2017-01-25 10:19:48.656 cmdagent(24656.16372) cmdagent.Info: Policy enforcement command initiated by cmdagent.

is there some sort of permission issues when I run invoke-command?