1

Sorry new around here!

Basically I want to create a simple script that will create a box that will output the data.

We have multiple print servers around Europe and to make it easy I want Tech users to be able to input the "Print Server Name" and it'll output the relevant information.

Currently using

Get-Printer -ComputerName SERVERNAME | Export-CSV -Path C:\temp\SERVERNAME.csv

So I just want a simple box that they can input only the server name and it'll output the information without the need of them going into powershell and changing the script.

  • 3
    Does it have to be a GUI? If the user doesn't mind entering the server name on the command line, you could use `$serverName = Read-Host "Please enter the server name"` instead, way less code :) – Mathias R. Jessen Nov 06 '18 at 17:15
  • 1
    https://learn.microsoft.com/en-us/powershell/scripting/getting-started/cookbooks/creating-a-custom-input-box?view=powershell-6 – JohnLBevan Nov 06 '18 at 17:17
  • Thanks, @JohnLBevan will take a look at the Link provided. –  Nov 06 '18 at 18:08
  • Thanks, @MathiasR.Jessen It doesn't need to be a GUI was just looking for it to be more user friendly. –  Nov 06 '18 at 18:09

1 Answers1

1

This is probably the easiest way. Put your code in a function, then use Show-Command to automatically generate a GUI.

function Get-PrinterExport {
    param($ServerName)

    Get-Printer -ComputerName $ServerName | Export-CSV -Path C:\temp\SERVERNAME.csv
}

Show-Command Get-PrinterExport 

Now by just running the script, you can do your inputs in the GUI.

If you also want to show the output without navigating to the csv, you can replace Export-CSV with a few different things.

Export-Excel is a cmdlet you can download which can auto-launch microsoft excel.

Out-GridView is a cmdlet you can use to generate a window that is similar to excel. Example:

function Get-GridExample {
    param($example)

    1..20 | % {
        [pscustomobject]@{
            a = $_
            b = $_ + 1
            c = $_ + 2
            d = $example
        }
    } | Out-GridView -Title 'Example Data'
}

Show-Command Get-GridExample

Hope this helps you get started! You will definitely want to flesh this out more with error handling, required parameters, and etc.

Edit: You can use this method to launch the script with a double click: https://stackoverflow.com/a/10137272/4868262 or just compile it yourself.

Jacob Colvin
  • 2,625
  • 1
  • 17
  • 36
  • Thanks! I’ll give this a go tomorrow. –  Nov 06 '18 at 17:33
  • I think he was saying he didnt want them to have to go into powershell and change the script. This would still not fix that issue. – ArcSet Nov 06 '18 at 17:41
  • How so @ArcSet ? Did you run the script? You can double click this and enter whatever you would like. – Jacob Colvin Nov 06 '18 at 17:42
  • So basically... Sorry if it was entirely clear. Just say for instance we have FRANCE, UK and Ireland. I want to have the script written so it has all the servers names inputted. All the end user has to do is enter FRANCE into a box and it'll run the FRANCE script. –  Nov 06 '18 at 18:04
  • @JacobColvin is there anyway I can alter the script so it only gathers the Printer Name and Location? As of now its gathering everything. –  Nov 06 '18 at 18:07
  • @AaronM Yes and yes. So basically you can just have multiple scripts in the function, take the user input i.e. FRANCE. Then, just make an if, else, or switch statement to pick servernames for the locations. Lots of ways to do this, lots of ways to simplify this. Selecting PrinterName and location is as simple as just writing `Get-Printer | Select < enter whatever properties you want > | Export-Csv / Out-GridView / etc` – Jacob Colvin Nov 06 '18 at 18:11
  • You should spend some time tinkering on your own as it sounds like you aren't very familiar with powershell. – Jacob Colvin Nov 06 '18 at 18:12
  • @JacobColvin Thanks. I'll take a look tomorrow. Yes- Pretty much new to it I know the basics and probably going out my depth with this one. –  Nov 06 '18 at 18:14
  • You just need to put in the time. Feel free to ask here again but only after you've spent a few hours trying to figure it out. That way you'll learn it slowly. After putting in like 50-100 hours things start to become way easier. – Jacob Colvin Nov 06 '18 at 18:17