Power Shell: Difference between revisions

From vwiki
Jump to navigation Jump to search
m (→‎Using WMI: Added Win32_userAccount)
m (Added "Credentials")
Line 1: Line 1:
== Using WMI ==
== Credentials ==
When running commands that require a connection to a remote machine its useful to be able to store a user/pass combination so that you aren't repeatedly prompted every time you run a command.  Create a credential object, then supply that in place of a username in a command
 
<pre>
PS H:\> $cred = Get-Credential
 
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
PS H:\> Get-WMIObject -query "SELECT * FROM Win32_OperatingSystem" -credential $cred -computer 159.104.224.167
 
SystemDirectory : C:\WINDOWS\system32
Organization    : TF
BuildNumber    : 3790
RegisteredUser  : TF
SerialNumber    : 69712-640-3560061-45009
Version        : 5.2.3790
</pre>
 
 
== WMI ==


Cmdlet for using WMI via PowerShell is '''Get-WMIObject''', which has an alias of '''gwmi''', for example...
Cmdlet for using WMI via PowerShell is '''Get-WMIObject''', which has an alias of '''gwmi''', for example...

Revision as of 13:57, 10 November 2009

Credentials

When running commands that require a connection to a remote machine its useful to be able to store a user/pass combination so that you aren't repeatedly prompted every time you run a command. Create a credential object, then supply that in place of a username in a command

PS H:\> $cred = Get-Credential

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
PS H:\> Get-WMIObject -query "SELECT * FROM Win32_OperatingSystem" -credential $cred -computer 159.104.224.167

SystemDirectory : C:\WINDOWS\system32
Organization    : TF
BuildNumber     : 3790
RegisteredUser  : TF
SerialNumber    : 69712-640-3560061-45009
Version         : 5.2.3790


WMI

Cmdlet for using WMI via PowerShell is Get-WMIObject, which has an alias of gwmi, for example...

PS H:\> Get-WMIObject -query "Select * from Win32_OperatingSystem"

SystemDirectory : C:\WINDOWS\system32
Organization    :
BuildNumber     : 2600
RegisteredUser  : TF
SerialNumber    : 76487-OEM-0011903-00102
Version         : 5.1.2600
PS H:\> Get-WMIObject -query "SELECT * FROM Win32_UTCTime"

__GENUS          : 2
__CLASS          : Win32_UTCTime
__SUPERCLASS     : Win32_CurrentTime
__DYNASTY        : Win32_CurrentTime
__RELPATH        : Win32_UTCTime=@
__PROPERTY_COUNT : 10
__DERIVATION     : {Win32_CurrentTime}
__SERVER         : L-0STRUTTS1
__NAMESPACE      : root\cimv2
__PATH           : \\L-0STRUTTS1\root\cimv2:Win32_UTCTime=@
Day              : 27
DayOfWeek        : 4
Hour             : 10
Milliseconds     :
Minute           : 0
Month            : 8
Quarter          : 3
Second           : 1
WeekInMonth      : 5
Year             : 2009
PS H:\> Get-WMIObject -query "Select * from Win32_OperatingSystem" -credential sysmgr -computer ukpgemon2

SystemDirectory : C:\WINDOWS\system32
Organization    : Thomson Financial
BuildNumber     : 3790
RegisteredUser  : Dell Image Rev 1
SerialNumber    : 69712-640-3560061-45321
Version         : 5.2.3790


PS H:\> Get-WMIObject -query "Select * from Win32_userAccount WHERE Domain = 'OCM-WCS1' AND Name = 'pptp_ypos'" -credential administrator -computer 159.104.224.167

AccountType : 512
Caption     : OCM-WCS1\pptp_ypos
Domain      : OCM-WCS1
SID         : S-1-5-21-2453442427-2400495818-1845097998-1073
FullName    : Ypos-Consulting GmbH
Name        : pptp_ypos

TechNet article: Windows PowerShell Best Inventory Tool Ever!