Error in PowerShell

Try to use the Get_WmiObject in a powershell but it returns with an error "Access is denied". Window .NET Framework 4.5 is installed. Any idea ? The powershell has been run using administrator privilege.

Thx
PowerShellScript.png
AXISHKAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello ThereSystem AdministratorCommented:
First, you have to run PS as Administrator. If you are already logged as Administrator, you still have to right-click on PS and choose Run As Administrator.
Second, you need to have a full permission to the file.
Sajen JoseCommented:
You may want to check the firewall settings

Windows Firewall blocking the connection

a) Go to Windows Firewall -> Advanced Settings
b) Select Inbound rules
c) Create a New Rule
d) In the Rule Type -> Click Pre-Defined and then select Windows Management Instrumentation (WMI)
e) Ensure the Windows Management Instrumentation (WMI-In) is checked
f)  Allow the connection and click on Finish.
Sajen JoseCommented:
Also you may want to enable remote access

1. Check if the WinRM Service is running by running get-service winrm cmdlet

2. If it is not running, then enable it by typing -> enable-PSRemoting -force

3. Add access permission to the remove host -> winrm /s winrm/config/client '@{TrustedHosts="remote pc name or ip address"}'

4. run the following cmdlet to reconfigure the winrm service -> winrm quickconfig.
Check Out How Miercom Evaluates Wi-Fi Security!

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom on how WatchGuard's Wi-Fi security stacks up against the competition plus a LIVE demo!

footechCommented:
Sorry, but none of the above are correct.
It doesn't matter if you run in a elevated session.  You would receive a different error if the firewall was blocking communication.  WinRM (and PS Remoting) have nothing to do with access via WMI.

When querying remote computers, unless you have changed the permissions, only accounts that are administrators on the remote machine are allowed.  You will need to provide credentials for the Get-WmiObject command (using the -credential parameter).  Example:
$cred = Get-Credential
Get-WmiObject Win32_Logicaldisk -filter "drivetype = 3" -ComputerName "comp1" -Credential $cred

Open in new window


Alternatively, if you started the console using Run As Different User, and supplied the credentials for an appropriate account, then the script would use that.
footechCommented:
It's also possible to use Get-CimInstance instead of Get-WmiObject.  By default the CIM cmdlets will use PS Remoting for communication, but they can also be configured to use WMI/DCOM.  The syntax is more convoluted than Get-WmiObject to use DCOM.
$cred = Get-Credential
$so = New-CimSessionOption -Protocol Dcom
$s = New-CimSession -ComputerName comp1 -Credential $cred -SessionOption $so
Get-CimInstance win32_logicaldisk -filter "drivetype = 3" -CimSession $s
$s | Remove-CimSession

Open in new window


For PsRemoting/WsMan/WinRm, the syntax would be:
$cred = Get-Credential
$s = New-CimSession -ComputerName comp1 -Credential $cred
Get-CimInstance win32_logicaldisk -filter "drivetype = 3" -CimSession $s
$s | Remove-CimSession

Open in new window

No CimSessionOption needed, unless you wanted to configure other parameters.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DBAduck - Ben MillerPrincipal ConsultantCommented:
Access Denied is related to not having the privileges to access that resource. You will need to address the permission you have on the Computer. If you are not in Administrators, then you will not be able to get WMI information.
AXISHKAuthor Commented:
$cred = Get-Credential

Is it possible to pass the user ID and password in the batch file, rather than ask interactively ?

Thx
footechCommented:
This isn't batch...

It's generally a bad idea to hard code in passwords.
$password = "password1" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PsCredential("mydomain\myuser", $password)

Open in new window


There are many previous questions here which cover a separate method of storing the password in an encrypted form in a file.  A caveat with that technique is that you can't copy the file to another machine and decrypt it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.