2012-11-16 21 views
-4

我需要在運行改變它的程序之前備份主機文件。如何使用Perl來備份主機文件?

因此,我首先刪除備份文件,如果它已經存在,然後用新名稱將主機文件複製到同一目錄中。

我只需要一個完整的腳本來完成這一項任務。我使用Perl2EXE將所有Perl腳本轉換爲EXE。

這是我到目前爲止的代碼:

#use Win32::OLE; 
#use Win32::OLE qw(in); 
use strict; 
use warnings; 

use File::Spec; 
use File::Copy 'copy'; 

$APPNAME = "TEST_TOP_Install"; 
############################################################################################################################### 
###### This section Creates a log file on machines being pushed 
###### The location of the log file is c:\winnt\Install_logs or c:\windows\Install_logs 
############################################################################################################################### 

###### System Root Path 
$rootpath = $ENV{"systemroot"}; 
$alluserspath = $ENV{"ALLUSERSPROFILE"}; 
$programfilespath = $ENV{"PROGRAMFILES"}; 
$SystemDrive = $ENV{"SystemDrive"}; # c: 
$WIN32DIR = "$SystemDrive\\program files\(x86\)"; 

$LOGDIR = "$rootpath\\install_logs" ; 
if (!-e $LOGDIR) { 
    $DIR = `mkdir $LOGDIR`; 
    } 

open ($APPNAME, ">$rootpath\\install_logs\\$APPNAME.log"); 

############################################################################################################################### 
###### This section writes date and time stamp and heading in the log 
############################################################################################################################### 

print $APPNAME ("************************************************************\n"); 
print $APPNAME ("This pack will uninstall and install TEST_TOP\n\n") ; 

$TDATE = `date \/T`; 
$TTIME = `time \/T`; 

print $APPNAME ("__________________________________________\n"); 
print $APPNAME ("Date Started\: $TDATE\n"); 
print $APPNAME ("Time Started\: $TTIME\n"); 
print $APPNAME ("__________________________________________\n\n"); 

############################################################################################################################### 
## SECTION 1: Asset name 
############################################################################################################################### 
$ASSET = `$rootpath\\system32\\hostname\.exe`; 
chomp $ASSET; 

############################################################################################################################### 
## SECTION x: Variables 
############################################################################################################################### 

$OSVERSION = `$rootpath\\system32\\reg\.exe QUERY "\\\\$ASSET\\HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" \/v CurrentBuildNumber`; 
$PRODUCTNAME = `$rootpath\\system32\\reg\.exe QUERY "\\\\$ASSET\\HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" \/v ProductName`; 

############################################################################################################################### 
## SECTION 2: OS Version 
############################################################################################################################### 

if ($OSVERSION =~ /1381/) 
    { 
     print $APPNAME ("WINDOWS NT MACHINE - $ASSET ... exiting\n\n"); 
     print $APPNAME ("Exit Code: 1\n"); 
     exit 1; 
    } 
elsif ($OSVERSION =~ /2195/) 
    { 
     print $APPNAME ("WINDOWS 2000 MACHINE - $ASSET ... exiting\n\n"); 
     print $APPNAME ("Exit Code: 2\n"); 
     exit 2; 
    } 
elsif ($OSVERSION =~ /2600/) 
    { 
     print $APPNAME ("WINDOWS XP MACHINE - $ASSET .. \n\n"); 

     $DisplayVersion = `reg query "\\\\$ASSET\\HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\\{B41ED86C\-800F\-48B8\-B3D7\-4AB19DBB62E5\}" \/v DisplayVersion`; 

     goto WINXPINSTALL; 
    } 
elsif (($OSVERSION =~ /76/) || ($PRODUCTNAME =~ /Windows 7/)) 
    { 
     print $APPNAME ("WINDOWS 7 MACHINE - $ASSET\n\n"); 

     $DisplayVersion = `reg query "\\\\$ASSET\\HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\\{B41ED86C\-800F\-48B8\-B3D7\-4AB19DBB62E5\}" \/v DisplayVersion`; 
    } 
