I have a script I found on the internet for swapping out printers when you get a new print server. https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-for-057ae37a. I don’t know if my issue is specific to the script. I suspect it isn’t. if I run it in powershell, or the ISE, it works great. but if I call it from a command prompt with powershell.exe, it breaks.
it uses get-wmiobject WMI to collect the current printers. this works great.
as it’s collecting the current printers, it adds them to an array called “$global:printerstobedeleted”
it uses a switch table to replace the current printers with their new print server counterparts. this works great.
after it has added the new printers, it does a foreach through the $global:printerstobedeleted" aray (of old printers), and deletes each one.
my issue is with the array. as I said, if I run this in a powershell window or in the ISE, it works fine. but if I call the script from a command prompt:
powershell.exe "&\\server\share\script.ps1"
it suddenly has trouble reading the array. specifically, it doesn’t seem to detect the array members as separate objects. it detects all the printer names as one long string.
so if I add a line to the script to output that $global:printerstobedeleted variable, and run the script in powershell, I get this:
full list of printers to be deleted: \\sisfpsp6\TX095-110PCL \\sisfpsp6\TX095-132PCL
notice a space between the printer names.
if I run the script from a command prompt by calling powershell.exe, I get this:
full list of printers to be deleted: \\sisfpsp6\TX095-132PCL\\sisfpsp6\TX095-110PC
no space between the printer names. later when the delete function runs its ‘foreach’ against that variable, it runs against that one long string, instead of each separate printer name.
why would calling the script from a command prompt matter? i’m on windows 8.1 with powershell 4.