2012-11-16 20 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

相关问题