Solved

Running Powershell to automate tasks.

Posted on 2013-01-11
10
524 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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 70

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
There are many software programs on offer that will claim to magically speed up your computer. The best advice I can give you is to avoid them like the plague, because they will often cause far more problems than they solve. Try some of these "do it…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

728 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