We help IT Professionals succeed at work.
Get Started

What needs to exist in a PowerShell script to assure Get-WMIObject works?

Last Modified: 2015-02-27
Hello Expert,

My goal is to export the desktop computer names from active directory, place the output into a text file and loop the names through the script to determine the administrator accounts on over 700 desktops.  When I tested the script I received an error:

"Get-WmiObject: The RPC is unavailable
     $AllLocalAccounts = Get-WmiObject -Class ...
   + CategoryInfo: Invalid Operation: (:) ... "

What is the script missing the 'AllLocalAccounts' variable? What is the best syntax?
Set-StrictMode -Version Latest
Set-ExecutionPolicy remotesigned -Force
import-module activedirectory

Get-ADComputer -Filter '*' -SearchBase "OU=Computers,DC=company,DC=com" -Properties Name | Select Name |  Out-File C:\Scripts\DesktopAccounts.txt

	[Alias('cn')][String[]]$ComputerName=(get-content c:\Scripts\DesktopAccounts.txt),
$Obj = @()

Foreach ($Computer in $ComputerName)
		$AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
		-Filter "LocalAccount='$True'" -ComputerName $Computer -Credential $Credential -ErrorAction Stop
		$AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
		-Filter "LocalAccount='$True'" -ComputerName $Computer -ErrorAction Stop
	Foreach($LocalAccount in $AllLocalAccounts)
        Set-ExecutionPolicy remotesigned -Force
        Import-Module activedirectory

        Set-StrictMode -Version Latest

        $Object = New-Object -TypeName PSObject
		$Object|Add-Member -MemberType NoteProperty -Name "User Name" -Value $LocalAccount.Name
		$Object|Add-Member -MemberType NoteProperty -Name "Full Name" -Value $LocalAccount.FullName
		$Object|Add-Member -MemberType NoteProperty -Name "Logon Account Caption" -Value $LocalAccount.Caption
      	$Object|Add-Member -MemberType NoteProperty -Name "Disabled" -Value $LocalAccount.Disabled
		$Object|Add-Member -MemberType NoteProperty -Name "Status" -Value $LocalAccount.Status
		$Object|Add-Member -MemberType NoteProperty -Name "LockOut" -Value $LocalAccount.LockOut
		$Object|Add-Member -MemberType NoteProperty -Name "Password Changeable" -Value $LocalAccount.PasswordChangeable
		$Object|Add-Member -MemberType NoteProperty -Name "Password Expires" -Value $LocalAccount.PasswordExpires
		$Object|Add-Member -MemberType NoteProperty -Name "Password Required" -Value $LocalAccount.PasswordRequired
		$Object|Add-Member -MemberType NoteProperty -Name "Domain" -Value $LocalAccount.Domain
		$Object|Add-Member -MemberType NoteProperty -Name "Description" -Value $LocalAccount.Description
		Foreach($Account in $AccountName)
		    $Obj| Where-Object{$psitem.Name -like "$Account"} 
		    $Obj | Export-csv c:\Scripts\DesktopAccounts.csv

Open in new window

Watch Question
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE