WMI and alternate credentials
Here’s an example of using PowerShell to connect to WMI on a remote computer using alternate credentials. Keep in mind that WMI itself doesn’t permit alternate credentials for local connections.
- #
- # ScriptingAnswers.com Essentials
- # copyright 2006 SAPIEN Technologies, Inc.
- #
- # Using alternate credentials with WMI
- #
- # get credential - graphical prompt
- $cred = Get-Credential
- # alternately, only prompt for password:
- # $cred = get-credential "DOMAIN\USERNAME"
- # can't use alt credentials against local machine
- # so be sure to specify a remote computer
- # get all instances of a class
- $wmi = Get-WmiObject Win32_Service -comp "ServerA" -cred $cred
- # execute a query
- $wmi = Get-WmiObject -query "SELECT * FROM Win32_Process" -comp "ServerA" -cred $cred
It’s probably worth noting that the Get-Credential call will prompt you for a password in a dialog box - there’s really no way, for security reasons, to include the password on the command-line. It’s how PowerShell is designed.
Tags: alternate credentials, powershell, wmi










