• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1250
  • Last Modified:

Run .cmd from Powershell Script

Hi All,

I would like to know how can I run a .cmd file from Powershell and wait until finish to continue with my script:

$DBReindex = "C:\Program Files\P-Synch\default\cgi-bin\Database Reindex Script.CMD"
$process = [System.Diagnostics.Process]::Start("cmd /c $DBReindex")
$process.WaitForExit() 

Open in new window



This is the content of my .cmd file
@echo off
cls
echo DB Reindex in progress... 
for /F %%i in (dblist.txt) do ..\util\dbop -pack %%i >> packresult.txt
for /F %%i in (dblist.txt) do ..\util\dbop -reindex %%i >> reindexresult.txt
echo .
echo .
echo Result is ready.Thank you very much. 

Open in new window



Thanks in advance,
0
Gonzalo Becerra
Asked:
Gonzalo Becerra
  • 4
2 Solutions
 
slidingfoxCommented:
You could try removing the CMD file from the equation and getting the powershell script to do the same thing.

I've not tested the script below, but it might be the sort of thing you need.

$DBReindex = "C:\Program Files\P-Synch\default\cgi-bin\Database Reindex Script.CMD"
$dblist = Get-Content 'dblist.txt'

Write-Host "Db Reindex in progress..."

ForEach ($db in $dblist) {
    C:\<full_path>\util\dbop -pack $db | Out-File packresult.txt -Append
}

ForEach ($db in $dblist) {
    C:\<full_path>\util\dbop -reindex $db | Out-File reindexresult.txt -Append
}

Write-Host
Write-Host
Write-Host "Result is ready. Thank you very much."

Open in new window

0
 
Gonzalo BecerraAuthor Commented:
slidingfox Thank you for your help.

Question, this will wait until finish to continue with the rest of the process?

I just added your lines as function but is mandatory to wait until finish the process of the reindex to continue with the script:

Write-Host "Setting the execution Policy to Unrestricted"
Set-ExecutionPolicy "Unrestricted"

function StopService($Name)
{
	$Service = Get-Service -name $Name -ErrorAction SilentlyContinue 
	if (-not $Service) {$Name + " is not installed on this computer."} 
	else {Write-host "Stopping $Name Service" 
		Stop-Service -Name $Name }
}

function StartService($Name)
{
	$Service = Get-Service -name $Name -ErrorAction SilentlyContinue 
	if (-not $Service) {$Name + " is not installed on this computer."} 
	else {Write-host "Starting $Name Service" 
		Start-Service -Name $Name }
}

function DBIndex()
{
	$DBReindex = "C:\Program Files\P-Synch\default\cgi-bin\Database Reindex Script.CMD"
	$dblist = Get-Content 'dblist.txt'

	Write-Host "Db Reindex in progress..."

	ForEach ($db in $dblist) {
	    C:\Program Files\P-Synch\default\util\dbop -pack $db | Out-File packresult.txt -Append
	}

	ForEach ($db in $dblist) {
	    C:\Program Files\P-Synch\default\util\dbop -reindex $db | Out-File reindexresult.txt -Append
	}

	Write-Host
	Write-Host
	Write-Host "Result is ready. Thank you very much."
}

Invoke-Command -ComputerName . { Stop-Service IISAdmin }
Invoke-Command -ComputerName . { Stop-Service W3SVC }
StopService "Service1"
StopService "Service2"
StopService "Service3"
StopService "Service4"


DBIndex

Invoke-Command -ComputerName . { Start-Service IISAdmin }
Invoke-Command -ComputerName . { Start-Service W3SVC }
StartService "Service1"
StartService "Service2"
StartService "Service3"
StartService "Service4"

Open in new window


Thanks in advance,
0
 
Gonzalo BecerraAuthor Commented:
I'm executing the script but is only showing the proper path in the DBIndex Function but is not executing the command line for each db.

Write-Host "Setting the execution Policy to Unrestricted"
Set-ExecutionPolicy "Unrestricted"

function StopService($Name)
{
	$Service = Get-Service -name $Name -ErrorAction SilentlyContinue 
	if (-not $Service) {$Name + " is not installed on this computer."} 
	else {Write-host "Stopping $Name Service" 
		Stop-Service -Name $Name }
}

