PERL -- VB 脚本迁移到新的打印机服务器,在本地匹配和删除打印机 -- 帮助具有长共享名称的打印机

Posted

技术标签:

【中文标题】PERL -- VB 脚本迁移到新的打印机服务器,在本地匹配和删除打印机 -- 帮助具有长共享名称的打印机【英文标题】:PERL -- VB Script migrate to new printer server, match & delete printer locally -- HELP with printer that have long share names 【发布时间】:2011-07-21 21:43:38 【问题描述】:

此脚本将有效地迁移到新的打印服务器并删除,例如,这些打印机(短共享名称):

打印机名称=\\p1\(share)Laserjet 1000 -- 共享名称/UNC 路径=\\p1\share

打印机名称=\\p1\(share1)Laser 1000 -- 共享名称/UNC 路径=\\p1\share1

如果我有一台共享名称/UNC 路径较长的打印机,它不会删除该打印机:

打印机名称=\\p1\(share)Laserjet 1000 -- 共享名称/UNC 路径=\\p1\(share)Laserjet1000

打印机名称=\\p1\(share1)Laser 1000 -- 共享名称/UNC 路径=\\p1\(share1)UniversalHP

规则:

    我不能使用 WMI,因为不是每个人都在运行 WMI.. 我需要坚持使用不需要安装任何框架的语言。这就是我选择 PERL 和 VBScript 的原因。 括号始终存在,并且始终包含正确的共享名称。

想法:我想我必须使用“网络视图”将打印机名称与实际共享名称匹配。我想实现这样的东西,以删除具有长共享名称的打印机:

sub RemovePrinterLong

print "Long printer names:";
my @list = `net view $OldServer`;
foreach my $line (@list)

if($line =~ /sharename/i)

my($shr,$rest) = split(/Print/,$line);
$shr =~ s/\s+$//;
if($shr =~ /sharename\)/i)

print "\\\\$OldServer\\$shr\n"; 

#$line =~ /^\s*(.)\s+Print\s+/; $newLine = $1;
#print "=$newLine=";



基本上,我希望它通过捕获括号来捕获它是否是一个长共享名称,然后在旧服务器上运行一个网络视图以创建一个 vbs 脚本来删除那些具有长共享名称的打印机...

这是 PERL 脚本:

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

#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 PQ1
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=\\P1\(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】:
#===============================================================================
#Creat a VB script to remove long share name network printer(s)
#===============================================================================
sub RemovePrinterLong


   open(OUTFILE,">C:\\TEMP\\rmprintlong.vbs") or die "Error open outfile..";
   print OUTFILE 'Set WshNetwork = WScript.CreateObject("WScript.Network")'."\n\n";
         print OUTFILE 'On Error Resume Next'."\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
my @list = `net view $OldServer`;
foreach my $line (@list)

if($line =~ /$prt/i)

my($shr,$rest) = split(/Print/,$line);
$shr =~ s/\s+$//;
if($shr =~ /$prt\)/i)

    #print "prt $prt\n";
    #print "shr $shr";
#print "\\\\$OldServer\\$shr\n"; 
print OUTFILE "PrinterPath = \"\\\\$OldServer\\$shr\"\n";
print OUTFILE "WshNetwork.RemovePrinterConnection PrinterPath, true, true\n\n";

#$line =~ /^\s*(.)\s+Print\s+/; $newLine = $1;
#print "=$newLine=";


      
   


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

   print "Deleting $OldServer long share name printer(s)..\n";
   sleep 2;
        my $rm_results = `cscript c:\\TEMP\\rmprintlong.vbs`;
        print "Script complete.";
   

【讨论】:

以上是关于PERL -- VB 脚本迁移到新的打印机服务器,在本地匹配和删除打印机 -- 帮助具有长共享名称的打印机的主要内容,如果未能解决你的问题,请参考以下文章

博客迁移:Shell脚本批量文件处理

kafka partiton迁移方法与原理

迁移到新的 SQL Server 后,在 MS Access 中更新链接表的最佳方法是啥?

迁移生产环境的GItLab11.3.5到新的服务器

Mercurial:迁移到新的存储库结构 - 子存储库更新

如何将 TFVC 项目从 Azure DevOps 服务器迁移到新的 Azure DevOps 帐户作为 Git 存储库