Solved

Running Powershell to automate tasks.

Posted on 2013-01-11
10
512 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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

863 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

22 Experts available now in Live!

Get 1:1 Help Now