Solved

Running Powershell to automate tasks.

Posted on 2013-01-11
10
520 Views
Last Modified: 2013-01-29
Hi EE,

I'm new to powershell and would like a powershell script to do jobs for me.

I have several things to do in Windows 7. Several of which is to unzip folders, copy files and click on some exe files to run. I've done this manually and would like to automate this. Running a script to automate these manual tasks will help a lot.

Ok heres some background information:

There is a root folder, lets call it RootFolder that has 2 folders. Lets call it foo1 and foo2. These 2 folders contain a number of files.

e.g,       C:\RootFolder\foo1\f1.txt

      C:\RootFolder\foo2\f21.txt
      C:\RootFolder\foo2\f22.txt


There is also two zipped folders that contain a number of folders and files. Lets called these zipped folders, foo1.zip and foo2.zip. When the zipped folders are unzipped they contain a similar structure as the above folders.

e.g foo1.zip unzips to C:\foo1\foo1\f1.txt, C:\foo1\foo1\f1.exe, and other folders and files which I'm not interested in. Like wise foo2.zip unzips to simillar file structures.

What I'd like powershell to do is:

1. unzip foo1.zip
2. copy and replace the file C:\foo1\foo1\f1.txt with C:\RootFolder\foo1\f1.txt

3. unzip foo2.zip
4. copy and replace the two files C:\foo2\foo2\f21.txt and C:\foo2\foo2\f22.txt with C:\RootFolder\foo2\f21.txt and C:\RootFolder\foo2\f22.txt

Output a message to say that it finished 1,2,3,4.

5. Run the exe file f1.exe in C:\foo1\foo1\. Let it run for 5 mins.

6. Run the exe file f2.exe in C:\foo2\foo2\. Let it run for 5 mins.

7. Output a message to show that it finished.


Thanks in advance for your help.
0
Comment
Question by:Daera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38771342
do you wish to terminate f1.exe and f2.exe after 5 minutes?
do you need to wait 5 minutes between running the 2 exe files?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38771377
that should get you started:

$shell_app = new-object -com shell.application

	
function Unzip($zipfile, $destination){
	$zip_file = $shell_app.namespace($zipfile)
	$destination = $shell_app.namespace($destination)
	$destination.Copyhere($zip_file.items(), 0x10)

	write-host "Unzip $zipfile to $destination"
}

Unzip("c:\foo1\foo1.zip", "c:\foo1")
Copy-Item "c:\foo1\foo1.txt" "C:\RootFolder\foo1" -Force
write-host c:\foo1\foo1.txt was copied to C:\RootFolder\foo1

Unzip("c:\foo2\foo2.zip", "c:\foo2")
Copy-Item "c:\foo2\foo2.txt" "C:\RootFolder\foo2" -Force
write-host c:\foo2\foo2.txt was copied to C:\RootFolder\foo2

$exe1 = Start-Process "C:\foo1\foo1\f1.exe" -passthru
write-host "running f1.exe"

$exe2 = Start-Process "C:\foo2\foo2\f2.exe" -passthru
write-host "running f2.exe"

$time_expired = $false
$exe_complete = $false
$script:StartTime = get-date
while($time_expired -eq $false -and $exe_complete -eq $false)
{		
	if($exe1.HasExited -and $exe2.HasExited){
		$exe_complete = $true
	}
	else{
		 sleep 1
		 $elapsedTime = $(get-date) - $script:StartTime
		 if($elapsedTime.Minutes -eq 5){
		 	$time_expired = $true
		 }
	}
}

#time expired so terminate exe1/exe2 (if running)
if($exe1.HasExited -eq $false){
	stop-process $exe1.Id
}

if($exe1.HasExited -eq $false){
	stop-process $exe2.Id
}

Open in new window

0
 

Author Comment

by:Daera
ID: 38771573
@sedgwick.

I wish to terminate f1.exe after 5 mins before running f2.exe for another 5 minutes. Both cannot run at the same time. Thanks.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 37

Expert Comment

by:ArneLovius
ID: 38771739
you could try serialising the processes

$shell_app = new-object -com shell.application
	
function Unzip($zipfile, $destination){
	$zip_file = $shell_app.namespace($zipfile)
	$destination = $shell_app.namespace($destination)
	$destination.Copyhere($zip_file.items(), 0x10)

	write-host "Unzip $zipfile to $destination"
}

Unzip("c:\foo1\foo1.zip", "c:\foo1")
Copy-Item "c:\foo1\foo1.txt" "C:\RootFolder\foo1" -Force
write-host c:\foo1\foo1.txt was copied to C:\RootFolder\foo1

Unzip("c:\foo2\foo2.zip", "c:\foo2")
Copy-Item "c:\foo2\foo2.txt" "C:\RootFolder\foo2" -Force
write-host c:\foo2\foo2.txt was copied to C:\RootFolder\foo2

$exe1 = Start-Process "C:\foo1\foo1\f1.exe" -passthru
write-host "running f1.exe"
$time_expired = $false
$exe_complete = $false
$script:StartTime = get-date
while($time_expired -eq $false -and $exe_complete -eq $false)
{		
	if($exe1.HasExited){
		$exe_complete = $true
	}
	else{
		 sleep 1
		 $elapsedTime = $(get-date) - $script:StartTime
		 if($elapsedTime.Minutes -eq 5){
		 	$time_expired = $true
		 }
	}
}
#time expired so terminate exe1 (if running)
if($exe1.HasExited -eq $false){
	stop-process $exe1.Id
}

