Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to use Powershell to get the print shares from a remote print server.

I'm using:

Get-WmiObject Win32_Share -computerName "print-server"

I'm getting an "access denied" error:

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:14
+ Get-WmiObject <<<<  Win32_Share -computerName "print-server"
    + CategoryInfo          : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

I don't get why I can's see the shares, though, as if I connect through My Computer (e.g. \\print-server\) I can see all the print shares fine.

Any ideas?

Thanks.

Ben

share|improve this question
    
What's that famous quote (originally said of XML)-- something like "When you say 'I have a problem. I think I'll solve it with PowerShell!' you end up with two problems." –  Evan Anderson Jul 18 '10 at 21:48
    
ugh! I love powershell! I'd rephrase it as "When you say 'I have a problem. I think I'll solve it with PowerShell!' you end up with two problems, how to solve the original problem and how many other scripts can you replace" –  Jim B Nov 29 '11 at 2:00
    
What are you trying to do with the script though? You mention login script, are you trying to map a printer for a user? As mentioned you won't be able to utilize wmi to get the share names of a remote print server without admin credentials. –  Eric C. Singer Dec 30 '11 at 22:36

2 Answers 2

Yeah you can see them in Windows Explorer but get an access denied with your Powershell command because you're trying to execute a WMI query on the remote machine, for which you need valid credentials.

If you wanted to store the credentials so that you could run that command non-interactively, you could convert the password and store it as a securestring in a file, but that's just obfuscation and any intelligent snoop would be able to decode it.

Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File secret.txt

$pass = Get-Content secret.txt | ConvertTo-Securestring
$creds = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist "domain\admin",$pass

Maybe try abandoning the WMI query route altogether. Maybe try a good ole' COM object like so:

$network = New-Object -Com WScript.Network
$network.AddWindowsPrinterConnection($printerShare)
share|improve this answer

Try passing credentials with Get-WmiObject as follows.

$Credential = Get-Credential

(You will get a prompt for credentials)

Get-WmiObject Win32_Share -ComputerName 'PRINT-SERVER' -Credential $Credential

share|improve this answer
    
Or Get-WmiObject Win32_Share -ComputerName 'PRINT-SERVER' -Credential (Get-Credential) for a one-liner –  fletcher Jul 18 '10 at 21:02
    
Trouble is, I want to run this in a login script, so don't want user to have to enter password. The bit I don't get is why I get access denied, even though I can see all the shares in Explorer...? –  Ben Jul 18 '10 at 21:03
    
Are you a local admin on "print-server"? You don't need to be an admin to see the print shares via explorer, but you need them if you are running a WMI query. –  fenster Jul 19 '10 at 21:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.