location based printing

This topic contains 7 replies, has 5 voices, and was last updated by Profile photo of Michael Martin Michael Martin 10 months ago.

  • Author
  • #33296
    Profile photo of Michael Martin
    Michael Martin

    Hello all

    I am working with appsense getting location based printing working. Short of it is I am setting environment variables that say where an endpoint is. where I am stuck is setting the dfault printer
    # Stores netowrk Printers int 4printer variabl#####
    $printer1 = (Get-ChildItem Env:PrtLoc).Value
    $printer1 = (Get-ChildItem Env:PrtLoc).Value
    $printer1 = (Get-ChildItem Env:PrtLoc).Value
    $default = (Get-ChildItem Env:PrtLoc).Value

    # Adds Network Printers
    (New-Object -ComObject Wscrip.Network).AddWindowsPrinterConnection($Prnter1)
    (Get-WmiObject -ComputerName . -Class Win32_Printer -Filter "Name='$default'").SetDefualtPrinter()
    (New-Object -ComObject Wscrip.Network).AddWindowsPrinterConnection($Prnter2)
    (New-Object -ComObject Wscrip.Network).AddWindowsPrinterConnection($Prnter3)

    # things i have tried
    # (New-Object -ComObject Wscript.Netowrk).SetDefaultPrinter('sfg-bldg-h44ns-a-hp") #(this worked)
    # $Default = (Get-WmiObject -Class Win32_Printer | ShareName |ft -auto

    I can see it calling the variable however it gives me a null output.

    thanks in advance for your help

  • #33300
    Profile photo of Curtis Smith
    Curtis Smith

    You are getting no results form

    # $Default = (Get-WmiObject -Class Win32_Printer | ShareName |ft -auto

    because "ShareName" is not a cmdlet. You can select the ShareName property by using Select-Object

    # $Default = Get-WmiObject -Class Win32_Printer | Select-Object -ExpandProperty ShareName

    With that said, I'm not real clear on what you are trying to accomplish here.

  • #33312
    Profile photo of Michael Martin
    Michael Martin

    Hi Curtis

    basically I am adding printers by environment variables we are setting in appsense.

    so ENVVARIABLE1=\\printerserver\printer1
    ENVVARIABLE2 = \\printerserver\printer2
    ENVVARIABLE3 = \\printersernver\printer3

    I have the commands working to add the printers and I can see it calling it by the printer sharename when I try to pull it for the default printer. But I get a null-value when it try to do the default printer on ENVVARIABLE1. It first states

    Get-WMIObject : Invalid query"select * from Win32Printer where Name='\\Printserver\Printer1"

    this tells me Its seeing the env variable but I get a null value when I try and $default into the default printe rcommnad.

    I will try the Select-Object option and see if I can get that to work

    thanks for the reply Curtis

  • #33313
    Profile photo of Curtis Smith
    Curtis Smith

    Just curious, why not use Group Policy Preferences for this instead of scripting it?

  • #33316
    Profile photo of tommymaynard

    What Curtis said. This was the exact thought I had when I read the thread title. You'd likely be better suited to use GPP for this task. I love PowerShell, but there's times when it's not the solution.

  • #33318
    Profile photo of Peter Jurgens
    Peter Jurgens

    I also noticed that you have single quotes around the $default variable when trying to set the default printer. Powershell interprets text in single quotes as literal so perhaps you may just need to escape the single quotes with a backtick to get your current code to work. Bear in mind that I'm answering on mobile with no chance to test my suggestion.

  • #33350
    Profile photo of Jessie Westlake
    Jessie Westlake

    @Peter: Because the single-quotes are inside of double-quotes, the single-quotes will be interpreted as text and not interfere with the variable expansion.

    @Michael: I don't know if you typed this out as an example of your script, but it is full of typos from what I can tell.

    You are setting $Printer1 three times in a row to the same thing, and then later you are calling $Prnter1 and $Prnter2, etc. Notice that the variable you use later is missing the "i" in the word "Printer"? Also, you misspelled "default", you misspelled "network" and some other things.

    I'm assuming you must have just typed this out on the fly here or something. If that's the case, try posting your verbatim code so we can try to help you pinpoint the problem.

    Good luck!

  • #33353
    Profile photo of Michael Martin
    Michael Martin

    Hi all

    @ Curtis Smith & Tommy Maynard We are using appsnese and the client has old 2003 architecture for with a ton of AD sync and GPO issues. the building is 7 floors with about 200 printers and the easiest way was to leverage appsense to set env variables buy printer and endpoint name. Alot of the endpoints are linux based for VDI which makes it hard to keep it uniform with two different solutions. So I was searching for a powershell solution to run in appsense. Trust me that was suggested.

    @ Peter and Jessi sorry for the typos and thanks for the input

    To all I figured out that the command to set the printer to default was looking for the share name. I was able to figure out the command which i will post tomorrow with a better explanation.

    Sorry I have to retype things and make sure you cannot see certain information like computer names and IP information.

You must be logged in to reply to this topic.