else 
    { 
     print $APPNAME ("Unable to determine OS - $ASSET\n\n"); 
     print $APPNAME ("Exit Code: 3\n"); 
     exit 3; 
    } 
############################################################################################################################### 
## Windows 7 ############# Windows 7 ################################### Windows 7 ############################### Windows 7 ## 
############################################################################################################################### 

if ($DisplayVersion =~ /1.1.10/) 
    { 
     print $APPNAME ("TEST exist ... uninstalling\n\n"); 
     goto WIN7UNINSTALLX; 
    } 
else 
    { 
     print $APPNAME ("TEST does not exist ... Installing\n\n"); 
     goto WIN7INSTALLAPP; 
    } 

WIN7INSTALLAPP: 
############################################################################################################################### 
## SECTION 3: Windows 7 Install 
############################################################################################################################### 

sleep (2); 
print $APPNAME ("TEST Installation\n"); 
print $APPNAME ("____________________________________________________\n\n"); 

autoflush STDOUT; 
autoflush STDERR; 

my $etc = File::Spec->catdir($ENV{SYSTEMROOT}, 'system32\drivers\etc'); 
my $hosts = File::Spec->catfile($etc, 'hosts'); 
my $backup = File::Spec->catfile($etc, 'hosts_backup.bak'); 

copy $hosts, $backup or die "Backup failed: $!"; 
print $APPNAME ("host file backup completed\n\n"); 

`start \/WAIT msiexec \/i "$rootpath\\MSI\\TEST_TOP\\TEST_TOP\.msi" TRANSFORMS="$rootpath\\MSI\\TEST_TOP\\TEST_TOP_WIN7\.mst" \/l*v "$rootpath\\install\_logs\\TEST\_TOP\_MSIinstall\.log" \/qn`; 

$errcode=$?; 
if ($errcode == 0) 
    { 
     print $APPNAME ("TEST install completed successfully\n\n"); 
    } 
else 
    { 
     print $APPNAME ("TEST install failed ... $errcode\n\n"); 
     print $APPNAME ("Exit Code: 4\n"); 
     exit 4; 
    } 
print $APPNAME ("\n\n"); 
exit 0; 


WIN7UNINSTALLX: 
############################################################################################################################### 
## SECTION 4: Windows 7 Uninstall 
############################################################################################################################### 

`start \/WAIT msiexec\.exe \/x \{B30ED86C\-800F\-48B8\-B3D7\-4AB19DBB62E5\} \/l*v \"$rootpath\\install\_logs\\TEST\_TOP\_MSIuninstall\.log\" \/qn`; 

$errcode=$?; 
if ($errcode == 0) 
    { 
     print $APPNAME ("TEST uninstall completed successfully\n\n"); 
     sleep (5); 
    } 
else 
    { 
     print $APPNAME ("TEST uninstall failed ... $errcode\n\n"); 
     print $APPNAME ("Exit Code: 5\n"); 
     exit 5; 
    } 

print $APPNAME ("\n\n"); 

goto WIN7INSTALLAPP; 

############################################################################################################################### 
### WINDOWS XP ###################### WINDOWS XP ######################### WINDOWS XP ########################## WINDOWS XP ### 
############################################################################################################################### 

WINXPINSTALL: 

if ($DisplayVersion =~ /1.1.10/) 

    { 
     print $APPNAME ("TEST exist ... uninstalling\n\n"); 
     goto WINXPUNINSTALLX; 
    } 

else 
    { 

     print $APPNAME ("TEST does not exist ... Installing\n\n"); 
     goto WINXPINSTALLAPP; 
    } 

WINXPINSTALLAPP: 
############################################################################################################################### 
## SECTION 3: Windows XP Install 
############################################################################################################################### 

sleep (2); 
print $APPNAME ("TEST Installation\n"); 
print $APPNAME ("____________________________________________________\n\n"); 

autoflush STDOUT; 
autoflush STDERR; 

