powershellpowershell-5.1cim

Setting new CIM instance values


I am trying to lock the local admin account on a remote computer that i'm connected to using a CIM session:

$CIMSession = New-CimSession -ComputerName ComputerOne -ErrorAction Stop
$adminCheck = Get-CimInstance -Query "SELECT * FROM Win32_UserAccount WHERE Name='AdminAccount'" -CimSession $CIMSession

When I enumerate all the available CIM class methods available for that object, only one shows up:

PS C:\windows\system32> $adminCheck.CimClass.CimClassMethods

Name   ReturnType Parameters Qualifiers                   
----   ---------- ---------- ----------                   
Rename     UInt32 {Name}     {Implemented, MappingStrings}

...figures this is due to some methods not showing completely, perhaps? So I tried setting it using Invoke-CimMethod.

Tried: $adminCheck.LockOut = $true, out of just trying stuff and to no surprise it doesn't work either. Which would make sense since the instance is just referenced on my computer.

So, in my last attempt I tried:

$adminCheck | Set-CimInstance -Property @{Lockout=$true}
# and
$adminCheck | Set-CimInstance -Property @{Lockout=$true} -CimSession $CIMSession

which didn't work, as well.

Question: Is there no method, to save the newly modified value for that CIM instance property?

I am basing this off of the older Get-WMIObject cmdlet which will allow you to set the property by saving it using the .put() method.

Get-WmiObject -Class Win32_UserAccount -ComputerName ComputerOne -Filter "Name='AdminAccount'" | 
    ForEach-Object -Process {
        $_.Lockout = $true;
        $_.put()
    }

just looking to switch over completely to the newer CIM cmdlets


Solution

  • Regarding the asymmetry of being able to set the Lockout property of Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_UserAccount instances to $false, but not to $true:


    Question: Is there no method, to save the newly modified value for that CIM instance property?

    There is no CIM method as such, but Set-CimInstance is indeed the cmdlet needed to modify properties of CIM instances (whereas calling methods on CIM instances requires Invoke-CimMethod).

    There are two idioms, both of which require running with elevation (with administrative privileges):