Quantcast
Channel: Windows Management Infrastructure Blog
Viewing all 66 articles
Browse latest View live

Performing Management tasks using Cimcmdlets [2] – Computer Hardware

$
0
0

As part of this blog series we are sharing PowerShell snippets for a few Computer Management tasks. In this post we will be going over the samples for Computer Hardware management.

The Visual Basic samples for Computer Hardware Management are at: http://msdn.microsoft.com/en-us/library/aa394587(v=vs.85).aspx

The corresponding PowerShell samples are below:

1. Determine how much free memory a computer has:

PS:> # Get Instance of Win32_OperatingSystem

PS:> $os = Get-CimInstance –ClassName Win32_OperatingSystem –Namespace root/cimv2

PS:> # Look for FreePhysicalMemory property

PS:> $os | Select FreePhysicalMemory

2. Determine whether a computer has a DVD drive:

# Look for instances of Win32_CDROMDrive

$insts = Get-CimInstance –ClassName Win32_CDROMDrive –Namespace root/cimv2

# Loop through them and print DeviceID, Description and Name

foreach($inst in $insts)

{

$inst | Select Name, DeviceID, Description

}

3. Determine how much RAM is installed in a computer:

PS:> # Get instance of Win32_ComputerSystem

PS:> $inst = Get-CimInstance –ClassName Win32_ComputerSystem

PS:> # Print Name of the System and the Total Physical Memory

PS:> $inst | Select Name, TotalPhysicalMemory

4. Determine if a computer has more than one processor:

PS:> # Get instance of Win32_ComputerSystem

PS:> $inst = Get-CimInstance –ClassName Win32_ComputerSystem –Namespace root/cimv2

PS:> # Print Name of the system and Number of Processors

PS:> $inst | Select Name, NumberOfProcessors

5. Determine whether a computer has a PCMCIA slot:

# Use the Win32_PCMCIAController class and check the value of the Count property.

# If Count is 0, then the computer has no PCMCIA slots.

$insts = Get-CimInstance –ClassName Win32_PCMCIAController –Namespace root/cimv2

if($insts -ne $null)

{

$count = $insts.Count

if($count -eq $null)

{

$count = 1

}

Write-Host “Number of PCMCIA slots: $count”

}

else

{

Write-Host “Number of PCMCIA slots: 0”

}

6. Identify devices that are not working (those marked with an exclamation point icon in Device Manager)?

# Use the Win32_PnPEntity class

# Use the following clause in your WQL query. WHERE ConfigManagerErrorCode <> 0

$query = “Select * from Win32_PnPEntity WHERE ConfigManagerErrorCode <> 0”

$insts = Get-CimInstance -Query $query

# Loop through all such devices and print their information.

foreach($inst in $insts)

{

$inst | Format-Table -Property @(“ClassGuid”, “Description”, “DeviceID”, “Manufacturer”, “Name”, “PNPDeviceID”, “Service”)

}

7. Determine the properties of the mouse used on computer:

# Use the Win32_PointingDevice class.

# This returns the properties of all pointing devices, not just mouse devices.

$insts = Get-CimInstance –ClassName Win32_PointingDevice

# Loop though instances of Win32_PointingDevice and print the properties.

foreach($inst in $insts)

{

$inst | Format-Table –Property @(“Description”, “DeviceID”, “DeviceInterface”, “DoubleSpeedThreshold”, “Handedness”, “HardwareType”, “InfFileName”, “InfSection”, “Manufacturer”, “Name”, “NumberOfButtons”, “PNPDeviceID”, “PointingType”, “QuadSpeedThreshold”, “Resolution”, “SampleRate”, “Synch”)

}

8. Determine the speed of a processor installed in a computer:

# Use the Win32_Processor class and check the value of the MaxClockSpeed property.

$insts = Get-CimInstance –ClassName Win32_Processor –Namespace root/cimv2

foreach($inst in $insts)

{

$inst | Select ProcessorId, MaxClockSpeed

}

9. Determine whether a computer is a tower, a mini-tower, a laptop, and so on:

# Use the Win32_SystemEnclosure class and check the value of the ChassisType property.

$colChassis = Get-CimInstance –ClassName Win32_SystemEnclosure –Namespace root/cimv2

# Loop though the collection of chassis

foreach($chassis in $colChassis)