my $etc = File::Spec->catdir($ENV{SYSTEMROOT}, 'system32\drivers\etc'); 
my $hosts = File::Spec->catfile($etc, 'hosts'); 
my $backup = File::Spec->catfile($etc, 'hosts_backup.bak'); 

copy $hosts, $backup or die "Backup failed: $!"; 
print $APPNAME ("host file backup completed\n\n"); 

`start \/WAIT msiexec \/i "$rootpath\\MSI\\TEST\_TOP\\TEST\_TOP\.msi" TRANSFORMS="$rootpath\\MSI\\TEST_TOP\\TEST_TOP_XP\.mst" \/l*v "$rootpath\\install\_logs\\TEST\_TOP\_MSIinstall\.log" \/qn`; 

$errcode=$?; 
if ($errcode == 0) 
    { 
     print $APPNAME ("TEST install completed successfully\n\n"); 
    } 
else 
    { 
     print $APPNAME ("TEST install failed ... $errcode\n\n"); 
     print $APPNAME ("Exit Code: 4\n"); 
     exit 4; 
    } 
exit 0; 

WINXPUNINSTALLX: 
############################################################################################################################### 
## SECTION 4: Windows XP Uninstall 
############################################################################################################################### 

`start \/WAIT msiexec\.exe \/x \{B30ED86C\-800F\-48B8\-B3D7\-4AB19DBB62E5\} \/l*v \"$rootpath\\install\_logs\\TEST\_TOP\_MSIuninstall\.log\" \/qn`; 


$errcode=$?; 
if ($errcode == 0) 
    { 
     print $APPNAME ("TEST uninstall completed successfully\n\n"); 
     sleep (5); 
    } 
else 
    { 
     print $APPNAME ("TEST uninstall failed ... $errcode\n\n"); 
     print $APPNAME ("Exit Code: 5\n"); 
     exit 5; 
    } 

print $APPNAME ("\n\n"); 

goto WINXPINSTALLAPP; 
+4

那麼,什麼是你的問題? – David

+0

在void上下文中使用反引號操作符並沒有錯,但是一個糟糕的選擇。反引號用於捕獲命令的輸出。改用['system'](http://p3rl.org/system)。 – memowe

回答

2

幾乎都是最好的,如果可以使用原生的Perl運營商或模塊,而非炮擊了使用comman行實用程序。在這種情況下,File::SpecFile::Copy模塊很有用。這些都是自Perl V5第一次發佈以來的核心Perl發行版的一部分,因此不需要在系統上安裝。

此外,不需要刪除大多數形式的複製或輸出的現有文件:將創建一個具有相同名稱的新文件,並且舊文件將被靜默刪除。

該程序可以滿足您的需求。 注意,你需要以管理員身份運行它,以便能夠寫入系統文件夾,其中hosts是,否則複製將失敗 - 可能與沒有這樣的文件或目錄錯誤。

use strict; 
use warnings; 

use feature 'say'; 

use File::Spec; 
use File::Copy 'copy'; 

autoflush STDOUT; 
autoflush STDERR; 

my $etc = File::Spec->catdir($ENV{SYSTEMROOT}, 'system32\drivers\etc'); 
my $hosts = File::Spec->catfile($etc, 'hosts'); 
my $backup = File::Spec->catfile($etc, 'hosts_backup.bak'); 

say "From $hosts"; 
say "To $backup"; 

copy $hosts, $backup or die "Backup failed: $!"; 
say 'Success'; 

輸出

From C:\Windows\system32\drivers\etc\hosts 
To C:\Windows\system32\drivers\etc\hosts_backup.bak 
Success 
+2

「如果可能的話,幾乎總是最好使用本地Perl運算符或模塊」,這主要是因爲它使錯誤檢查變得容易得多,這往往會幫助它發生(就像這種情況一樣)。 – ysth

+0

'use feature'say';'應該是'use 5.010;'它具有所需的效果,但如果在5.10之前使用則會產生更好的錯誤。 – ysth

+0

在某些情況下(比如在SO上),我更喜歡'使用'say'',因爲這對讀者來說顯而易見。 – memowe

相關問題