locked
Cleaning 'KeyManagementServiceMachine' in 'SoftwareLicensingProduct' RRS feed

  • Question

  • Hi,

    I have couple of machines that have hard-coded KMS host and am unable to remove it remotely via SLMGR as version is too old.

    I figured I may try via WMI and powershell but I can't - it seems 'KeyManagementServiceMachine' property is read only.

    Do you have any other way to remove server name from above property? I tried with 'ClearKeyManagementServiceMachine' method but cannot get it to work.

    This is what I have. Can anyone point me in the right direction? Thanks.

    BTW. VAMT should have option to clear cached KMS.

    $Servers = Get-Content C:\Temp\servers.csv #Gets Hostnames from CSV
    
    $Servers = $Servers[1..($Servers.count - 1)] #Removes Header
    
    $UserCredential = Get-Credential #prompt for creddentials
    
    ForEach ($server in $Servers) {
    Get-WmiObject -Class SoftwareLicensingProduct -ComputerName $server -Filter "ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f' AND Description like '%KMS%' AND KeyManagementServiceMachine = '10.10.10.10'" | Set-WmiInstance -Arguments @{KeyManagementServiceMachine = ""}
    }
    (Get-WmiObject -Class SoftwareLicensingProduct -ComputerName DWG1PAT02.cantoso.com -Filter "ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f'" -Property KeyManagementServicemachine).ClearKeyManagementServiceMachine();



    • Edited by myckail Tuesday, October 10, 2017 8:31 AM
    Friday, October 6, 2017 12:52 PM

Answers

  • Start by changing this line:

    $Servers = Get-Content C:\Temp\servers.csv #Gets Hostnames from CSV

    To this:

    $Servers = Import-CsvC:\Temp\servers.csv

    and remove this line:

    $Servers =$Servers[1..($Servers.count - 1)] #Removes Header

    Study the documentation first:

    help import-csv -online


    \_(ツ)_/

    Friday, October 20, 2017 8:35 AM