{

# Look though the Chassis Types

foreach($item in $chassis.ChassisTypes)

{

Write-Host “Chassis Type: $item”

}

}

10. Get the serial number and asset tag of a computer:

# Use the Win32_SystemEnclosure class, and the properties SerialNumber and SMBIOSAssetTag.

$sytemEnclosures = Get-CimInstance –ClassName Win32_SystemEnclosure

$sytemEnclosureCollection = @()

$sytemEnclosureCollection += $sytemEnclosures

foreach($systemEnclosure in $sytemEnclosureCollection)

{

$systemEnclosure | Select PartNumber, SerialNumber, SMBIOSAssetTag

}

11. Determine what kind of device is plugged into a USB port:

# Use the Win32_USBHub class and check the Description property.

# This property may have a value such as “Mass Storage Device” or “Printing Support”.

$items = Get-CimInstance –ClassName Win32_USBHub –Namespace root/cimv2

foreach($item in $items)

{

$item | Select DeviceID, PNPDeviceID, Description

}

12. Determine how many tape drives are installed on a computer:

# Use the class Win32_TapeDrive class and then count the number of instances received.

# If Count = 0, then no tape drives are installed on the computer.

$insts = Get-CimInstance –ClassName Win32_TapeDrive

$count = 0

if($insts -ne $null)

{

$count = $insts.Count

if($count -eq $null)

{

$count = 1

}

}

Write-Host “Number of tape drives = $count”

 

We will be covering other computer management scenarios in our future posts. If you have any questions please feel free to send them to us.

Important links:

            Processes

 

Thanks

Vaibhav Chugh [MSFT]

Standards Based Management


Performing Management tasks using Cimcmdlets [3] – Operating System

$
0
0

 

As a part of the ongoing blog series “Performing Management tasks using Cimcmdlets” we are sharing PowerShell snippets for a few Computer Management tasks.

In this post we will be going over the PS snippets for Operating System Management. The corresponding Visual Basic samples are at: http://msdn.microsoft.com/en-us/library/aa394596(v=vs.85).aspx

1. Determine if a service pack has been installed on a computer:

PS:> # Get Instance of Win32_OperatingSystem

PS:> # Look for ServicePackMajorVersion and ServicePackMinorVersion properties

PS:> Get-CimInstance –ClassName Win32_OperatingSystem –Namespace root/cimv2 | Select ServicePackMajorVersion, ServicePackMinorVersion

2. Determine when the operating system was installed on a computer:

PS:> # Get Instance of Win32_OperatingSystem

PS:> # Look for InstallDate property

PS:> Get-CimInstance –ClassName Win32_OperatingSystem –Namespace root/cimv2 | Select  InstallDate

3. Determine which version of the Windows operating system is installed on a computer:

PS:> # Get Instance of Win32_OperatingSystem

PS:> # Look for Caption and Version properties

PS:> $os = Get-CimInstance –ClassName Win32_OperatingSystem –Namespace root/cimv2 | Select Caption, Version

4. Determine which folder is the Windows folder (%Windir%) on a computer:

PS:> # Get Instance of Win32_OperatingSystem

PS:> # Look for WindowsDirectory property

PS:> $os = Get-CimInstance –ClassName Win32_OperatingSystem –Namespace root/cimv2 | Select WindowsDirectory

5. Determine what hotfixes have been installed on a computer:

PS:> # Get Instance of Win32_ QuickFixEngineering

PS:> # Look for HotFixID property

PS:> Get-CimInstance –ClassName Win32_QuickFixEngineering –Namespace root/cimv2 | Select HotFixID

 

We will be covering other computer management scenarios in our future posts. If you have any questions please feel free to send them to us.

Important links:

              Processes

              Computer Hardware

 

Thanks

Vaibhav Chugh [MSFT]

Standards Based Management

Performing management tasks using CIM Cmdlets [4] – Files and Folders

$
0
0

 

As a part of the ongoing blog series Performing Management tasks using CIM Cmdlets we are sharing PowerShell snippets for a few computer management tasks.

In this post, we will be going over the PowerShell snippets for file and folder management. The corresponding Visual Basic samples are in the MSDN article, WMI Tasks for Files and Folders.

  • Rename a file:


PS:> $fileName = ‘C:\\Temp\\Test1.txt’

PS:> $newFileName = ‘C:\\Temp\\Test1_NewName.txt’

PS:> $query = "SELECT * FROM CIM_DataFile WHERE Name = ‘$fileName’"

