dazza007 Posted April 23, 2015 Posted April 23, 2015 I am trying to get a powershell script (stolen) from Arthur on here and it takes ages to run and is not removing any printers when set as a script on login This is the script: [color=#333333]Get-WmiObject -Class Win32_Printer -Namespace 'root\CIMV2' -Filter "SystemName='\\\\[/color][color=#FF0000]OLDSERVERNAMEHERE[/color][color=#333333]'" -ErrorAction SilentlyContinue | ForEach { $_.delete() }[/color] I tried diagnosing this by running and this is taking ages to run. [color=#333333]Get-WmiObject -Class Win32_Printer[/color] Any ideas The powershell version is S C:\> $PSVersionTable Name Value ---- ----- PSVersion 4.0 WSManStackVersion 3.0 SerializationVersion 1.1.0.1 CLRVersion 4.0.30319.18063 BuildVersion 6.3.9600.16406 PSCompatibleVersions {1.0, 2.0, 3.0, 4.0} PSRemotingProtocolVersion 2.2 and the restriction policy is unrestricted
Arthur Posted April 23, 2015 Posted April 23, 2015 it takes ages to run and is not removing any printers when set as a script on login Try running it as a startup script instead. 1
halbaradkenafin Posted April 23, 2015 Posted April 23, 2015 and the restriction policy is unrestricted I'd change that to RemoteSigned for security purposes. You'll still be able to run any scripts you make on site but it covers up a potential area for malware and such to get in.
dazza007 Posted April 23, 2015 Author Posted April 23, 2015 Sorry my bad, it is running as a startup script not a login script I tried calling it on a machine from powershell and it worked. Now it is hanging, I am stumped!
halbaradkenafin Posted April 23, 2015 Posted April 23, 2015 Try using Get-CimInstance -Classname Win32_Printer? I did a quick bit of testing and found that this works for removing a printer: Get-CimInstance -ClassName Win32_Printer | Foreach { if ($_.Name -eq "Fax") { Remove-CimInstance -InputObject $_ }} So that works fine for removing a specific printer (no idea why I had a fax installed as a printer but it was useful for testing) but did require running as Administrator. So removing all the printers would just be as simple as: Get-CimInstance -ClassName Win32_Printer | Remove-CimInstance
dazza007 Posted April 24, 2015 Author Posted April 24, 2015 Ok so my headache is persisting! What I was trying to achieve was to remove all the printer connections from the user profile that is from an old server. Is there a tried and tested method of doing this?
halbaradkenafin Posted April 24, 2015 Posted April 24, 2015 (edited) It would depend on how they were assigned to the device but it should be possible in Powershell. I'll do some testing and get back to you soon. Edit: Turns out it's pretty easy to do with Powershell: Get-CimInstance Win32_Printer | Where {$_.SystemName -eq "\\servername" } | Remove-CimInstance Should do it but you'll need to run as Administrator. You can do the same thing with Get-WmiObject and Remove-WmiObject but still need Run As Administrator apparently. If you're running it from a script file it should be possible to do but it's not something I've looked up how to do, start up script might override that as it will run as SYSTEM (I think) but login script would need something put in place as even with Domain Admin access I had to open a prompt with Run As Admin. Edited April 24, 2015 by halbaradkenafin
dazza007 Posted April 24, 2015 Author Posted April 24, 2015 (edited) Thanks for your help! I have just setup remote powershell onsite to get to the root of this problem so I ran the following, which might explain why nothing is happening. Despite many printers deployed nothing there! C:\> winrs -r: powershell Get-WMIObject -Class Win32_Printer Location : Name : Send To OneNote 2010 PrinterState : 0 PrinterStatus : 3 ShareName : SystemName : Location : Name : Microsoft XPS Document Writer PrinterState : 0 PrinterStatus : 3 ShareName : SystemName : Location : Name : Fax PrinterState : 0 PrinterStatus : 3 ShareName : SystemName : C:\> winrs -r: powershell Get-CimInstance Win32_Printer Name ShareName SystemName PrinterState PrinterStat Location us ---- --------- ---------- ------------ ----------- -------- Send To OneNo... 0 3 Microsoft XPS... 0 3 Fax 0 3 Edited April 24, 2015 by dazza007
halbaradkenafin Posted April 24, 2015 Posted April 24, 2015 Get-CimInstance and Get-WmiObject both support remoting already using the -ComputerName property.
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