Batch file has an error that when called from a Powershell script does not allow the batch file to finish
In the script below I am calling a batch file to break mirroring between some dbs. The batch file has a user prompt to start the script but the PS script races right past it. When I call the batch file directly from the powershell console it works fine. How can I keep the开发者_高级运维 script from move past the invoke command block with until the batch file is complete?
$session = New-PSSession -computerName xssqlk02 -credential $cred
Invoke-Command -Session $session -Scriptblock {c:\MSSQL\DBMaintenance\Mirroring\SERVER_Remove_Mirroring.bat xssqlk02 ossqlk02}
Remove-PSSession $session
edit: I trimmed up just the part of the code that I am having problems with. When this is run from a ps script I just noticed (it's been a long day....) I am getting the following error and it runs through the user prompt at that point.
PS C:\Users\dans> C:\Tools\Scripts\test5.ps1 A subdirectory or file c:\MSSQL\DBMaintenance\Mirroring\Common already exists. + CategoryInfo : NotSpecified: (A subdirectory ...already exists.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
Invalid drive specification Remove Mirroring for RCM databases between xssqlk02 and ossqlk02: Continue? y/n 0 File(s) copied
Here is what the output is when I just run the batchfile directly from the PS console on the local machine.
PS C:\Documents and Settings\DanS> c:\MSSQL\DBMaintenance\Mirroring\SERVER_Remove_Mirroring.bat xssqlk02 ossqlk02 Remove Mirroring for RCM databases between xssqlk02 and ossqlk02: Continue? y/n yA subdirectory or file c:\MSSQL\DBMaintenance\Mirroring\Common already exists.
\OPFLSK02\SQLBackupsForTape\DBMaintenance\Mirroring\Common\DB_Create_Snapshots.bat \OPFLSK02\SQLBackupsForTape\DBMaintenance\Mirroring\Common\DB_Force_Mirror_To_Principal.bat ....... 18 File(s) copied
The error occurs because the batch file does not have a check to see if the directory already exists. How do I handle this is from the invoke command block to allow the script to continue? For the moment I am not able to change the batch file itself.
If Alexey's solution doesn't work, you can try this one:
$job = Invoke-Command -Session $session -Scriptblock { Start-Process -FilePath "C:\MSSQL\DBMaintenance\Mirroring\SERVER_Remove_Mirroring.bat" -ArgumentList "xssqlk02", "ossqlk02" -Wait } -AsJob
$job | Wait-Job
You can try so
Invoke-Command -Session $session -Scriptblock {start c:\MSSQL\DBMaintenance\Mirroring\SERVER_Remove_Mirroring.bat xssqlk02 ossqlk02 -wait}
The idea is that the cmdlet will wait until you finish the bat file.
"Invalid drive specification Remove Mirroring for RCM databases between xssqlk02 and ossqlk02:"
it may indicate that $session dont have access to ossqlk02. Try appoint yourself an admin on all computers and run script under admin. If it does not help, you can to use utility - PsExec link PsExec support
精彩评论