tags:

views:

81

answers:

3

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 y

A 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.

A: 

Hello. 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.

alexey.chumagin
this did not actually work I get the following error when I and the start and -wait items: A positional parameter cannot be found that accepts argument 'ossqlk02'. + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
Dan Snell
Try changing it to this: `Invoke-Command -Session $session -Scriptblock { Start-Process -FilePath "C:\MSSQL\DBMaintenance\Mirroring\SERVER_Remove_Mirroring.bat" -ArgumentList "xssqlk02", "ossqlk02" -Wait }` although I'm not 100% sure that it will work.
George Howarth
that actually just caused the script to start and just sit there with no prompt or action what so ever. I just noticed something else so I am going to edit the question a bit.
Dan Snell
+3  A: 

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
George Howarth
A: 

"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

alexey.chumagin
I am pretty confident that it is not a permission issue since I am an admin on the box. Also if you notice the error detials the problem is that the batch file I am calling is trying to create a directory that already exists. The is somethig about the error when it is being run as part of a remote process that is causing it to run over the user prompt and the actualy running of the batch file.
Dan Snell