function StartService($Name)
{
	$Service = Get-Service -name $Name -ErrorAction SilentlyContinue 
	if (-not $Service) {$Name + " is not installed on this computer."} 
	else {Write-host "Starting $Name Service" 
		Start-Service -Name $Name }
}

function DBIndex()
{
	$DBReindex = "C:\Program Files\P-Synch\default\cgi-bin\Database Reindex Script.CMD"
	$dblist = Get-Content 'dblist.txt'

	Write-Host "Db Reindex in progress..."

	ForEach ($db in $dblist) {
		Write-Host "C:\Program Files\P-Synch\default\util\dbop.exe -pack $db"
	    "C:\Program Files\P-Synch\default\util\dbop -pack $db" | Out-File packresult.txt -Append
	}

	ForEach ($db in $dblist) {
		Write-Host "C:\Program Files\P-Synch\default\util\dbop.exe -reindex $db"
	    "C:\Program Files\P-Synch\default\util\dbop -reindex $db" | Out-File reindexresult.txt -Append
	}

	Write-Host
	Write-Host
	Write-Host "Result is ready. Thank you very much."
}


Invoke-Command -ComputerName . { Stop-Service IISAdmin }
Invoke-Command -ComputerName . { Stop-Service W3SVC }
StopService "Service1"
StopService "Service2"
StopService "Service3"
StopService "Service4"


DBIndex

Invoke-Command -ComputerName . { Start-Service IISAdmin }
Invoke-Command -ComputerName . { Start-Service W3SVC }
StartService "Service1"
StartService "Service2"
StartService "Service3"
StartService "Service4"

Open in new window



This is the result:
C:\Program Files\P-Synch\default\util\dbop.exe -reindex db1
C:\Program Files\P-Synch\default\util\dbop.exe -reindex db2
C:\Program Files\P-Synch\default\util\dbop.exe -reindex db3
C:\Program Files\P-Synch\default\util\dbop.exe -reindex db4
C:\Program Files\P-Synch\default\util\dbop.exe -reindex db5
C:\Program Files\P-Synch\default\util\dbop.exe -reindex db6

Open in new window


But is not executing the command.
0
 
Gonzalo BecerraAuthor Commented:
I solve the issue, this is the final script:



Write-Host "Setting the execution Policy to Unrestricted"
Set-ExecutionPolicy "Unrestricted"

function StopService($Name)
{
	$Service = Get-Service -name $Name -ErrorAction SilentlyContinue 
	if (-not $Service) {$Name + " is not installed on this computer."} 
	else {Write-host "Stopping $Name Service" 
		Stop-Service -Name $Name }
}

function StartService($Name)
{
	$Service = Get-Service -name $Name -ErrorAction SilentlyContinue 
	if (-not $Service) {$Name + " is not installed on this computer."} 
	else {Write-host "Starting $Name Service" 
		Start-Service -Name $Name }
}

function DBIndex()
{
	$DBReindex = "C:\Program Files\Program\Directory\program.cmd"
	$dblist = Get-Content 'dblist.txt'

	Write-Host "Db Reindex in progress..."

	ForEach ($db in $dblist) {
		Write-Host "C:\Program Files\Program\Directory\program.exe -pack $db"
	   & "C:\Program Files\Program\Directory\program.exe" -pack $db #| Out-File packresult.txt -Append
	}

	ForEach ($db in $dblist) {
		Write-Host "C:\Program Files\Program\Directory\program.exe -reindex $db"
	   & "C:\Program Files\Program\Directory\program.exe" -reindex $db #| Out-File reindexresult.txt -Append
	}

	Write-Host
	Write-Host
	Write-Host "Result is ready. Thank you very much."
}


Invoke-Command -ComputerName . { Stop-Service IISAdmin }
Invoke-Command -ComputerName . { Stop-Service W3SVC }
StopService "Service1"
StopService "Service2"
StopService "Service3"
StopService "Service4"


DBIndex

Invoke-Command -ComputerName . { Start-Service IISAdmin }
Invoke-Command -ComputerName . { Start-Service W3SVC }
StartService "Service1"
StartService "Service2"
StartService "Service3"
StartService "Service4"

Open in new window

0
 
Gonzalo BecerraAuthor Commented:
Thank you very much! The script is working as expected.
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now