CHiLL Posted November 24, 2015 Posted November 24, 2015 (edited) Hello, I have a PS script to add printers that I am currently testing. When attempting to run the script as a normal user as a logon script, the printers are NOT added. If I then open a command prompt and run the script from NETLOGON, the printers still DO NOT add, though error messages are returned in the command line. If I simply re-run the script immediately, without doing anything else...the script then adds the printers as per the script! I am stumped as to why, and I'm under pressure to get this working. In my instance, we are going to run this as a logon script (defined in the AD user properties), so the PS script has to be launched from a command prompt. So there are two scripts that are run and no entries are entered into the event logs as to why this fails. The majority of our clients (including the machines I'm testing with) are Windows 8.1 x64. The only other OS we have is Windows 7 x86, though I haven't tested at all using this OS yet. Script 1: Printers.bat Powershell.exe -NoProfile -ExecutionPolicy Bypass -File "\\SJW.Internal\NETLOGON\Printers.ps1" Script 2: Printers.ps1 <# CHI 10/11/2015 Script to delete all network printers and then add printers based on location and/or user#> <#====================================================================================================================================================== This section of the script will set up the parameters used to add printers later in the script ======================================================================================================================================================#> $User = $env:USERNAME #$User = "15test" $Machine = $env:COMPUTERNAME #$Machine = "LIB-01" $net = New-Object -ComObject WScript.Network #Create the network object <#====================================================================================================================================================== This section Like the complete list of network printers on the print server ======================================================================================================================================================#> #Network Printers $Printer1 = "\\Print\PHOTOCOPIER" $Printer2 = "\\Print\Photocopier Colour" $Printer3 = "\\Print\Head-PA" $Printer4 = "\\Print\KSI" $Printer5 = "\\Print\Admin" $Printer6 = "\\Print\Finance" $Printer7 = "\\Print\RM01-HP4005" $Printer8 = "\\Print\ICTTECH-KY5250" $Printer9 = "\\Print\EXAMS-HP2055" $Printer10 = "\\Print\CHAPEL-HP2055" $Printer11 = "\\Print\MEDIA-HP5550" $Printer12 = "\\Print\SFC-HP1515" $Printer13 = "\\Print\RM02" $Printer14 = "\\Print\RM03" $Printer15 = "\\Print\SFC" $Printer16 = "\\Print\T2" $Printer17 = "\\Print\T4" $Printer18 = "\\Print\T7" $Printer19 = "\\Print\ALA" $Printer20 = "\\Print\MEDIA-FS5250" $Printer21 = "\\Print\Library" $Printer22 = "\\Print\ExecHead-WF5190" $Printer23 = "\\Print\RM17-HPPro200" <#====================================================================================================================================================== Below defines the functions that are called later in the script. ======================================================================================================================================================#> Function DeletePrinters { Get-WMIObject Win32_Printer | where{$_.Network -Like 'true'} | foreach{$_.delete()} } Function Students { #Library If ($Machine -Like "*LIB*") { $net.AddWindowsPrinterConnection($Printer21) $net.SetDefaultPrinter($Printer21) } #RM01 If ($Machine -Like "*RM01*") { $net.AddWindowsPrinterConnection($Printer7) $net.SetDefaultPrinter($Printer7) } #RM02 If ($Machine -Like "*RM02*") { $net.AddWindowsPrinterConnection($Printer13) $net.SetDefaultPrinter($Printer13) } #RM03 If ($Machine -Like "*RM03*") { $net.AddWindowsPrinterConnection($Printer14) $net.SetDefaultPrinter($Printer14) } #SFC If ($Machine -Like "*SFC*") { $net.AddWindowsPrinterConnection($Printer15) $net.SetDefaultPrinter($Printer15) } #T2 If ($Machine -Like "*T2*") { $net.AddWindowsPrinterConnection($Printer16) $net.SetDefaultPrinter($Printer16) } #T4 If ($Machine -Like "*T4*") { $net.AddWindowsPrinterConnection($Printer17) $net.SetDefaultPrinter($Printer17) } #T8 If ($Machine -Like "*T8*") { $net.AddWindowsPrinterConnection($Printer18) $net.SetDefaultPrinter($Printer18) } } Function Staff { #All Staff $net.AddWindowsPrinterConnection($Printer1) $net.AddWindowsPrinterConnection($Printer2) If ($User -NotLike "KMarsden*") { $net.SetDefaultPrinter($Printer1) } #Admin If ($Machine -Like "*Office*") { $net.AddWindowsPrinterConnection($Printer5) $net.SetDefaultPrinter($Printer5) } #ALA If (($User -Like "ALabas*") -OR ($User -Like "CHazelwood*")) { $net.AddWindowsPrinterConnection($Printer19) If ($Machine -Like "*DEPUTYHEAD-02*") { $net.SetDefaultPrinter($Printer19) } } #Chapel If ($User -Like "LEllis*") { $net.AddWindowsPrinterConnection($Printer10) If ($Machine -Like "CHAPEL") { $net.SetDefaultPrinter($Printer10) } } #Exams If (($User -Like "MNash*") -OR ($User -Like "MKesterto*n")) { $net.AddWindowsPrinterConnection($Printer9) If ($Machine -Like "*EXAMS*") { $net.SetDefaultPrinter($Printer9) } } #Exec-Head If (($User -Like "AQuinn*") -OR ($User -Like "CHazelwood*") -OR ($User -Like "KMarston*")) { $net.AddWindowsPrinterConnection($Printer22) If ($Machine -Like "*Exec-Head*") { $net.SetDefaultPrinter($Printer22) } } #Finance If (($User -Like "TMcNiell*") -OR ($User -Like "CGeorge*")) { $net.AddWindowsPrinterConnection($Printer3) $net.SetDefaultPrinter($Printer3) } #Head-PA If (($Machine -Like "*Office*") -OR ($Machine -Like "*HEAD-PA*")) { $net.AddWindowsPrinterConnection($Printer3) If ($User -Like "AQuirke*") { $net.SetDefaultPrinter($Printer3) } } #ICT Technicians If (($Machine -Like "*MWI*") -OR ($User -Like "Mariusz*") -OR ($Machine -Like "*CHI*") -OR ($User -Like "CHil*l")) { $net.AddWindowsPrinterConnection($Printer1) $net.AddWindowsPrinterConnection($Printer2) $net.AddWindowsPrinterConnection($Printer8) $net.SetDefaultPrinter($Printer8) } #KSI If (($Machine -Like "*Office*") -OR ($Machine -Like "*DEPUTYHEAD-01*")) { $net.AddWindowsPrinterConnection($Printer4) If ($User -Like "KSingh*") { $net.SetDefaultPrinter($Printer4) } } #Media If ($User -Like "PMull*") { $net.AddWindowsPrinterConnection($Printer11) $net.AddWindowsPrinterConnection($Printer20) $net.SetDefaultPrinter($Printer20) } #RM17 If ($Machine -Like "*RM17*") { $net.AddWindowsPrinterConnection($Printer23) } #T2 If (($Machine -Like "*T2*") -OR ($Machine -Like "*T2B*")) { $net.AddWindowsPrinterConnection($Printer23) $net.SetDefaultPrinter($Printer23) } } <#====================================================================================================================================================== This section is the main body of the script, where is it actually executed. It will delete all existing network printers and then determine if the user running the script is a student or a member of staff. If the user is a student, then the script will run the students function, which will determine which room the user based on the machine name add the appropriate printer for that room. If the user is a member of staff, then the script will run the staff function, which will add a printer based on either location (determined by machine name) or by the user and add the appropriate printer. ======================================================================================================================================================#> DeletePrinters If (($User -Like "1*") -OR ($User -Like "09*")) { Students } else { Staff } Error message returned via the command line after the first run: Exception calling "AddWindowsPrinterConnection" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))" At \\SJW.Internal\NETLOGON\Printers2.ps1:93 char:13 + $net.AddWindowsPrinterConnection($Printer1) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: ( [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation Exception calling "AddWindowsPrinterConnection" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))" At \\SJW.Internal\NETLOGON\Printers2.ps1:94 char:13 + $net.AddWindowsPrinterConnection($Printer2) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: ( [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation Exception calling "SetDefaultPrinter" with "1" argument(s): "There is no printer called "\\Print\PHOTOCOPIER"." At \\SJW.Internal\NETLOGON\Printers2.ps1:96 char:21 + $net.SetDefaultPrinter($Printer1) } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: ( [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation Can anybody help?! Edited November 24, 2015 by CHiLL
CHiLL Posted November 24, 2015 Author Posted November 24, 2015 Note: The error from the command line "0x80070057" is the exact same error code that we have been receiving whilst trying to deploy printers using Group Policy Preferences. As I described in one of my other threads: http://www.edugeek.net/forums/windows/160941-group-policy-preferences-printer-mapping-issues.html
old_n07 Posted December 9, 2015 Posted December 9, 2015 We are using a similar method with the exception of creating functions for mapping the printers, it is working OK in our environment (Win 7 x64) - here is a sample of our script, I've only included a couple of rooms to keep it short #Script for mapping printers to fixed classroom and office machines depending on computer name #variables $script:name = $env:COMPUTERNAME.ToLower() #get the name of the computer running the script and convert to lowercase $script:server = "\\servername\" #your print server - edit as necessary but leave trailing \ #Map a printer function function map { $map = $server + $Printer $MapPrinter = new-Object -com WScript.Network $MapPrinter.AddWindowsPrinterConnection($map) } #Map and set a printer as default function function mapdefault { $map = $server + $Printer $MapPrinter = new-Object -com WScript.Network $MapPrinter.AddWindowsPrinterConnection($map) $MapPrinter.SetDefaultPrinter($map) } function mapprinter{ # example of single printer mapping for any computer in f004 e.g. computer name is f004n01 if($name -like "f004*"){$script:printer = "f004_laser" mapdefault } # example of mapping multiple printers and setting a default printer for any computer in f014 e.g. computer name is f014n01 if($name -like "f014*"){$script:printer = "f014_laser" mapdefault $script:printer = "f025ext_copier" map } } #Delete all currently mapped network printers Get-WMIObject Win32_Printer | where{$_.Network -eq 'true'} | foreach{$_.delete()} #Call mapprinter function mapprinter
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now