2011-05-09 170 views
0

我的Perl腳本需要更改以允許打印機共享名較長。這個腳本適用於短打印機共享名稱。如果你做一個NET SHARE,你會明白我的意思。不管怎麼樣,這裏是Perl腳本:Perl腳本將打印機從一臺打印服務器重新映射到另一臺打印服務器

#this script will not migrate novaPDF pritter 
my @printers; 
my %PQ2; 
my %PQ; 
my @NewPQ; 
my $NewServer = "MTAPQ10"; 
my $OldServer = "MTAPQ5"; 

#Retrive print queues info from the new network print server 
print "Generating a printer list on $NewServer..\n"; 
@NewPQ = `net view $NewServer`; 


#Create a required TEMP folder on C: 
system("md C:\\TEMP") if(not (-e "C:\\TEMP")); 

#Create a VBScripts to enumerate network printer connections 
open(OUTFILE,">C:\\TEMP\\EnumPQ.vbs") or die "Unable to create TEMP file"; 

print OUTFILE "Option Explicit\n"; 
print OUTFILE "Dim objNetwork, objPrinter, intDrive, intNetLetter\n"; 
print OUTFILE "Set objNetwork = CreateObject(\"WScript\.Network\")\n"; 
print OUTFILE "Set objPrinter = objNetwork.EnumPrinterConnections\n"; 
print OUTFILE "For intDrive = 0 To (objPrinter.Count -1) Step 2\n"; 
print OUTFILE " intNetLetter = IntNetLetter +1\n"; 
print OUTFILE " Wscript.Echo objPrinter.Item(intDrive +1)\n"; 
print OUTFILE "Next\n"; 
print OUTFILE "Wscript\.Quit(1)\n"; 

close OUTFILE; 

#Run VBScript EnumPQ.vbs to generate a list of connected network printers 
print "Enumerating local printers...\n"; 
my @results = `cscript c:\\TEMP\\EnumPQ.vbs`; 
print "Local printer enumeration complete\n"; 

my $FOUND = 0; 
#Search for printer(s) on MTAPQ5 
foreach my $rec (@results) 
{ 
    chomp $rec; 

    next if($rec =~ /nova/i); #bypass nova PDF printer 
    #Searching for old server in the form of \\ServerName 
    if($rec =~ /\\\\$OldServer/i) 
    { 
     #Exp rec=\\MTAPQ2\(05-103) HP Color LaserJet 4650 PS 6= 
     push @printers, $rec; 
     $FOUND = 1; 
    } 
} 

if($FOUND) 
{ 
    &RemovePrinter(); 
    &AddPrinter(); 
} 
else 
{ 
    print "No network printer on $OldServer found..\n" 
} 

exit 0; #exit main 
#=============================================================================== 
#Creat a VB script to remove network printer(s) 
#=============================================================================== 
sub RemovePrinter 
{ 

open(OUTFILE,">C:\\TEMP\\rmprint.vbs") or die "Error open outfile.."; 
print OUTFILE 'Set WshNetwork = WScript.CreateObject("WScript.Network")'."\n\n"; 

foreach my $printer (@printers) 
{ 
    $printer =~ m/^\\\\$OldServer\\\((.+)\)/i; 
    $PQ2{$1} = "old printer"; 
} 

foreach my $shrname (@NewPQ) 
{ 
    chomp $shrname; 
    $shrname =~ m/^.+\((.+)\).+/i; 
    $PQ{$1} = "New printer"; 
} 

my @PQ2Printers = keys %PQ2; 

foreach my $prt (@PQ2Printers) 
{ 
    if(exists $PQ{$prt}) 
    { 
     #Create VB Script to remove the found printers 
     print OUTFILE "PrinterPath = \"\\\\$OldServer\\$prt\"\n"; 
     print OUTFILE "WshNetwork.RemovePrinterConnection PrinterPath, true, true\n\n"; 
    } 
} 


print OUTFILE "Wscript\.Quit(1)\n"; 
close OUTFILE; 

print "Deleting $OldServer printer(s)..\n"; 
sleep 2; 
my $rm_results = `cscript c:\\TEMP\\rmprint.vbs`; 
#print "remove result:\n"; 
#print $rm_results."\n"; 


}#end sub RemovePrinter 
#=============================================================================== 
#Create a VB script to add network printers 
#=============================================================================== 
sub AddPrinter 
{ 
open(OUTFILE,">C:\\TEMP\\addprint.vbs") or die "Error open outfile.."; 
print OUTFILE 'Set WshNetwork = WScript.CreateObject("WScript.Network")'."\n\n"; 


my @PQ2Printers = keys %PQ2; 

#if old printer exists on new server, map it. 
foreach my $prt (@PQ2Printers) 
{ 
     if(exists $PQ{$prt}) 
     { 
     #Create VB Script to map the found printers to new server 
     print OUTFILE "PrinterPath = \"\\\\$NewServer\\$prt\"\n"; 
     print OUTFILE "WshNetwork.AddWindowsPrinterConnection PrinterPath, true, true\n\n"; 
     print "Remapping $OldServer printer to: \\\\$NewServer\\$prt\n"; 
    } 
} 

print OUTFILE "Wscript\.Quit(1)\n"; 
close OUTFILE; 

sleep 2; 
my $add_results = `cscript c:\\TEMP\\addprint.vbs`; 
#print "Add result:\n"; 
#print $add_results."\n"; 

}#end sub AddPrinter 
#=============================================================================== 
+1

我不確定這是一個perl問題,好像問題是如何從'net view'獲得寬輸出或如何用windows腳本查詢打印機列表。如果您將代碼剝離到顯示問題所需的最低限度,您可能會得到更好的答案。 – Andy 2011-05-09 18:38:13

回答

2

你看着在CPAN不同的Win32模塊?

這些提供比嘗試從Windows命令行取消更好的接口。

例如Win32::Printer::Enum會找到並列出,而不是使用所有可用的打印機:

@NewPQ = `net view $NewServer`; 

而且,你應該能夠使用的,而不是去關閉到系統命令使目錄Perl的mkdir命令。

如果您已經下載了ActivePerl,請查看他們的Win32模塊。它可能有你需要的一切。

相關問題