Solved

Running Powershell to automate tasks.

Posted on 2013-01-11
10
514 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…

813 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

17 Experts available now in Live!

Get 1:1 Help Now