$exe2 = Start-Process "C:\foo2\foo2\f2.exe" -passthru
write-host "running f2.exe"
$time_expired = $false
$exe_complete = $false
$script:StartTime = get-date
while($time_expired -eq $false -and $exe_complete -eq $false)
{		
	if($exe2.HasExited){
		$exe_complete = $true
	}
	else{
		 sleep 1
		 $elapsedTime = $(get-date) - $script:StartTime
		 if($elapsedTime.Minutes -eq 5){
		 	$time_expired = $true
		 }
	}
}
#time expired so terminate exe2 (if running)
if($exe1.HasExited -eq $false){
	stop-process $exe2.Id
}
write-host "completed"

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38771756
here:

	$shell_app = new-object -com shell.application

		
	function Unzip($zipfile, $destination){
		$zip_file = $shell_app.namespace($zipfile)
		$destination = $shell_app.namespace($destination)
		$destination.Copyhere($zip_file.items(), 0x10)

		write-host "Unzip $zipfile to $destination"
	}

	function CheckExeExpiration($exe){
		$time_expired = $false
		$exe_complete = $false
		$script:StartTime = get-date
		while($time_expired -eq $false -and $exe_complete -eq $false)
		{		
			#exe has exited before time expired
			if($exe.HasExited){
				$exe_complete = $true
			}
			else{
				 sleep 1
				 $elapsedTime = $(get-date) - $script:StartTime
				 if($elapsedTime.Minutes -eq 5){
				 	$time_expired = $true
				 }
			}
		}

		#time expired so terminate exe
		if($exe.HasExited -eq $false){
			stop-process $exe.Id
		}	
	}
	
	Unzip("c:\foo1\foo1.zip", "c:\foo1")
	Copy-Item "c:\foo1\foo1.txt" "C:\RootFolder\foo1" -Force
	write-host c:\foo1\foo1.txt was copied to C:\RootFolder\foo1

	Unzip("c:\foo2\foo2.zip", "c:\foo2")
	Copy-Item "c:\foo2\foo2.txt" "C:\RootFolder\foo2" -Force
	write-host c:\foo2\foo2.txt was copied to C:\RootFolder\foo2

	$exe1 = Start-Process "C:\foo1\foo1\f1.exe" -passthru
	write-host "running f1.exe"

	CheckExeExpiration($exe1)
	
	$exe2 = Start-Process "C:\foo2\foo2\f2.exe" -passthru
	write-host "running f2.exe"

	CheckExeExpiration($exe2)

Open in new window

0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 38772809
sedgwick,

a "call" like Unzip("c:\foo1\foo1.zip", "c:\foo1") will never work, PowerShell does not know of "functions" in the classic way. The call needs to be like with cmdlets:
Unzip "c:\foo1\foo1.zip" "c:\foo1"

Open in new window

(no parens, no comma). A comma operator always builds an array. Only exception is a method call, where comma is the argument separator.

Further, I would not recommend to compare boolean values against $false, because that is a kind of negation difficult to read. It is better to use the -not (or !) operator:
while (!$time_expired -and !$exe_complete)

Open in new window


Putting that aside, the code should work as shown. However, instead of a polling/waiting loop, I would just use:
start-process "C:\foo1\foo1\f1.exe" -PassThru | wait-process -Timeout 300 -ea SilentlyContinue
start-process "C:\foo2\foo2\f2.exe" -PassThru | wait-process -Timeout 300 -ea SilentlyContinue

Open in new window

That uses less resources.
If the -ea parameter is omitted, it will output an error message if the process is killed because of timeout. With -ea SilentlyContinue, no message is visible.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38773495
@Qlemo

From all my teachers I have grown wise

10x.
0
 

Author Comment

by:Daera
ID: 38781003
@sedgwick, tried the script but as mentioned above it didnt unzip the folders. Any pointers. Thanks.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 250 total points
ID: 38781586
cls
function Unzip($file, $dest){

$shellApplication = new-object -com shell.application 
$zipPackage = $shellApplication.NameSpace($file) 
$destinationFolder = $shellApplication.NameSpace($dest) 
$destinationFolder.CopyHere($zipPackage.Items(),20) 

}
	
	Unzip "c:\foo1\foo1.zip", "c:\foo1" 
	Copy-Item "c:\foo1\foo1.txt" "C:\RootFolder\foo1" -Force
	write-host c:\foo1\foo1.txt was copied to C:\RootFolder\foo1

	Unzip "c:\foo2\foo2.zip", "c:\foo2" 
	Copy-Item "c:\foo2\foo2.txt" "C:\RootFolder\foo2" -Force
	write-host c:\foo2\foo2.txt was copied to C:\RootFolder\foo2

	write-host "running f1.exe"
start-process "C:\foo1\foo1\f1.exe" -PassThru | wait-process -Timeout 300 -ea SilentlyContinue

	write-host "running f2.exe"
start-process "C:\foo2\foo2\f2.exe" -PassThru | wait-process -Timeout 300 -ea SilentlyContinue

write-host "Done"

Open in new window

0
 

Author Closing Comment

by:Daera
ID: 38834086
Thanks
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question