All replies

  • Hi,

    Based on the test in my lab, I recommend you could remove the -Property KeyManagementServiceMachine in the second script and try again to see if the issue remains , the following figure for your reference:


    Also, you could have a try with the method SetKeyManagementServiceMachine(), the following script for your reference:
    $kmsm = Get-WmiObject -Class SoftwareLicensingProduct -ComputerName localhost -Filter "ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f'"
    $kmsm.SetKeyManagementServiceMachine('')

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, October 9, 2017 5:25 AM
  • $Servers = Get-Content C:\Temp\test.csv
    $Servers = $Servers[1..($Servers.count - 1)]
    $UserCredential = Get-Credential
    
    ForEach ($server in $Servers)
    {
        $wmi = Get-WmiObject -Class softwarelicensingservice -ComputerName $server -credential $UserCredential
        foreach ($item in $wmi)
        {
            $item.ClearKeyManagementServiceMachine()
        }
    }

    I have the above and when I run it remotely it won't work. Running it locally, while logged in using same credentials (after some modifications, not to look for test.csv) it works fine.

    Is there any way to invoke this script locally instead of calling remote machine?


    Tuesday, October 10, 2017 10:45 AM
  • Hi,

    Based on my research, if you have configured WinRM on the remote machine already, please have a try with the Invoke-Command cmdlet for running scripts on remote machine locally. The following scripts for your reference, hope it is helpful to you:
    $Servers = Get-Content C:\Temp\test.csv
    $Servers = $Servers[1..($Servers.count - 1)]
    $UserCredential = Get-Credential
    
    Invoke-Command -ComputerName $Servers -ScriptBlock {(Get-WmiObject -Class softwarelicensingservice).ClearKeyManagementServiceMachine()} -Credential $UserCredential

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, October 12, 2017 8:00 AM
  • Hi,

    Just want to confirm the current situations. Have you tried the method provided before?

    If you already tried them or the issue remains after trying them, please don’t hesitate to tell me. I will do more research and try my best to give you helpful suggestions.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, October 17, 2017 7:37 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, October 20, 2017 8:22 AM
  • Start by changing this line:

    $Servers = Get-Content C:\Temp\servers.csv #Gets Hostnames from CSV

    To this:

    $Servers = Import-CsvC:\Temp\servers.csv

    and remove this line:

    $Servers =$Servers[1..($Servers.count - 1)] #Removes Header

    Study the documentation first:

    help import-csv -online


    \_(ツ)_/

    Friday, October 20, 2017 8:35 AM
  • Not very sophicticated but works for me.

    #Gets Hostnames from CSV
    $Servers = Get-Content C:\temp\test.csv
    #Removes Header
    $Servers = $Servers[1..($Servers.count - 1)]
    #Get credentials
    $UserCredential = Get-Credential

    #Get proper activation key
    ForEach ($server in $Servers) {
    $opsys= Get-WmiObject -Query 'SELECT Caption FROM win32_operatingsystem' -ComputerName $server -Credential $UserCredential
    if($opsys.Caption -like "*2016 Standard*"){
    $key="WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY"}
    elseif($opsys.Caption -like "*2016 Datacenter*"){
    $key="CB7KF-BWN84-R7R2Y-793K2-8XDDG"}
    elseif($opsys.Caption -like "*2012 R2 Standard*"){
    $key="D2N9P-3P6X9-2R39C-7RTCD-MDVJX"}
    elseif($opsys.Caption -like "*2012 R2 Datacenter*"){
    $key="W3GGN-FT8W3-Y4M27-J84CP-Q3VJ9"}
    elseif($opsys.Caption -like "*2012 Standard*"){
    $key="XC9B7-NBPP2-83J2H-RHMBY-92BT4"}
    elseif($opsys.Caption -like "*2012 Datacenter*"){
    $key="48HP8-DN98B-MYWDG-T2DCC-8W83P"}
    elseif($opsys.Caption -like "*2008 R2 Standard*"){
    $key="YC6KT-GKW9T-YTKYR-T4X34-R7VHC"}
    elseif($opsys.Caption -like "*2008 R2 Enterprise*"){
    $key="489J6-VHDMP-X63PK-3K798-CPX3Y"}
    elseif($opsys.Caption -like "*2008 R2 Datacenter*"){
    $key="74YFP-3QFB3-KQT8W-PMXWJ-7M648"}
    elseif($opsys.Caption -like "*2008 Standard*"){
    $key="TM24T-X9RMF-VWXK6-X8JC9-BFGM2"}
    elseif($opsys.Caption -like "*2008 Enterprise*"){
    $key="TM24T-X9RMF-VWXK6-X8JC9-BFGM2"}
    elseif($opsys.Caption -like "*2008 Standard*"){
    $key="YQGMW-MPWTJ-34KDK-48M3W-X4Q6V"}
    elseif($opsys.Caption -like "*2008 Datacenter*"){
    $key="7M67G-PC374-GR742-YH8V4-TCBY3"}
    elseif($opsys.Caption -like "*Windows 10 Professional*"){
    $key="W269N-WFGWX-YVC9B-4J6C9-T83GX"}
    elseif($opsys.Caption -like "*Windows 7 Professional*"){
    $key="FJ82H-XT6CR-J8D7P-XQJJ2-GPDD4"}
    else {}

    #Install new product key, clear KMS host and activate.
    $licserv= Get-WmiObject -Class SoftwareLicensingService -ComputerName $server
    $licserv.InstallProductKey($key)
    $licserv.ClearKeyManagementServiceMachine()
    $licserv.RefreshLicenseStatus()

    #If Windows still not activated then create an empty file with hostname
    $licprod= Get-WmiObject -Class SoftwareLicensingProduct -ComputerName $server -Filter "ApplicationID = '55c92734-d682-4d71-983e-d6ec3f16059f' AND Description like '%KMS%' AND PartialProductKey != null" -Property LicenseStatus
    If ($licprod.LicenseStatus -ne 1) {Add-Content C:\temp\KMSfailed.txt "`n$server"} else {}
    }

    Thursday, March 22, 2018 3:44 PM