Solved

Running Powershell to automate tasks.

Posted on 2013-01-11
10
510 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
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
 
LVL 36

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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 68

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

The curse of the end user strikes again      

You’ve updated all your end user’s email signatures. Hooray! But guess what? They’re playing around with the HTML, adding stupid taglines and ruining the imagery. Find out how you can save your signatures from end users today.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now