Hello,
I have this one-liner that I am trying to put into a script. The file is not getting copied, so the service never gets started. Im pretty sure the syntax around the xcopy is failing.
Invoke-Command -ComputerName $computername -ScriptBlock {
Get-Service -Name "*MSMQ*" | Stop-Service -Force
Remove-Item -Path "W:\MSMQ*" -Recurse -ErrorAction SilentlyContinue
#Start-Process xcopy.exe C:\Temp\MSMQ\* "\\$computername\W$\msmq" /e /h /k /y /o /I
Start-Process -Verbose -FilePath "C:\Windows\system32\cmd.exe" -ArgumentList "xcopy C:\Temp\MSMQ\* \\$computername\W$\MSMQ /e /h /k /y /o /I"
Start-Service -Name "*MSMQ*"
}
The complete script will stop the MSMQ service, remove the W:\MSMQ* directories, perform the xcopy, then starts the service, on a list of remote servers.
Thanks for any help.
oh to add, when I run this manually, I HAVE to run the xcopy in a DOS window. It fails in a PS window.
Thanks
It’s called the double hop issue. You’re remoting to a server and then trying to make another remote connection to copy the file. Simply copy the file to the remote machine prior to your invoke-command
Also the $computername variable doesn’t appear to be defined in the remote scriptblock.
Thanks krzydoug
Here is the complete script.
I reworked it like so:
Clear-Host
$computerlist = Get-content 'C:\Temp\servers.txt'
Write-host "This is the list of computers you are stopping MSMQ service and removing W:\MSMQ* on:`n"
$computerlist
$answer = Read-Host -Prompt "`nProceed with this list? Y/N"
If ($answer -eq 'Y') {
Foreach ($computername in $computerlist) {
If (Test-Connection $computername -Count 1 -Quiet) {
Remove-Item -Path "\\$computername\W$\MSMQ*" -Recurse -Force
Start-Process -Verbose -FilePath "C:\Windows\system32\cmd.exe" -ArgumentList "xcopy C:\Temp\MSMQ\* \\$computername\W$\MSMQ /e /h /k /y /o /I"
Invoke-Command -ComputerName $computername -ScriptBlock {
Get-Service -Name "*MSMQ*" | Stop-Service -Force
Start-Service -Name "*MSMQ*"
}
}
}
}
else {
Invoke-Item "C:\Temp\servers.txt"
}
Im getting an error on the copy I guess. A CMD window opens when I run this, and it says this:
'/y' is not recognized as an internal or external command,
operable program or batch file.
C:\Temp\ITScript\Services>
Then in the PS window it says failed to start the service.