PS:> # Get Instance of CIM_DataFile where file name is C:\\Temp\\Test1.txt

PS:> $file = Get-CimInstance -Query $query -Namespace root/cimv2

PS:> # Change name of file Test1.txt to Test1_NewName.txt

PS:> Invoke-CimMethod -InputObject $file -MethodName ‘Rename’ –Arguments
@{FileName=$newFileName}

ReturnValue PSComputerName
———– ————– 0

 

  • Determine whether users have .MP3 files stored on their computer:


PS:> $query = "SELECT * FROM CIM_DataFile WHERE Extension = ‘mp3’"

PS:> # Get Instances of CIM_DataFile with Extension property set to mp3

PS:> $mp3Files = Get-CimInstance -Query $query -Namespace root/cimv2

PS:> $mp3Files | Select Name

Name
—-
c:\temp\test1.mp3
c:\temp\test2.mp3

 

  • Create shared folders on a computer:


PS:> # Create new share under C:\Temp called TestShare

PS:> Invoke-CimMethod -ClassName Win32_Share -Namespace root/cimv2 –MethodName
‘Create’ -Arguments @{Path=’C:\Temp’; Name=’TestShare’; Type=[uint32]’0′;
MaximumAllowed=[uint32]’25’; Description=’Public share for team’}

ReturnValue PSComputerName
———– ————–
0

 

We will be covering other computer management scenarios in our future posts. If you have any questions, please feel free to send them to us, or post them in the Comments section.

 

Important links:

The complete list of tasks we are covering in this series is in the MSDN article, WMI Tasks for Scripts and Applications.

Links to previous posts:

Processes

Computer Hardware

Operating System

 

Thanks,

Milena Natanov [MSFT]

Standards-Based Management

Enumerating and getting WMI provider class instances with performance in mind

$
0
0

 

Performance of CIM_DataFile and Win32_Directory enumeration depends on the number of files or directories that are being enumerated, and, hence, can be very slow and can take hours.

In following example, we run the Get-CimInstance cmdlet to enumerate all the instances of the class CIM_DataFile. On a brand-new virtual machine, the operation took 38 minutes.

PS:> Measure-Command { Get-CimInstance -ClassName CIM_DataFile -Namespace root/cimv2 }
Days : 0
Hours : 0
Minutes : 38
Seconds : 27

 

How performance can be improved

Here are few alternatives that can improve performance of your PowerShell script if it includes CIM_DataFile or Win32_Directory instance enumeration. These methods can be used on any other class with a large number of instances.

 

Alternative 1: Enumerating instances using a WQL query

You can use a WQL query to narrow down enumeration to single instances. When you use WQL query, the Get-CimInstance cmdlet executes faster.

For example, the following query returns result immediately:

PS:> # Build a WQL query, which will return C:\ directory
PS:> $query = “Select * From Win32_Directory Where Name = ‘C:'”
PS:> # Get Win32_Directory instance using WQL query
PS:> $instances = Get-CimInstance -Query $query -Namespace root/cimv2
PS:> $instances | Select Name
Name : c:

If WQL query uses the LIKE operator, expect the query to be as slow as Get-CimInstance without a WQL query.

PS:> # Build a WQL query, which will return all directories under C:\
PS:> $query = “Select * From Win32_Directory Where Name LIKE ‘C:*'”
PS:> # Get Win32_Directory instances matching the query filter
PS:> Measure-Command { $instances = Get-CimInstance -Query $query -Namespace root/cimv2 }
Days : 0
Hours : 0
Minutes : 2
Seconds : 34

 

 

Alternative 2: Enumerating instances using Get-CimInstance with local instance

This method can only be used when the instance key property value is known.

PS:> # Get CIM_DataFile class
PS:> $class = Get-CimClass -ClassName CIM_DataFile -Namespace root/cimv2
PS:> # Create hash table containing instance’s key property and its value
PS:> $keyPropertyHash = @{}
PS:> $keyPropertyHash.Add(‘Name’, ‘C:\Temp\Test.txt’)
PS:> # Create a new CIM instance in memory for a CIM_DataFile class with the key property @{ ‘Name’=’C:\Temp\Test.txt’ }
PS:> $localInstance = New-CimInstance -CimClass $class -Property $keyPropertyHash –ClientOnly
PS:> #Get Test.txt file instance using $localInstance as an input object
PS:> $instance = Get-CimInstance –InputObject $localInstance
PS:> $instance | fl Name
Name : c:\temp\test.txt

 

Summary

When you’re enumerating instances in a class with large number of instances:

  • Use WQL queries if enumeration can be scoped down.
  • Run Get-CimInstance with the local instance if the key property value is known.
  • Avoid using queries with the LIKE operator if possible.
  • If you need full enumeration, plan for the time overhead that’s added to your script.

 

Important links:

Link to related post:

Files and Folders

 

Thanks,

Milena Natanov [MSFT]

Standards-Based Management

WS-Man (Web Services for Management) 1.2 Published

$
0
0

The DMTF recently published the Web Services for Management (WS-Man) standard version 1.2. This release of the WS-Man specification clarifies support for the latest encryption protocols, which has been required by organizations and governmental agencies such as the US Government NIST program.

WS-Man is a SOAP-based protocol that can be used with any transport, although it is primarily used with HTTP and HTTPS.  The older versions of the WS-Man specification made specific references to older versions of TLS (Transport Layer Security) that have proven to be insufficient.  The updated version more clearly decouples the protocol and the transport to ensure that interoperability is not dependent on specific versions of encryption algorithms, including TLS.  There is no functional change with the updated WS-Man 1.2 specification.  

Microsoft, an active member of the DMTF, contributed and edited the updated WS-Man specification. The Microsoft implementation of WS-Man is known as Windows Remote Management (WinRM), and has been a part of Windows since Windows 7 / Windows Server 2008 R2.  The WinRM implementation of WS-Man conforms with the latest NIST requirements described in NIST Special Publication 800 -52r1. WinRM leverages HTTP.SYS, which implements and enables configuration of the most recent and industry-standard secure transport protocol layer, as described in these two articles:

· Configuring WINRM for HTTPS

· The Configuring WinRM to Use HTTPS section of Configuration and Security

Note that if HTTPS is not used, WinRM still encrypts the payload over HTTP by default unless explicitly set to not use encryption.

WinRM has been, and should continue to be relied on in secure environments that are configured to meet the government and industry recommended cryptographic algorithms. Due to the lack of clarity in the previous releases of the WS-Man specification, some users were confused about whether or not WS-Man and WinRM supported recent and more secure implementations of the Transport-Layer Security (TLS), which it does. While this has not been an issue for the Microsoft implementation in WinRM, the WS-Man version 1.2 specification update has addressed that, and removed the confusing elements.

Steve Lee
Principal Engineering Manager
Windows Server

Keith Bankston
Senior Program Manager
Windows Server

Move to PowerShell for WinRM Management

$
0
0

As most people are aware, PowerShell provides two ways of managing the Windows Remote Management (WinRM) infrastructure: the WSMan Provider and the WS-Management Cmdlets. (As you are no doubt aware, WinRM is the Windows implementation of the WS-Management standard, which is why these cmdlets are named Ws-Management.) Together, the WSMan Provider and WS-Management cmdlets give administrators the ability to enable, configure, manage, and test WinRM easily from within PowerShell, and are the best way for building scripts to manage WinRM.

Prior to the release of the WS-Management cmdlets, the only way to perform some WinRM management tasks was to use the WinRM.VBS scripting interface and WinRM.CMD. However, with the proven success of the PowerShell cmdlets, it is time to move away from those older tools. No development is planned for either WinRM.CMD or WinRM.VBS, and while they are still a part of Windows (to support backwards compatibility), no new scripts or development should be based on them.  

This is a small part of the long-established strategy to move to PowerShell for management of the Windows platforms. PowerShell has become the Windows scripting choice for IT Professionals, and is being more broadly adopted by developers with the addition of Desired State Configuration.

The WS-Management Cmdlets and WSMan Provider give PowerShell a complete mechanism for both local and remote management of WinRM, CredSSP, and PowerShell Sessions that is unavailable from VBScript. For more information on these, you will find links to both the WS-Management cmdlets and the WS-Man provider here on TechNet. There is also a very good article titled “Understanding PowerShell Remote Management” that covers these topics quite well.

To reiterate the key point: while WinRM.VBS and WinRM.CMD will ship in Windows to support backwards compatibility, new scripts developed to manage WinRM should be created using the WSMan Provider and the PowerShell WS-Management cmdlets.

Viewing all 66 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>