Exploring the PowerShell DSC xPendingReboot Resource

While building a DSC Demo for the new job this week I got the chance to explore using many of the “new” Resources that have been released.  One of those Resources is the xPendingReboot which I am going to talk about here, because the documentation wasn’t very clear (to me anyways after having been away from DSC for a long time) on how to use it properly.

The TechNet article on the Resource and an article by the Scripting Guy can be found at the links below.

https://gallery.technet.microsoft.com/scriptcenter/xPendingReboot-PowerShell-b269f154

http://blogs.technet.com/b/heyscriptingguy/archive/2014/10/15/use-powershell-dsc-to-check-pending-reboot.aspx

If you just look at it, you would assume you could do something like this to check for a reboot:

Configuration TestPendingReboot{

    Param(
        [string]$ComputerName='localhost'
    )

    Import-DSCResource -ModuleName xPendingReboot

    Node $ComputerName{

        xPendingReboot PreTest{
            Name = "Check for a pending reboot before changing anything"
        }
        
    }

}

However, you would be wrong! If we create the .MOF file for this Configuration and run this against the local system (which has a reboot pending after a computer rename), the system doesn’t actually reboot itself, it just notifies you that a reboot is pending.

TestPendingReboot -OutputPath C:\Scripts\TestPendingReboot
Start-DscConfiguration -Wait -Force -Verbose -Path .\TestPendingReboot

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer WIN-74EKGETUJS6 with user sid S-1-5-21-2712606644-3520791333-1947032181-500.
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ Start  Set      ]
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ Start  Resource ]  [[xPendingReboot]PreTest]
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ Start  Test     ]  [[xPendingReboot]PreTest]
VERBOSE: [WIN-74EKGETUJS6]:                            [[xPendingReboot]PreTest] A pending reboot was found for PendingComputerRename.
VERBOSE: [WIN-74EKGETUJS6]:                            [[xPendingReboot]PreTest] Setting the DSCMachineStatus global variable to 1.
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ End    Test     ]  [[xPendingReboot]PreTest]  in 0.4020 seconds.
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ Start  Set      ]  [[xPendingReboot]PreTest]
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ End    Set      ]  [[xPendingReboot]PreTest]  in 0.0150 seconds.
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ End    Resource ]  [[xPendingReboot]PreTest]
VERBOSE: [WIN-74EKGETUJS6]:                            [] A reboot is required to progress further. Please reboot the system.
WARNING: [WIN-74EKGETUJS6]:                            [] A reboot is required to progress further. Please reboot the system.
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ End    Set      ]    in  3.2110 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 3.763 seconds

Well, that’s great and all but it didn’t reboot the machine like I needed it to. Looking at those examples, maybe I need to add the LocalConfigurationManager piece to make this work?

Configuration TestPendingReboot{

    Param(
        [string]$ComputerName='localhost'
    )

    Import-DSCResource -ModuleName xPendingReboot

    Node $ComputerName{

        xPendingReboot PreTest{
            Name = "Check for a pending reboot before changing anything"
        }

        LocalConfigurationManager{
            RebootNodeIfNeeded = $True
        }
        
    }

}

When you build this Configuration you will immediately notice you get a localhost.mof as well as a localhost.meta.mof . The Meta.mof is a result of making a change to the Local Configuration Manager (LCM) and should be a hint that something needs to be done with it :). The TechNet article uses a RebootNodeifNeeded = ‘True’ instead of the Boolean $True, which is not correct. If you try to build the Configuration using ‘True’ you get this error:

ConvertTo-MOFInstance : System.ArgumentException error processing property 'RebootNodeIfNeeded' OF TYPE 'LocalConfigurationManager': Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only 
Boolean values and numbers, such as $True, $False, 1 or 0.
At line:285 char:16
+     $aliasId = ConvertTo-MOFInstance $keywordName $canonicalizedValue
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Write-Error], InvalidOperationException
    + FullyQualifiedErrorId : FailToProcessProperty,ConvertTo-MOFInstance
Errors occurred while processing configuration 'TestPendingReboot'.
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:3189 char:5
+     throw $errorRecord
+     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (TestPendingReboot:String) [], InvalidOperationException
    + FullyQualifiedErrorId : FailToProcessConfiguration

I am going to ignore the localhost.meta.mof file for now and just try this again to see what happens. And the exact same thing happens. If you are wondering if moving the LocalConfigurationManager section ahead of the xPendingReboot section matters or will help, it won’t. You actually need to change the LCM setting on the computer before starting the Configuration, because right now it is set to this. Notice the RebootNodeIfNeeded section at the bottom:

PS C:\Scripts> Get-DscLocalConfigurationManager


ActionAfterReboot              : ContinueConfiguration
AllowModuleOverWrite           : False
CertificateID                  : 
ConfigurationDownloadManagers  : {}
ConfigurationID                : 
ConfigurationMode              : ApplyAndMonitor
ConfigurationModeFrequencyMins : 15
Credential                     : 
DebugMode                      : {NONE}
DownloadManagerCustomData      : 
DownloadManagerName            : 
LCMCompatibleVersions          : {1.0, 2.0}
LCMState                       : PendingReboot
LCMVersion                     : 2.0
MaxPendingConfigRetryCount     : 
StatusRetentionTimeInDays      : 10
PartialConfigurations          : {}
RebootNodeIfNeeded             : False
RefreshFrequencyMins           : 30
RefreshMode                    : PUSH
ReportManagers                 : {}
ResourceModuleManagers         : {}
PSComputerName    

You do that by using this command:

Set-DscLocalConfigurationManager -Path .\TestPendingReboot -Verbose
VERBOSE: Performing the operation "Start-DscConfiguration: SendMetaConfigurationApply" on target "MSFT_DSCLocalConfigurationManager".
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendMetaConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredStateConfiguratio
n'.
VERBOSE: An LCM method call arrived from computer WIN-74EKGETUJS6 with user sid S-1-5-21-2712606644-3520791333-1947032181-500.
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ Start  Set      ]
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ Start  Resource ]  [MSFT_DSCMetaConfiguration]
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ Start  Set      ]  [MSFT_DSCMetaConfiguration]
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ End    Set      ]  [MSFT_DSCMetaConfiguration]  in 0.0160 seconds.
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ End    Resource ]  [MSFT_DSCMetaConfiguration]
VERBOSE: [WIN-74EKGETUJS6]: LCM:  [ End    Set      ]    in  0.0160 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Set-DscLocalConfigurationManager finished in 0.09 seconds.

Get-DscLocalConfigurationManager


ActionAfterReboot              : ContinueConfiguration
AllowModuleOverWrite           : False
CertificateID                  : 
ConfigurationDownloadManagers  : {}
ConfigurationID                : 
ConfigurationMode              : ApplyAndMonitor
ConfigurationModeFrequencyMins : 15
Credential                     : 
DebugMode                      : {NONE}
DownloadManagerCustomData      : 
DownloadManagerName            : 
LCMCompatibleVersions          : {1.0, 2.0}
LCMState                       : Ready
LCMVersion                     : 2.0
MaxPendingConfigRetryCount     : 
StatusRetentionTimeInDays      : 10
PartialConfigurations          : {}
RebootNodeIfNeeded             : True
RefreshFrequencyMins           : 30
RefreshMode                    : PUSH
ReportManagers                 : {}
ResourceModuleManagers         : {}
PSComputerName                 : 

Now when we start the Configuration, we get the exact same result as above, plus an automatic reboot!
PendingReboot






If you want everything in one file, you can find it here.

Moving SCOM 2012 R2 Data Warehouse Database to New SQL Server Cluster

This is the second of two posts in regards to moving Operations Manager 2012 R2 Databases.  The first post about moving the OperationsManager database can be found here.  I won’t rehash any of that information but will instead just get right into the How To portion.  The TechNet article for moving the SCOM DataWarehouse Database can be found here.

  1. Stop the following services on OPSMGR01 and change their type to Manual.  Otherwise they will restart and cause you problems.
    1. System Center Data Access
    2. System Center Management Configuration
  2. Stop the following services on OPSMGR02 and change their type to Manual.  Otherwise they will restart and cause you problems.
    1. System Center Data Access
    2. System Center Management Configuration
  3. On OLDSQLServer, use SQL Server Management Studio to create a full backup of the data warehouse database. The default name is OperationsManagerDW. We recommend that you also back up the associated master database.
    1. Copy backup file to \\SQLCLUSTERNODE1\SHARE
  4. Open SQL Management Studio on SQLCLUSTERNODE1 and connect to SQLCLUSTER\SCOMDW Instance
    1. Restore OperationsManagerDW Database using SQL
  5. Backup registry on OPSMGR01
  6. Backup registry on OPSMGR02
  7. Update Registry on OPSMGR01
    1. Regedit
    2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup
    3. For each of the following keys, double-click the name, change the value to the hostname of the SQL Server-based computer now hosting the operational database, and then click OK to save your changes.
      1. DatabaseWarehouseDBServerName: SQLCLUSTER\SCOMDW
      2. DatabaseName: OperationsManagerDW
  8. Update Registry on OPSMGR02
    1. Regedit
    2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup
    3. For each of the following keys, double-click the name, change the value to the hostname of the SQL Server-based computer now hosting the operational database, and then click OK to save your changes.
      1. DatabaseWarehouseDBServerName: SQLCLUSTER\SCOMDW
      2. DatabaseName: OperationsManagerDW
  9. Login to the server you are using for reporting off of the SCOM Data Warehouse
    1. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\System Center Operations Manager\3.0\Reporting,\ DWDBInstance double-click the name and change the value to the hostname of the SQL Server-based computer now hosting the operations manager DW database, and then click OK to save your change.
      1. In my case that would be SQLCLUSTER\SCOMDW
    1. Start the System Center Data Access Service on OPSMGR01 associated with the reporting server. This is needed to access the reports page
    2. On reporting server, change the connection strings.
      1. Open a browser and go to the reporting webpage
      2. Click Show Details and then click Data Warehouse Main. Change the Connection String to contain the new data warehouse server name, and then click Apply. SQLCLUSTER\SCOMDW.
      3. Test the connection
      4. Click Application monitoring, and then click .NET monitoring.
      5. Click AppMonitoringSource.
      6. On the AppMonitoringSource page, click Properties and change Connection string to contain SQLCLUSTER\SCOMDW, and then click Apply.
      7. Test the connection
      8. Close the browser.
    3. On SQLCLUSTERNODE1 update the OperationsManager database table. (Note that this is NOT the DataWarehouse instance!)
      1. Open SQL Server Management Studio. SQLCLUSTER\SCOM
      2. Expand Databases, OperationsManager, and Tables.
      3. Right-click dbo.MT_Microsoft$SystemCenter$DataWarehouse, and then click Edit Top 200 Rows.
      4. Change the value in the MainDatabaseServerName_2C77AA48_DB0A_5D69_F8FF_20E48F3AED0F column to SQLCLUSTER\SCOMDW
    4. Update the OperationsManager database for Application Performance Monitoring functionality.
      1. Right-click dbo.MT_Microsoft$SystemCenter$DataWarehouse$AppMonitoring, and then click Edit Top 200 Rows.
      2. Change the value in the MainDatabaseServerName_5C00C79B_6B71_6EEE_4ADE_80C11F84527A column to SQLCLUSTER\SCOMDW
      3. Do the same for the following tables.
      4. Right-click dbo. MT_Microsoft$SystemCenter$DataWarehouse$AppMonitoring_Log and then click Edit Top 200 Rows. Change the value of column Post_MainDatabaseServerName_5C00C79B_6B71_6EEE_4ADE_80C11F84527A to SQLCLUSTER\SCOMDW
      5. Right-click dbo.MT_Microsoft$SystemCenter$DataWarehouse_Log and then click Edit Top 200 Rows. Change the value of column.Pre_MainDatabaseServerName_2C77AA48_DB0A_5D69_F8FF_20E48F3AED0F TO SQLCLUSTER\SCOMDW.
      6. Close SQL Server Management Studio.
    5. On SQLCLUSTERNODE1, update the member database.
      1. Open SQL Server Management Studio and connect to SQLCLUSTER\SCOMDW instance.
      2. Expand Databases, OperationsManagerDW, and Tables.
      3. Right-click dbo.MemberDatabase, and then click Edit Top 200 Rows.
      4. Change the value in the ServerName column to reflect the name of the new SQL Server, SQLCLUSTER\SCOMDW
      5. Close SQL Server Management Studio.
    6. Update security credentials on SQLCLUSTER\SCOMDW
      1. All of my security credentials carried over and I had no issues.  However, your environment may be completely different.  Definitely make sure to read this section in the TechNet documentation
    7. Stop SQL Services relating to SCOM on OLDSQLServer
    8. Open PowerShell.  Type Write-Host “Bye Kitty” to sacrifice a kitten
    9. Restart OPSMGR01 and OSPMGR02 (this was to ensure registry setting changes were picked up)
    10. Start the following services on OPSMGR01 and change their startup type to Automatic
      1. System Center Data Access
      2. System Center Management Configuration
    11. Start the following services on OPSMGR02 and change their startup type to Automatic
      1. System Center Data Access
      2. System Center Management Configuration
    12. Spam refresh OperationsManager event log on OPSMGR01.  Hopefully you see lots of informational messages and no errors about connections to the SQL Server.

To verify a successful move of the Operations Manager Data Warehouse Database:

  1. Verify that you can successfully run a report from the console.
  2. Ensure that the health state of all management servers in the management group are Healthy.
  3. If the health state of any management server is Critical, open Health Explorer, expand Availability – <server name>, and then continue to expand until you can navigate to Data Warehouse SQL RS Deployed Management Pack List Request State. Check the associated events to determine if there is an issue accessing the data warehouse database.
  4. Check operating system events:
    1. Open the operating system’s Event viewer. Navigate to Event Viewer, and then to Operations Manager.
    2. In the Operations Manager pane, search for events with a Source of Health Service Module and a Category of Data Warehouse.
  1. The move was successful if event number 31570, 31558, or 31554 exists.
  2. There is an issue accessing the data warehouse database if event numbers 31563, 31551, 31569, or 31552 exists.
  3. Check events in Operations Manager:
    1. In the Operations console, select Monitoring.
    2. Navigate to Monitoring, Operations Manager, Health Service Module Events, and then to Performance Data Source Module Events.
    3. Search the Performance Data Source Module Events pane for events with a Date and Time that is later than the move.

 

Moving SCOM 2012 R2 Database to New SQL Server Cluster

When our System Center environment (VMM, Orchestrator, SCOM, DPM) was originally setup all the databases were installed on a single SQL Server VM (but in separate instances).  For a variety of reasons I made the decision that our System Center databases needed to at least be in a cluster configuration.  Luckily I stumbled across this excellent guide by MVP Paul Keely about configuring a SQL 2012 Cluster with Always On to be used for System Center databases.  I am not going to cover any of that here, this post is just going to be about how to move the Operations Manager database once the new cluster is built.

First, I need to go on a rant about just plain poor timing on my part.  If you don’t care to read it, skip to the next paragraph.  There is an article published by Microsoft that talks about how to move the Operational Database.  If you look at that article you will notice that it was last updated on January 19th.  That’s awesome.  You know what’s not awesome?  I first attempted to move the Operational Database on January 17th.  It failed miserably.  Why?  Because the article I was working off of didn’t include a second registry key that needed to be modified.  That’s 6 hours of my  life I will never get back.

In my environment I have two Operations Manager Management servers, in this article they are named OPSMGR01 and OPSMGR02.  When I wrote up my plan for this move I duplicated some sections that needed to be done on both servers and I kept that the same in this article.  For the SQL Server name, I used the name of the new SQL Cluster as SQLCLUSTER followed by the instance name.  If you are moving the databases to a single server you would just use the server name followed by the instance name.

  1. Stop the following services on OPSMGR01 and change their type to Manual.  Otherwise they will restart and cause you problems.
    1. System Center Data Access
    2. System Center Management Configuration
  2. Stop the following services on OPSMGR02 and change their type to Manual.  Otherwise they will restart and cause you problems.
    1. System Center Data Access
    2. System Center Management Configuration
  3. Create full backup of OperationsManager Database on OLDSQLServer using SQL
    1. Copy backup file to \\SQLCLUSTERNODE1\SHARE
  4. Open SQL Management Studio on SQLCLUSTERNODE1 and connect to SQLCLUSTER\SCOM Instance
    1. Restore OperationsManager Database using SQL
  5. Delete backup file from \\SOMEFILESHARE (Needed the space for the massive SCOM Data Warehouse Database backup that was coming next)
  6. Backup Registry on OPSMGR01
  7. Backup Registry on OPSMGR02
  8. Update Registry on OPSMGR01
    1. Regedit
    2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup
    3. For each of the following keys, double-click the name, change the value to the hostname of the SQL Server-based computer now hosting the operational database, and then click OK to save your changes.
      1. DatabaseName: SQLCLUSTER\SCOM
      2. DatabaseServerName: OperationsManager
    4. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\System Center\2010\Common\Database and repeat step 3. (This was the aforementioned missing step)
  9. Update Registry on OPSMGR02
    1. Regedit
    2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Setup
    3. For each of the following keys, double-click the name, change the value to the hostname of the SQL Server-based computer now hosting the operational database, and then click OK to save your changes.
      1. DatabaseName: SQLCLUSTER\SCOM
      2. DatabaseServerName: OperationsManager
    4. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\System Center\2010\Common\Database and repeat step 3.
  10. Edit the following file on OPSMGR01
    1. Browse to %ProgramFiles%\System Center 2012\Operations Manager\Server\ConfigService.config
    2. Backup existing file (copy and rename to .old and place somewhere safe)
    3. Open Notepad as Administrator and open the file in Step 1.
    4. In the <Category> tags named “Cmdb” and “ConfigStore”, change the value for ServerName to the name of the new SQL server.
    5. SQLCLUSTER\SCOM
    6. Save changes and close
  11. Edit the following file on OPSMGR02
    1. Browse to %ProgramFiles%\System Center 2012\Operations Manager\Server\ConfigService.config
    2. Backup existing file (copy and rename to .old and place somewhere safe)
    3. Open Notepad as Administrator and open the file in Step 1.
    4. In the <Category> tags named “Cmdb” and “ConfigStore”, change the value for ServerName to the name of the new SQL server.
    5. SQLCLUSTER\SCOM
    6. Save changes and close
  12. Update the operational database with the new database server name on SQLCLUSTERNODE1
    1. Open SQL Server Management Studio. Connect to SQLCLUSTER\SCOM instance.
    2. Expand Databases, OperationsManager, and Tables.
    3. Right-click dbo.MT_Microsoft$SystemCenter$ManagementGroup, and then click Edit Top 200 Rows.
    4. Change the value in the SQLServerName_6B1D1BE8_EBB4_B425_08DC_2385C5930B04 column to SQLCLUSTER\SCOM
      1. The GUID part in your database isn’t going to match the one above.  It’s ok!
    5. Save the change.
  13. On SQLCLUSTERNODE1, update the operational database with the new database server name to specify the location of the Application Performance Monitoring tables
    1. Open SQL Server Management Studio. Connect to SQLCLUSTER\SCOM instance
    2. Expand Databases, OperationsManager, and Tables.
    3. Right-click dbo.MT_Microsoft$SystemCenter$OpsMgrDB$AppMonitoring, and then click Edit Top 200 Rows.
    4. Change the value in the MainDatabaseServerName_5C00C79B_6B71_6EEE_4ADE_80C11F84527A column to SC-SQLCLUS-SCOM\SCOM
      1. The GUID part in your database isn’t going to match the one above.  It’s ok!
    5. Save the change.
  14. Update security credentials on SQLCLUSTER\SCOM
    1. All of my security credentials carried over and I had no issues.  However, your environment may be completely different.  Definitely make sure to read this section in the TechNet documentation
  15. Execute these SQL commands on new OperationsManager database instance:
    1. sp_configure ‘show advanced options’,1
    2. reconfigure
    3. sp_configure ‘clr enabled’,1
    4. reconfigure
  16. Run the following SQL query (If you added the OperationsManager database to AlwaysOn before this step you can skip this step. I ran through all of this and added it to AlwaysOn once I knew for sure it was working on the new SQLCLUSTER):
    1. SELECT is_broker_enabled FROM sys.databases WHERE name=’OperationsManager’
      1. If the result of this query was an is_broker_enabled value of 1, skip this step. Otherwise, run the following SQL queries:
    2. ALTER DATABASE OperationsManager SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    3. ALTER DATABASE OperationsManager SET ENABLE_BROKER
    4. ALTER DATABASE OperationsManager SET MULTI_USER
  17. Stop SQL Services relating to SCOM on OLDSQLServer
  18. Open PowerShell.  Type Write-Host “Bye Kitty” to sacrifice a kitten
  19. Restart OPSMGR01 and OSPMGR02 (this was to ensure registry setting changes were picked up)
  20. Start the following services on OPSMGR01 and change their startup type to Automatic
    1. System Center Data Access
    2. System Center Management Configuration
  21. Start the following services on OPSMGR02 and change their startup type to Automatic
    1. System Center Data Access
    2. System Center Management Configuration
  22. Spam refresh OperationsManager event log on OPSMGR01.  Hopefully you see lots of informational messages and no errors about connections to the SQL Server.
  23. Start OperationsManager console.  Check stuff

That’s it!

Creation of the VMM Resource Group Failed

While trying to install VMM as a Highly Available following the steps in this article, I kept running into the same issue over and over and over again.  The installation would start, run for a few minutes, and then fail and rollback with this error at the end:  “Creation of the VMM resource group VMM failed.Ensure that the group name is valid, and cluster resource or group with the same name does not exist, and the group name is not used in the network.”

I first thought it was because I had pre-configured the DNS entry, but even after removing the DNS entry and verifying that neither cluster node could resolve it anymore the installation still failed.  Next I rebooted both nodes of the cluster.  The installation still failed.  My next thought is maybe I can’t use VMM as the HA VMM name so I used XXXVMM and sure enough it worked.  Now, whether or not that is because I had the VMM DNS entry created previously and it screwed something up, or because you really can’t use the VMM name, I don’t know.

At this point it said setup completed successfully, but with warnings.

 

Starting the clustered VMM service XXXVMM failed.Ensure that the user has permission, the VMM service is installed properly, and cluster resources can be brought online.A service connection point (SCP) could not be registered in Active Directory Domain Services (AD DS) for the VMM management server.Run “D:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\setup\ConfigureSCPTool.exe -install XXXVMM.nfm.com XXX\XXXVMM$” in a command window and then check AD DS. If an SCP is not registered, VMM consoles on other computers will not be able to connect to this VMM management server and deploying a Hyper-V host to a bare-metal computer will not work.

I ran the setup as a domain administrator, and am logged into the cluster node as this same domain administrator.

Looking at the Cluster event logs, this is the error I get when I try to manually bring the service online:

Failed to bring the resource VMM Service XXXVMM online.  Error code 0x80071736 The Resource failed to come online due to the failure of one or more resource providers.

Looking at Services, the System Center Virtual Machine Manager Agent service is running, but the System Center Virtual Machine Manager service is not running, and if I try to start it, it fails with the error.

In the Event Log, I find this error entry which seems pretty useful.

Cluster network name resource ‘XXXVMM’ failed to create its associated computer object in domain ‘nfm.com’ during: Resource online.

The text for the associated error code is: A constraint violation occurred.

Please work with your domain administrator to ensure that:

– The cluster identity ‘VMM-HV-CLUS01$’ has Create Computer Objects permissions. By default all computer objects are created in the same container as the cluster identity ‘VMM-HV-CLUS01$’.

– The quota for computer objects has not been reached.

– If there is an existing computer object, verify the Cluster Identity ‘VMM-HV-CLUS01$’ has ‘Full Control’ permission to that computer object using the Active Directory Users and Computers tool.

While looking in AD for this object, I also stumbled across a VMM computer object (who the hell knows where that come from) so I deleted that object as well.  I am nearly 100% certain that is what caused my installation using VMM as my VMM Cluster Name to fail.

At the very bottom of this article it outlines how to troubleshoot issues with Cluster AD Accounts.  The instructions are only slightly different for Server 2012, and it tells you how to grant only the permissions that you need.

With that done, I removed the VMM installation and tried it again, with the original VMM Cluster name (just VMM) and it worked fine, with no errors!

 

 

 

 

PowerShell DSC Journey – Day 22

Alright, after my little fiasco yesterday I need to do a little re-configuring of my Configuration because of course DSC will not allow a Plain text password.

PS C:\Scripts> C:\Users\jacob.benson\SkyDrive\PowerShell\DSC\TestSCVMMHardware.ps1
ConvertTo-MOFInstance : System.InvalidOperationException error processing property 'Credential' OF TYPE 'cSCVMM_Hardware': Converting and storing encrypted passwords as plain text is not recommended for security reasons. If you understand the risks, you 
can add a property named “PSDscAllowPlainTextPassword” with a value of “$true” to your DSC configuration data, for each node where you want to allow plain text passwords. For more information about DSC configuration data, see the TechNet Library topic, 
http://go.microsoft.com/fwlink/?LinkId=386620.
At C:\Users\jacob.benson\SkyDrive\PowerShell\DSC\TestSCVMMHardware.ps1:13 char:9
+   cSCVMM_Hardware
At line:180 char:16
+     $aliasId = ConvertTo-MOFInstance $keywordName $canonicalizedValue
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Write-Error], InvalidOperationException
    + FullyQualifiedErrorId : FailToProcessProperty,ConvertTo-MOFInstance
Errors occurred while processing configuration 'TestSCVMMHardware'.
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2203 char:5
+     throw $errorRecord
+     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (TestSCVMMHardware:String) [], InvalidOperationException
    + FullyQualifiedErrorId : FailToProcessConfiguration

Here is the new version of the Configuration.

$ConfigData = @{
    AllNodes = @(
        @{
            NodeName = "localhost"
            PSDSCAllowPlainTextPassword = $True
            }
    )
}

Configuration TestSCVMMHardware
{

    param
    (
        [PSCredential]$Credential = (Get-Credential)
    )

    Import-DscResource -Module cSCVMM

    node $AllNodes.NodeName
    {
        cSCVMM_Hardware MyHardwareProfile
        {
            VMMServer = "MY-VMM-SERVER1"
            CPUCount = 2
            DVDDrive = $True
            Ensure = "Present"
            Name = "Jacobs Profile"
            VMNetwork = "Server Traffic"
            Credential = $Credential
       }

    }

}

TestSCVMMHardware -ConfigurationData $ConfigData

Now, let’s try to run this and see what breaks. And. Nothing breaks. I am literally speechless. Seriously.

PS C:\Scripts> C:\Users\jacob.benson\SkyDrive\PowerShell\DSC\TestSCVMMHardware.ps1
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:


    Directory: C:\Scripts\TestSCVMMHardware


Mode                LastWriteTime     Length Name                                                                                                                                                                   
----                -------------     ------ ----                                                                                                                                                                   
-a---         6/24/2014   2:27 PM       1770 localhost.mof 

Well. Here goes nothing. And I forgot to change something back in .psm1 file when I was messing around with it yesterday that caused this entire thing to blow up. I will spare you all the red text but here is the error.

PS C:\Scripts> Start-DscConfiguration -Wait -Verbose -Path .\TestSCVMMHardware
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredState
Configuration'.
VERBOSE: An LCM method call arrived from computer OM808-IT-293 with user sid S-1-5-21-738551990-92959840-526660263-26386.
VERBOSE: [MyComp]: LCM:  [ Start  Set      ]
Importing module cSCVMM_Hardware failed with error - At C:\Program Files\WindowsPowerShell\Modules\cSCVMM\DscResources\cSCVMM_Hardware\cSCVMM_Hardware.psm1:10 char:67
+ ...      $Credential = [System.Management.Automation.PSCredential]::Empty

With that fixed I try to run it, and I don’t get any errors, but clearly I have something to fix with my Test-TargetResource function because it just skipped running Set-TargetResource.

PS C:\Scripts> Start-DscConfiguration -Wait -Verbose -Path .\TestSCVMMHardware
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredState
Configuration'.
VERBOSE: An LCM method call arrived from computer MyComp with user sid S-1-5-21-738551990-92959840-526660263-26386.
VERBOSE: [MyComp]: LCM:  [ Start  Set      ]
VERBOSE: [MyComp]: LCM:  [ Start  Resource ]  [[cSCVMM_Hardware]MyHardwareProfile]
VERBOSE: [MyComp]: LCM:  [ Start  Test     ]  [[cSCVMM_Hardware]MyHardwareProfile]
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] VMMServer is MY-VMM-SERVER1
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Hardware Profile Name is Jacobs Profile
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager
.R2Aliases.ps1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManager
LibraryClientCleanup.ps1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmach
inemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmach
inemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTransfer.psd1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading 'Assembly' from path 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\Microsoft.BackgroundIntelligen
tTransfer.Management.Interop.dll'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading 'FormatsToProcess' from path 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTransfer.Format.ps
1xml'.
VERBOSE: [MyComp]: LCM:  [ End    Test     ]  [[cSCVMM_Hardware]MyHardwareProfile]  in 7.3940 seconds.
VERBOSE: [MyComp]: LCM:  [ Skip   Set      ]  [[cSCVMM_Hardware]MyHardwareProfile]
VERBOSE: [MyComp]: LCM:  [ End    Resource ]  [[cSCVMM_Hardware]MyHardwareProfile]
WARNING: The specified ConfigurationModeFrequencyMins was over-written to a multiple of RefreshFrequencyMins
VERBOSE: [MyComp]: LCM:  [ End    Set      ]    in  7.7470 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 4.705 seconds

So, let’s see if we can figure out what’s going on. I am pretty sure this section is the problem.

    $result = $false

    #Check to see if Credential and VMMServer is valid
    $ResourceVMMServer = Get-SCVMMServer -ComputerName $VMMServer -Credential $Credential
        If($ResourceVMMServer)
        {
            Return $true
        }
        Else
        {
            Return $false
        }

I set the $result to $false, then tested for the $VMMServer, and returned $True, so DSC was like “oh hey, everything is gravy.” Fail on my part. Let’s fix this. I already know if the Credential or VMMServer is invalid that it will fail, so I just need to check to make sure $ResourceVMMServer exists and then do the rest of my checks. I am pretty sure this is going to fail for a couple of reasons, but I am going to test this anyways in the interest of full disclosure :).

    $ResourceVMMServer = Get-SCVMMServer -ComputerName $VMMServer -Credential $Credential
        If($ResourceVMMServer)
        {
            Write-Verbose "VMMServer was found and is $ResourceVMMServer"

            try{

                $HWProfile = Get-SCHardwareProfile -VMMServer $VMMServer | Where-Object Name -eq $Name -ErrorAction Stop
                Write-Verbose "Hardware Profile is $HWProfile"

                If($Ensure -eq "Present")
                {
                    If($DVDDrive -ne $HWProfile.VirtualDVDDrives.Enabled){Return $False}
                    If($CPUCount -ne $HWProfile.CPUCount){Return $False}
                    If($VMNetwork -ne $HWProfile.VirtualNetworkAdapters.VMNetwork){Return $False}

                    Return $True
                }
                Else
                {
                    Return $False

                }
            }
            catch [System.Management.Automation.ActionPreferenceStopException]
            {
                ($Ensure -eq 'Absent')
            }
        }

I run several of my tests that I expect to return both $True and $False. I made a few changes and added one line, so here is the new and improved section of my code.

    $ResourceVMMServer = Get-SCVMMServer -ComputerName $VMMServer -Credential $Credential
        If($ResourceVMMServer)
        {
            Write-Verbose "VMMServer was found and is '$VMMServer'"

            try{

                $HWProfile = Get-SCHardwareProfile -VMMServer $VMMServer | Where-Object Name -eq $Name -ErrorAction Stop
                If($HWProfile){Write-Verbose "Hardware Profile is $HWProfile"}

                If($Ensure -eq "Present")
                {
                    If($DVDDrive -ne $HWProfile.VirtualDVDDrives.Enabled){Return $False}
                    If($CPUCount -ne $HWProfile.CPUCount){Return $False}
                    If($VMNetwork -ne $HWProfile.VirtualNetworkAdapters.VMNetwork){Return $False}

                    Return $True
                }
                Else
                {
                    Return $False

                }
            }

So, let’s try this again! HOLY BUCKETS IT WORKED! Minus, one small issue.

PS C:\Scripts> Start-DscConfiguration -Wait -Verbose -Path .\TestSCVMMHardware
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredState
Configuration'.
VERBOSE: An LCM method call arrived from computer MyComp with user sid S-1-5-21-738551990-92959840-526660263-26386.
VERBOSE: [MyComp]: LCM:  [ Start  Set      ]
VERBOSE: [MyComp]: LCM:  [ Start  Resource ]  [[cSCVMM_Hardware]MyHardwareProfile]
VERBOSE: [MyComp]: LCM:  [ Start  Test     ]  [[cSCVMM_Hardware]MyHardwareProfile]
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] VMMServer is MY-VMM-SERVER1
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Hardware Profile Name is Jacobs Profile
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager
.R2Aliases.ps1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManager
LibraryClientCleanup.ps1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmach
inemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmach
inemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] VMMServer was found and is 'MY-VMM-SERVER1'
VERBOSE: [MyComp]: LCM:  [ End    Test     ]  [[cSCVMM_Hardware]MyHardwareProfile]  in 1.8714 seconds.
VERBOSE: [MyComp]: LCM:  [ Start  Set      ]  [[cSCVMM_Hardware]MyHardwareProfile]
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] VMMServer is MY-VMM-SERVER1
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Hardware Profile Name is Jacobs Profile
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager
.R2Aliases.ps1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManager
LibraryClientCleanup.ps1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmach
inemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmach
inemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] Checking if the Hardware Profile Jacobs Profile exists
VERBOSE: [MyComp]:                            [[cSCVMM_Hardware]MyHardwareProfile] The Hardware Profile was not found.  Creating new Hardware Profile Jacobs Profile
VERBOSE: [MyComp]: LCM:  [ End    Set      ]  [[cSCVMM_Hardware]MyHardwareProfile]  in 3.7302 seconds.
VERBOSE: [MyComp]: LCM:  [ End    Resource ]  [[cSCVMM_Hardware]MyHardwareProfile]
WARNING: The specified ConfigurationModeFrequencyMins was over-written to a multiple of RefreshFrequencyMins
VERBOSE: [MyComp]: LCM:  [ End    Set      ]    in  5.8367 seconds.
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 3.555 seconds

dsc62

Now, the one issue there is that no VMNetwork was set. Probably because there is no network adapter, which I am guessing I forgot to include in my Set-TargetResource. Let’s take a look.

                        If($VMNetwork -ne $ResourceHWProfile.VirtualNetworkAdapters.VMNetwork)
                        {
                            Write-Verbose "VMNetwork should be $VMNetwork.  Setting VMNetwork"
                            Get-SCVirtualNetworkAdapter -VMMServer $VMMSErver -HardwareProfile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
                            Write-Verbose "VMNetwork set to $VMNetwork"
                        }

Yeah, that’s not going to work. I need to create the adapter first. Turns out it’s easier than I thought it would be. Just kidding, I can’t use the parameter for $VMNetwork, it needs to be a different type.

                        If($VMNetwork -ne $ResourceHWProfile.VirtualNetworkAdapters.VMNetwork)
                        {
                            Write-Verbose "VMNetwork should be $VMNetwork.  Setting VMNetwork"
                            New-SCVirtualNetworkAdapter -VMMServer $VMMServer -HardwareProfile $ResourceHWProfile -VMNetwork $VMNetwork
                            Write-Verbose "VMNetwork set to $VMNetwork"
                        }
New-SCVirtualNetworkAdapter : Cannot bind parameter 'VMNetwork'. Cannot convert the "Server Traffic Virtual Switch" value of type "System.String" to type 
"Microsoft.SystemCenter.VirtualMachineManager.VMNetwork".
At line:1 char:98
+ ... reProfile "Jacobs Profile" -VMNetwork "Server Traffic Virtual Switch"
+                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-SCVirtualNetworkAdapter], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.NewNICCmdlet

Which opens a whole new can of worms because I need to check to make sure that is a valid VM Network somewhere. For the purposes of this, I am going to assume that if it should be present, it is a valid name. Actually I lied. We aren’t going to do that, because that opens up a giant mess when it comes to creating a new Virtual Network.

After banging away on this for about the last 30 minutes I am going to stop here for the day and pick it up again tomorrow. I am currently stuck on getting the right object type from Get-SCVMNetwork to pass to……..oh hell…wait a minute. Just kidding! Kidding again. I have a moment of genius! And this is also where I hate Virtual Machine Manager anymore. Only thing good to say is that I learned a hell of a lot more than I ever wanted to know about VMM cmdlets this afternoon.

So, let me delete the Hardware Profile and run my Configuration again. The network adapter didn’t get created. My brain is exhausted. I’m done for today. For real this time.

PowerShell DSC Journey – Day 19

Alright, so in my last post I was able to resolve the issue with my Custom Resource not showing up under Get-DSCResource (because as usual I was doing something dumb).

Proof!

PS C:\Scripts> Get-DscResource

ImplementedAs   Name                      Module                         Properties                                        
-------------   ----                      ------                         ----------                                        
Binary          File                                                     {DestinationPath, Attributes, Checksum, Content...
PowerShell      SCVMM_Hardware            cSCVMM                         {Name, VMMServer, CPUCount, DependsOn...}         

Now, let’s try and write a Configuration! Look ma, no errors!

Configuration TestSCVMMHardware
{

    Import-DscResource -Module cSCVMM

}

Let’s build this out for a test.

Configuration TestSCVMMHardware
{

    Import-DscResource -Module cSCVMM

    node localhost
    {
        SCVMM_Hardware MyHardwareProfile
        {
            VMMServer = "MDC-SC-VMM01"
            CPUCount = 2
            DVDDrive = $True
            Ensure = "Present"
            Name = "Jacobs Profile"
            VMNetwork = "Server Traffic"
       }

    }

}

TestSCVMMHardware

I run this Configuration and the .MOF gets created.

    Directory: C:\Scripts\TestSCVMMHardware


Mode                LastWriteTime     Length Name                                                                                                                                                                   
----                -------------     ------ ----                                                                                                                                                                   
-a---         6/19/2014   2:14 PM       1294 localhost.mof 

When I run this configuration I immediately encounter two errors.

PS C:\Scripts> Start-DscConfiguration -Path .\TestSCVMMHardware -Wait -Verbose
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft/Windows/DesiredState
Configuration'.
VERBOSE: An LCM method call arrived from computer MyComp with user sid S-1-5-21-738551990-92959840-526660263-26386.
VERBOSE: [MyComp]: LCM:  [ Start  Set      ]
VERBOSE: [MyComp]: LCM:  [ Start  Resource ]  [[SCVMM_Hardware]MyHardwareProfile]
VERBOSE: [MyComp]: LCM:  [ Start  Test     ]  [[SCVMM_Hardware]MyHardwareProfile]
VERBOSE: [MyComp]:                            [[SCVMM_Hardware]MyHardwareProfile] VMMServer is MY-VMM-SERVER
VERBOSE: [MyComp]:                            [[SCVMM_Hardware]MyHardwareProfile] Hardware Profile Name is Jacobs Profile
VERBOSE: [MyComp]:                            [[SCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.
R2Aliases.ps1'.
VERBOSE: [MyComp]:                            [[SCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerL
ibraryClientCleanup.ps1'.
VERBOSE: [MyComp]:                            [[SCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachi
nemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: [MyComp]:                            [[SCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachi
nemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: [MyComp]:                            [[SCVMM_Hardware]MyHardwareProfile] Loading module from path 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTransfer.psd1'.
VERBOSE: [MyComp]:                            [[SCVMM_Hardware]MyHardwareProfile] Loading 'Assembly' from path 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\Microsoft.BackgroundIntelligent
Transfer.Management.Interop.dll'.
VERBOSE: [MyComp]:                            [[SCVMM_Hardware]MyHardwareProfile] Loading 'FormatsToProcess' from path 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTransfer.Format.ps1
xml'.
You cannot contact the VMM management server. The credentials provided have insufficient privileges on MY-VMM-SERVER.
Ensure that your account has access to the VMM management server MY-VMM-SERVER, and then try the operation again.
    + CategoryInfo          : ReadError: (:) [], CimException
    + FullyQualifiedErrorId : 1605,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.GetSCHWConfigCmdlet
    + PSComputerName        : localhost
 
VERBOSE: [MyComp]: LCM:  [ End    Test     ]  [[SCVMM_Hardware]MyHardwareProfile]  in 4.6853 seconds.
The PowerShell DSC resource cSCVMM_Hardware threw one or more non-terminating errors while running the Test-TargetResource functionality. These errors are logged to the ETW channel called 
Microsoft-Windows-DSC/Operational. Refer to this channel for more details.
    + CategoryInfo          : InvalidOperation: (:) [], CimException
    + FullyQualifiedErrorId : NonTerminatingErrorFromProvider
    + PSComputerName        : localhost
 
The SendConfigurationApply function did not succeed.
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : MI RESULT 1
    + PSComputerName        : localhost
 
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 3.203 seconds

The first issue crossed my mind literally as I was hitting enter to start the Configuration. That is, am I going to need a credential variable to pull this off because not just anyone can connect to a VMM Server. This error came from running PowerShell as Administrator. When I run PowerShell as my elevated account (which has access) this is what happens.

PS C:\Scripts> Start-DscConfiguration -Path .\TestSCVMMHardware -Wait -Verbose
VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = SendConfigurationApply,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName' = root/Microsoft
/Windows/DesiredStateConfiguration'.
The WS-Management service cannot process the request. The WMI service returned an 'access denied' error. 
    + CategoryInfo          : PermissionDenied: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : HRESULT 0x80338104
    + PSComputerName        : localhost
 
VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 10.109 seconds

I am going to try this (although if it works this not a valid solution as far as I am concerned), and this shouldn’t work but I am going to try it anyways.

Configuration TestSCVMMHardware
{

    param
    (
        [pscredential]$Credential = (Get-Credential)
    )

    Import-DscResource -Module cSCVMM

    node localhost
    {
        SCVMM_Hardware MyHardwareProfile
        {
            VMMServer = "MDC-SC-VMM01"
            CPUCount = 2
            DVDDrive = $True
            Ensure = "Present"
            Name = "Jacobs Profile"
            VMNetwork = "Server Traffic"
       }

    }

}

TestSCVMMHardware -Credential (Get-Credential)

And it did exactly what I thought it should do (which is nice for a change).

You cannot contact the VMM management server. The credentials provided have insufficient privileges on MY-VMM-SERVER1.
Ensure that your account has access to the VMM management server MY-VMM-SERVER1, and then try the operation again.
    + CategoryInfo          : ReadError: (:) [], CimException
    + FullyQualifiedErrorId : 1605,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.GetSCHWConfigCmdlet
    + PSComputerName        : localhost

So, I am going to need a $Credential parameter. That sounds like a good place to start tomorrow 🙂

PowerShell DSC Journey – Day 18

Yesterday I finished up modifying the Set-TargetResource function and doing all the tests and it seems to be working exactly the way that I want. The next step today is to turn this into a module, import it, write a DSC Configuration and see if it actually works.

I already have my SCVMM_Hardware.psm1 file, so I just need to add a module manifest file.

PS C:\scripts> New-ModuleManifest -Path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psd1' -Author "Jacob Benson" -PowerShellVersion 4.0 -RootModule 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psm1' -Verbose
VERBOSE: Performing the operation "Creating the "C:\Program Files\WindowsPowerShell\Modules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psd1" module manifest file." on target "C:\Program Files\WindowsPowerShell\Mod
ules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psd1".

Looking at the other DSC Resources, none of them specify a root module, so I guess we will see if this breaks it or not. They also have multiple resources associated with each module, so that could be part of the reason as well.

This article on TechNet says to “Finally, use the New-ModuleManifest cmdlet to define a .psd1 file for your custom resource module. When you invoke this cmdlet, reference the script module (.psm1) file”, but the module manifest they show has the root module as ”, which doesn’t match up with what they are saying. I am just going to try it this way and see what happens. Because that’s what I do. Actually, I am going to change something up here to prepare for my other SCVMM Resource.

  • Rename the root folder to cSCVMM
  • Create a DSCResources folder underneath
  • Create a cSCVMM_Hardware folder, move my .psm1 and schema.mof files into this folder
  • Create the module manifest and place it in the root cSCVMM folder

Round 2.

PS C:\scripts> New-ModuleManifest -Path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1' -Author "Jacob Benson" -PowerShellVersion 4.0 -RootModule 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\SCVMM_Hardware\SCVMM_Hardware.psm1' -Verbose
VERBOSE: Performing the operation "Creating the "C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1" module manifest file." on target "C:\Program Files\WindowsPowerShell\Modules\DSCResource
s\cSCVMM\cSCVMM.psd1".

So far so good! (No errors anyways). I am going to restart ISE and see if it loads the module or what happens. This is where I had so much trouble in my previous post. And I ran into the same issue. If I say Get-Module -Name cSCVMM nothing happens. If I do Get-Module -ListAvailable it shows up in the list. So first thing, I am going to get rid of the root module path and see what happens.

PS C:\Scripts> New-ModuleManifest -Path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1' -Author "Jacob Benson" -PowerShellVersion 4.0 -Verbose
VERBOSE: Performing the operation "Creating the "C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1" module manifest file." on target "C:\Program Files\WindowsPowerShell\Modules\DSCResource
s\cSCVMM\cSCVMM.psd1".

Same thing. Move it out of the DSCResources folder, same thing.

Looking at the other resources, they have a CLR version and a description, so let’s just try that (although I am sure that has nothing to do with it).

New-ModuleManifest -Path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1' -Author "Jacob Benson" -PowerShellVersion 4.0 -ClrVersion 4.0 -Description "Module with DSC Resources for System Center Virtual Machine Manager" -Verbose

Long story short, turns out (once again) that I just don’t know what I am doing. Once you import it by name, you can then get the information on it. Duh.

PS C:\Scripts> Import-Module cSCVMM -Verbose
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DSCResources\cSCVMM\cSCVMM.psd1'.

PS C:\Scripts> Get-Module -Name cSCVMM

ModuleType Version    Name                                ExportedCommands                                                                                                                                          
---------- -------    ----                                ----------------                                                                                                                                          
Manifest   1.0        cSCVMM                                                                                                                                                                                        

So, now that I wasted a bunch of time on that, let’s see if I can remember how to build a Configuration :).

First issue I run into is, when I do Import-DSCResource -Module cSCVMM it acts like it has no idea what the hell that is. So, uh, what do I do about that?

Configuration TestSCVMMHardwareProfile
{
    Import-DscResource -Module xNetworking
    Import-DscResource -Module cSCVMM

    node localhost
    {

    }
}

So, yeah. My .psm1 file is exporting the commands, so that shouldn’t be a problem. I guess first thing first, let’s run Get-DSCResource and see what happens.

PS C:\Scripts> Get-DSCResource
Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.
Parameter name: length"
At C:\windows\system32\windowspowershell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2336 char:13
+             $moduleName = $moduleFolder.FullName.Substring($folder.Le ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

I get that error, but then all of the DSCResources are listed (except for mine).

Further investigation reveals this.

PS C:\Scripts> Get-DscResource -Name cSCVMM
Exception calling "Substring" with "2" argument(s): "Length cannot be less than zero.
Parameter name: length"
At C:\windows\system32\windowspowershell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2336 char:13
+             $moduleName = $moduleFolder.FullName.Substring($folder.Le ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException
 
CheckResourceFound : The term 'cSCVMM' is not recognized as the name of a Resource.
At C:\windows\system32\windowspowershell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2428 char:13
+             CheckResourceFound $name $resources
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,CheckResourceFound

Alright, so that’s fun. I am going to backup here and try something referencing the steps in this article.

I create a “new” DSCResource using my properties. And guess what? Even then it doesn’t show up as a DSC Resource. So, that’s good right? I don’t think so. That article makes it seem like it should be pretty simple, so I don’t know what’s going on.

Next step, I am going to test this on WMF 4.0 (I am using WMF 5.0 Preview). And the same thing happens there. So that’s good I guess. I really have no idea what is going on here. Completely. Puzzled.

Time to stop for the day. Hopefully I think of something before tomorrow as of what to try next.

Edit: I was able to resolve this issue with the help of Don Jones on the PowerShell.org forums.

When I first created the Resource what I did was this:

New-xDscResource -Name cSCVMM_Hardware -Property $DVDDrive, $VMNetwork, $CPUCount, $Ensure, $Name, $VMMServer -FriendlyName "SCVMM_Hardware" -ClassVersion 1.0 -Path 'C:\Program Files\WindowsPowerShell\Modules\'

What I should have done was this:
New-xDscResource -Name cSCVMM_Hardware -Property $DVDDrive, $VMNetwork, $CPUCount, $Ensure, $Name, $VMMServer -FriendlyName “SCVMM_Hardware” -ClassVersion 1.0 -Path ‘C:\Program Files\WindowsPowerShell\Modules\cSCVMM’

Since I didn’t specify the Module name it just created the DSC Resources folder and that messed up everything. Lesson learned!

PowerShell DSC Journey – Day 17

Alright. So yesterday I worked through some issues in the Set-TargetResource Function and left off with a big one still remaining. Namely, that even though I have Ensure = Present in my test of the Function, it is running through the Ensure = Absent portion of the script if the Hardware Profile already exists, which doesn’t make any sense.

Here is the section of the Function in question.

#Check to see if Hardware Profile exists
        Write-Verbose "Checking if the Hardware Profile $Name exists"
        $HWProfile = Get-SCHardwareProfile -VMMServer $VMMServer

        If($HWProfile.Name -match $Name)
        {
            Write-Verbose "The Hardware Profile was found"
            #Set Variables for the Hardware Profile
            $ResourceHWProfile = Get-SCHardwareProfile -VMMServer $VMMServer | Where-Object Name -eq $Name

            #Set this variable for use later
            $HWProfileID = $ResourceHWProfile.ID

            #If Hardware Profile Should not exist, remove it
            Write-Verbose "Ensure set to $Ensure"
            If($Ensure = "Absent")
            {
                Write-Verbose "Hardware Profile $Name Should be $Ensure"
                Get-SCHardwareProfile -VMMServer $VMMServer -ID $HWProfileID | Remove-SCHardwareProfile
                Write-Verbose "Hardware Profile $Name is $Ensure"
            }

And if I run this Test with a Hardware Profile that already exists, this is the output that I get.

Set-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
PS C:\USERS\jacob.benson\Downloads\physdiskwrite-0.5.3> Set-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
VERBOSE: VMMServer is MY-VMM-SERVER1
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile exists
VERBOSE: The Hardware Profile was found
VERBOSE: Ensure set to Present
VERBOSE: Hardware Profile DSCWEB Hardware Profile Should be Absent
(Then it displays the Hardware Profile)
VERBOSE: Hardware Profile DSCWEB Hardware Profile is Absent

And I know it’s removing it because the Hardware Profile disappears from VMM. The question is, why? I can see from the Verbose output that $Ensure is set to Present, so why is the Function ignoring that? As another test, if I run the same test but set $Ensure to Absent, it removes it. But that was really just a diversion to avoid thinking about the real problem because I am completely stumped.

I change my Else block to ElseIf($Ensure = “Present”) and that has no effect. It just jumps right into the If Absent block and deleted the Hardware Profile. Lacking ideas I start going through the xVMHyperV Set-TargetResource function searching for Ensure to see how they do it. And they are using -eq in the If statement instead of =. Does this really make a difference? Yes, that’s the answer. After I change it to that, it evaluates the parameter properly. I feel like this is a very simple mistake that I made solely because of my lack of experience and knowledge of PowerShell.

Doing a little research in PowerShell using Get-Help I discover the following:

  • = is an Assignment Operator.  It sets the value of a variable to a specified value
  • -eq is a Comparison Operator.  It compares to objects looking for an identical value
  • Thus, = does not equal -eq.  I feel dumb.  I should have known that.

That being said, when I run my test using an existing Hardware Profile now (with Ensure = Present), I get this for output.

PS C:\scripts> Set-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
VERBOSE: VMMServer is MY-VMM-SERVER1
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile exists
VERBOSE: The Hardware Profile was found
VERBOSE: Ensure set to Present
VERBOSE: DVDDrive is already set properly
VERBOSE: CPUCount is NFM Default Hardware Configuration NFM Default Hardware Configuration - Gen 2 DSCWEB Hardware Profile.CPUCount, should be 1
VERBOSE: CPUCount Set to 1
VERBOSE: VMNetwork set to NFM Default Hardware Configuration NFM Default Hardware Configuration - Gen 2 DSCWEB Hardware Profile.VirtualNetworkAdapters.VMNetwork, should be 
Set-SCVirtualNetworkAdapter : Cannot validate argument on parameter 'VirtualNetwork'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:101 char:144
+ ... rofile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
+                                                                ~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-SCVirtualNetworkAdapter], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.SetNICCmdlet
 
VERBOSE: VMNetwork set to 

Clearly there are a couple of issues here. The DVDDrive section looks OK, but then in the CPU section I get this beauty for Verbose output.

VERBOSE: CPUCount is NFM Default Hardware Configuration NFM Default Hardware Configuration - Gen 2 DSCWEB Hardware Profile.CPUCount, should be 1

Yeah, that’s not real useful. Looking at this it appears that $HWProfile.CPCount is not what I think it is. Actually I think it’s because I used $HWProfile instead of the $ResourceHWProfile variable. This is going to require a fix in a lot of places. I make those changes and try my test again. I get the expected output, except that I get the same error about the VirtualNetworkAdapter that I got above. This goes back to my work in the previous blog post about how to handle this, because I don’t want to make this parameter mandatory. So, I need to add in some additional logic to my Function here.

If($VMNetwork)
                    {

                        If($VMNetwork -ne $ResourceHWProfile.VirtualNetworkAdapters.VMNetwork)
                        {
                            Write-Verbose "VMNetwork set to $ResourceHWProfile.VirtualNetworkAdapters.VMNetwork, should be $VMNetwork"
                            Get-SCVirtualNetworkAdapter -VMMServer $VMMSErver -HardwareProfile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
                            Write-Verbose "VMNetwork set to $VMNetwork"
                        }
                        Else
                        {
                            Write-Verbose "VMNetwork is already set to $VMNetwork"
                        }
                    }
                    Else
                    {
                        Write-Verbose "No VMNetwork was specified"
                    }#EndVMNetwork

And now when I run my test, I get exactly the output that I would expect.

PS C:\scripts> Set-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
VERBOSE: VMMServer is MY-VMM-SERVER1
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile exists
VERBOSE: The Hardware Profile was found
VERBOSE: Ensure set to Present
VERBOSE: DVDDrive is already set properly
VERBOSE: CPUCount is already set to 1
VERBOSE: No VMNetwork was specified

Boom!
I do various tests with different CPU, DVDDrive and VMNetwork settings and everything looks good. I do make a change to one line of Verbose output, but other than that everything checks out fine. This is really cool. I have an actual functioning DSC Custom Resource. Tomorrow I am going to turn this thing into a Module, and try out an actual DSC Configuration and see if that actually works before adding in my additional parameters.

PowerShell DSC Journey – Day 16

When I left off yesterday I had a somewhat functioning Set-TargetResource function. I use functioning in the way that when something kind of works and doesn’t throw errors but doesn’t actually do anything functions. So, today I am going to be figuring out where I screwed up my logic and getting this Resource to work. And hopefully this doesn’t turn into a smaller version of War and Peace.

Here is my Set-TargetResource in it’s entirety.

#region
function Set-TargetResource
{
	[CmdletBinding()]
	param
	(
		[System.Boolean]
		$DVDDrive,

		[System.String]
		$VMNetwork,

		[System.UInt64]
		$CPUCount,

		[ValidateSet("Present","Absent")]
		[System.String]
		$Ensure,

		[parameter(Mandatory = $true)]
		[System.String]
		$Name,

		[parameter(Mandatory = $true)]
		[System.String]
		$VMMServer
	)

	#Write-Verbose "Use this cmdlet to deliver information about command processing."

	#Write-Debug "Use this cmdlet to write debug information while troubleshooting."

	#Include this line if the resource requires a system reboot.
	#$global:DSCMachineStatus = 1

    Write-Verbose "VMMServer is $VMMServer"
    Write-Verbose "Hardware Profile Name is $Name"

    #Check if VirtualMachineManager module is present for SCVMM cmdlets
        If(!(Get-Module -ListAvailable -Name VirtualMachineManager))
        {
            Throw "The VirtualMachineManager Module was not found.  Please ensure this module is available by installing the VMM Console.  For information
            On installing the VMM Console see:  http://technet.microsoft.com/en-us/library/gg610627.aspx"
        }

    #Check to see if Hardware Profile exists
        Write-Verbose "Checking if the Hardware Profile $Name exists"
        $HWProfile = Get-SCHardwareProfile -VMMServer $VMMServer

        If($HWProfile.Name -match $Name)
        {
            Write-Verbose "The Hardware Profile was found"
            $ResourceHWProfile = Get-SCHardwareProfile -VMMServer $VMMServer | Where-Object Name -eq $Name
            $HWProfileID = $ResourceHWProfile.ID

            #If Hardware Profile Should not exist, remove it
            If($Ensure = "Absent")
            {
                Write-Verbose "Hardware Profile $Name Should be $Ensure"
                Get-SCHardwareProfile -VMMServer $VMMServer -ID $HWProfileID | Remove-SCHardwareProfile
                Write-Verbose "Hardware Profile $Name is $Ensure"
            }
            Else
            {
                #Hardware Profile should be present with the correct settings.  Check the settings.
                    If($DVDDrive -eq $HWProfile.VirtualDVDDrives.Enabled)
                    {
                        Write-Verbose "DVDDrive is already set properly"
                    }
                    Else
                    {
                        If($DVDDrive -eq $True)
                        {
                            New-SCVirtualDVDDrive -HardwareProfile $Name -LUN 1 -Bus 0
                            Write-Verbose "DVDDrive has been created"
                        }
                        Else
                        {
                            Get-SCVirtualDVDDrive -HardwareProfile $Name | Remove-SCVirtualDVDDrive
                            Write-Verbose "DVDDrive has been removed"
                        }
                    }

                    If($CPUCount -ne $HWProfile.CPUCount)
                    {
                        Write-Verbose "CPUCount is $HWProfile.CPUCount, should be $CPUCount"
                        Set-SCHardwareProfile -HardwareProfile $Name -CPUCount $CPUCount
                        Write-Verbose "CPUCount Set to $CPUCount"                                        
                    }
                    Else
                    {
                        Write-Verbose "CPUCount is already set to $CPUCount"
                    }
                    If($VMNetwork -ne $HWProfile.VirtualNetworkAdapters.VMNetwork)
                    {
                        Write-Verbose "VMNetwork set to $HWProfile.VirtualNetworkAdapters.VMNetwork, should be $VMNetwork"
                        Get-SCVirtualNetworkAdapter -VMMServer $VMMSErver -HardwareProfile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
                        Write-Verbose "VMNetwork set to $VMNetwork"
                    }
                    Else
                    {
                        Write-Verbose "VMNetwork is already set to $VMNetwork"
                    }
                        
                }
                           
            }
        Else
        {
            Write-Verbose "The Hardware Profile was not found.  Creating new Hardware Profile $Name"

            New-SCHardwareProfile -VMMServer $VMMSErver -Name $Name

            If($DVDDrive -eq $True)
            {
                New-SCVirtualDVDDrive -HardwareProfile $Name -LUN 1 -Bus 0
                Write-Verbose "DVDDrive has been created"
            }
            Else
            {
                Write-Verbose "DVDDrive is set to $False, no DVDDrive created"
            }

            If($CPUCount -gt 0)
            {
                Write-Verbose "Setting CPUCount to $CPUCount"
                Set-SCHardwareProfile -HardwareProfile $Name -CPUCount $CPUCount
                Write-Verbose "CPUCount Set to $CPUCount" 
            }
            Else
            {
                Write-Verbose "CPU Count set to 0 or not specified, CPU Count should be at least 1"
            }

            If($VMNetwork -ne $null)
            {
                Write-Verbose "Setting VMNetwork to $VMNetwork"
                Get-SCVirtualNetworkAdapter -VMMServer $VMMSErver -HardwareProfile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
                Write-Verbose "VMNetwork set to $VMNetwork"
            }
            Else
            {
                Write-Verbose "VMNetwork not specified"
            }
        }
}
#endregion

The first problem I noticed is that when I run this test of my Set-TargetResource (in which I am testing for a Hardware Profile that doesn’t exist), here is the output I get.

PS C:\Scripts> Set-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
VERBOSE: VMMServer is MY-VMM-SERVER1
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile exists
VERBOSE: The Hardware Profile was not found.  Creating new Hardware Profile DSCWEB Hardware Profile


CPUCount                            : 1
Memory                              : 512
DynamicMemoryEnabled                : False
DynamicMemoryMaximumMB              : 
DynamicMemoryBufferPercentage       : 
MemoryWeight                        : 
VirtualVideoAdapterEnabled          : False
MonitorMaximumCount                 : 
MonitorResolutionMaximum            : 
BootOrder                           : 
FirstBootDevice                     : 
SecureBootEnabled                   : 
UndoDisksEnabled                    : False
CPUType                             : 3.60 GHz Xeon (2 MB L2 cache)
IsHighlyAvailable                   : False
HAVMPriority                        : 
IsDRProtectionRequired              : False
RecoveryPointObjective              : 
LimitCPUFunctionality               : False
LimitCPUForMigration                : False
ExpectedCPUUtilization              : 20
DiskIO                              : 0
NetworkUtilization                  : 0
RelativeWeight                      : 100
CPUReserve                          : 0
CPUMax                              : 100
CPUPerVirtualNumaNodeMaximum        : 
MemoryPerVirtualNumaNodeMaximumMB   : 
VirtualNumaNodesPerSocketMaximum    : 
DynamicMemoryMinimumMB              : 
NumLockEnabled                      : 
NumaIsolationRequired               : 
Generation                          : 1
VirtualDVDDrives                    : {}
ShareSCSIBus                        : False
VirtualNetworkAdapters              : {}
VirtualFibreChannelAdapters         : {}
VirtualFloppyDrive                  : DSCWEB Hardware Profile
VirtualCOMPorts                     : {COM1, COM2}
VirtualSCSIAdapters                 : {}
CapabilityProfile                   : 
CapabilityProfileCompatibilityState : Compatible
GrantedToList                       : {}
UserRoleID                          : 75700cd5-893e-4f68-ada7-50ef4668acc6
UserRole                            : Administrator
Owner                               : NFM\jacob.benson
ObjectType                          : HardwareProfile
Accessibility                       : Public
Name                                : DSCWEB Hardware Profile
IsViewOnly                          : False
Description                         : 
AddedTime                           : 6/16/2014 7:21:42 AM
ModifiedTime                        : 6/16/2014 7:21:42 AM
Enabled                             : True
MostRecentTask                      : Create hardware profile
ServerConnection                    : Microsoft.SystemCenter.VirtualMachineManager.Remoting.ServerConnection
ID                                  : 083da7f1-54af-4720-b4a4-dad908b76dfa
MarkedForDeletion                   : False
IsFullyCached                       : True
MostRecentTaskIfLocal               : Create hardware profile

VERBOSE: DVDDrive is set to False, no DVDDrive created
VERBOSE: CPU Count set to 0 or not specified, CPU Count should be at least 1
VERBOSE: Setting VMNetwork to 
Set-SCVirtualNetworkAdapter : Cannot validate argument on parameter 'VirtualNetwork'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:138 char:136
+ ... rofile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
+                                                                ~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-SCVirtualNetworkAdapter], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.SetNICCmdlet
 
VERBOSE: VMNetwork set to 

Couple of things to note here. It didn’t find the Hardware Profile, so it created it. However, I didn’t specify anything for the DVD Drive, but the verbose output indicates that it was set to False, so it did not create a DVD Drive, so that needs to be fixed. The second issue is that the verbose output indicates that the CPU count set to 0 or not specified, CPU count should be at least 1. If I don’t specify a CPU count the Hardware Profile creation defaults to 1 CPU, so the message should indicate that. And lastly it just errored out on setting the VMNetwork because I didn’t specify one.

First things first, here is the section of code I have to handle the DVD Drive.

If($DVDDrive -eq $True)
            {
                New-SCVirtualDVDDrive -HardwareProfile $Name -LUN 1 -Bus 0
                Write-Verbose "DVDDrive has been created"
            }
            Else
            {
                Write-Verbose "DVDDrive is set to $False, no DVDDrive created"
            }

While thinking about this, it occurs to me that maybe I should set this to default to True, because I imagine that in almost every case you are going to want a DVD Drive on your server. I feel like that is a better solution than adding in another ElseIf section that has the Null case. Do people agree? I changed the parameter in the Set-TargetResource Function so that $DVDDrive = $True, and that means the existing code block should work fine.

Second minor issue is changing the Verbose text when the CPU Count is set to 0 or not specified. This particular section of code I changed from:

           If($CPUCount -gt 0)
            {
                Write-Verbose "Setting CPUCount to $CPUCount"
                Set-SCHardwareProfile -HardwareProfile $Name -CPUCount $CPUCount
                Write-Verbose "CPUCount Set to $CPUCount" 
            }
            Else
            {
                Write-Verbose "CPU Count set to 0 or not specified, CPU Count set to 1"
            }

To:

 If($CPUCount -gt 0)
            {
                Write-Verbose "Setting CPUCount to $CPUCount"
                Set-SCHardwareProfile -HardwareProfile $Name -CPUCount $CPUCount
                Write-Verbose "CPUCount Set to $CPUCount" 
            }
            Else
            {
                Write-Verbose "CPUCount set to 0 or not specified, setting CPUCount to 1"
                Set-SCHardwareProfile -HardwareProfile $Name -CPUCount 1
                Write-Verbose "CPUCount set to 1"
            }

Of course after doing this I think, should I just set the CPUCount parameter to default to 1? I mean, you need a CPU for it to run, and it defaults to 1 anyways without even setting anything. I realize the code in the Else block is a little redundant, but for consistency sake I left it that way so somebody reading it wouldn’t be like “why did he set the hardware profile in the first section and not the second?”. I could also make CPUCount a mandatory parameter, but that seems a little excessive when it just defaults to 1 anyways. Hmm. I don’t think there is a good answer to this. I am going to set CPUCount to default to 1 and just change the Else statement to use -CPUCount $CPUCount for now. I don’t like the idea of hard coding parameter values into a DSC Configuration, but in this particular case I think it is justifiable.

Alright, now for the last problem with the VMNetwork. If I don’t specify it, it shouldn’t just freak out and throw and error, it needs to say something useful. Here is my current code.

            If($VMNetwork -ne $null)
            {
                Write-Verbose "Setting VMNetwork to $VMNetwork"
                Get-SCVirtualNetworkAdapter -VMMServer $VMMSErver -HardwareProfile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
                Write-Verbose "VMNetwork set to $VMNetwork"
            }
            Else
            {
                Write-Verbose "VMNetwork not specified"
            }

The problem here is that I thought that checking to see if the parameter was not equal to Null mean it would also check to make sure it wasn’t empty. Clearly this is not the case. So how do I check for both cases? It appears based on some Google searches the best way to do this is to change it from If($VMNetwork -ne $null) to just If($VMNetwork) which checks for Null or Empty.

With those changes done, lets test again and see what happens (I deleted the Hardware Configuration created from the previous test).

Much better!

PS C:\Scripts> Set-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
VERBOSE: VMMServer is MY-VMM-SERVER1
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine 
Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine 
Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine 
Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine 
Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile exists
VERBOSE: The Hardware Profile was not found.  Creating new Hardware Profile DSCWEB Hardware Profile


CPUCount                            : 1
Memory                              : 512
DynamicMemoryEnabled                : False
DynamicMemoryMaximumMB              : 
DynamicMemoryBufferPercentage       : 
MemoryWeight                        : 
VirtualVideoAdapterEnabled          : False
MonitorMaximumCount                 : 
MonitorResolutionMaximum            : 
BootOrder                           : 
FirstBootDevice                     : 
SecureBootEnabled                   : 
UndoDisksEnabled                    : False
CPUType                             : 3.60 GHz Xeon (2 MB L2 cache)
IsHighlyAvailable                   : False
HAVMPriority                        : 
IsDRProtectionRequired              : False
RecoveryPointObjective              : 
LimitCPUFunctionality               : False
LimitCPUForMigration                : False
ExpectedCPUUtilization              : 20
DiskIO                              : 0
NetworkUtilization                  : 0
RelativeWeight                      : 100
CPUReserve                          : 0
CPUMax                              : 100
CPUPerVirtualNumaNodeMaximum        : 
MemoryPerVirtualNumaNodeMaximumMB   : 
VirtualNumaNodesPerSocketMaximum    : 
DynamicMemoryMinimumMB              : 
NumLockEnabled                      : 
NumaIsolationRequired               : 
Generation                          : 1
VirtualDVDDrives                    : {}
ShareSCSIBus                        : False
VirtualNetworkAdapters              : {}
VirtualFibreChannelAdapters         : {}
VirtualFloppyDrive                  : DSCWEB Hardware Profile
VirtualCOMPorts                     : {COM1, COM2}
VirtualSCSIAdapters                 : {}
CapabilityProfile                   : 
CapabilityProfileCompatibilityState : Compatible
GrantedToList                       : {}
UserRoleID                          : 75700cd5-893e-4f68-ada7-50ef4668acc6
UserRole                            : Administrator
Owner                               : NFM\jacob.benson
ObjectType                          : HardwareProfile
Accessibility                       : Public
Name                                : DSCWEB Hardware Profile
IsViewOnly                          : False
Description                         : 
AddedTime                           : 6/16/2014 7:56:15 AM
ModifiedTime                        : 6/16/2014 7:56:15 AM
Enabled                             : True
MostRecentTask                      : Create hardware profile
ServerConnection                    : Microsoft.SystemCenter.VirtualMachineManager.Remoting.ServerC
                                      onnection
ID                                  : fdcaad29-7709-4ead-ad79-9264ae2e5820
MarkedForDeletion                   : False
IsFullyCached                       : True
MostRecentTaskIfLocal               : Create hardware profile

Connection            : None
ISOId                 : 
ISO                   : 
HostDrive             : 
BusType               : 
Bus                   : 
Lun                   : 
ISOLinked             : False
ObjectType            : VirtualDVDDrive
Accessibility         : Public
Name                  : DSCWEB Hardware Profile
IsViewOnly            : False
Description           : 
AddedTime             : 6/16/2014 7:56:15 AM
ModifiedTime          : 6/16/2014 7:56:15 AM
Enabled               : True
MostRecentTask        : Create virtual DVD drive
ServerConnection      : Microsoft.SystemCenter.VirtualMachineManager.Remoting.ServerConnection
ID                    : e6520ca7-0348-45cc-8e51-758e9ccc462c
MarkedForDeletion     : False
IsFullyCached         : True
MostRecentTaskIfLocal : Create virtual DVD drive

VERBOSE: DVDDrive has been created
VERBOSE: Setting CPUCount to 1
CPUCount                            : 1
Memory                              : 512
DynamicMemoryEnabled                : False
DynamicMemoryMaximumMB              : 
DynamicMemoryBufferPercentage       : 
MemoryWeight                        : 
VirtualVideoAdapterEnabled          : False
MonitorMaximumCount                 : 
MonitorResolutionMaximum            : 
BootOrder                           : 
FirstBootDevice                     : 
SecureBootEnabled                   : 
UndoDisksEnabled                    : False
CPUType                             : 3.60 GHz Xeon (2 MB L2 cache)
IsHighlyAvailable                   : False
HAVMPriority                        : 
IsDRProtectionRequired              : False
RecoveryPointObjective              : 
LimitCPUFunctionality               : False
LimitCPUForMigration                : False
ExpectedCPUUtilization              : 20
DiskIO                              : 0
NetworkUtilization                  : 0
RelativeWeight                      : 100
CPUReserve                          : 0
CPUMax                              : 100
CPUPerVirtualNumaNodeMaximum        : 
MemoryPerVirtualNumaNodeMaximumMB   : 
VirtualNumaNodesPerSocketMaximum    : 
DynamicMemoryMinimumMB              : 
NumLockEnabled                      : 
NumaIsolationRequired               : 
Generation                          : 1
VirtualDVDDrives                    : {DSCWEB Hardware Profile}
ShareSCSIBus                        : False
VirtualNetworkAdapters              : {}
VirtualFibreChannelAdapters         : {}
VirtualFloppyDrive                  : DSCWEB Hardware Profile
VirtualCOMPorts                     : {COM1, COM2}
VirtualSCSIAdapters                 : {}
CapabilityProfile                   : 
CapabilityProfileCompatibilityState : Compatible
GrantedToList                       : {}
UserRoleID                          : 75700cd5-893e-4f68-ada7-50ef4668acc6
UserRole                            : Administrator
Owner                               : NFM\jacob.benson
ObjectType                          : HardwareProfile
Accessibility                       : Public
Name                                : DSCWEB Hardware Profile
IsViewOnly                          : False
Description                         : 
AddedTime                           : 6/16/2014 7:56:15 AM
ModifiedTime                        : 6/16/2014 7:56:16 AM
Enabled                             : True
MostRecentTask                      : Change properties of hardware profile
ServerConnection                    : Microsoft.SystemCenter.VirtualMachineManager.Remoting.ServerC
                                      onnection
ID                                  : fdcaad29-7709-4ead-ad79-9264ae2e5820
MarkedForDeletion                   : False
IsFullyCached                       : True
MostRecentTaskIfLocal               : Change properties of hardware profile

VERBOSE: CPUCount Set to 1
VERBOSE: VMNetwork not specified, no VMNetwork was set

The one thing I don’t like about this, is that it still sets it set the CPUCount, when in all actuality it was already set by default. So, I am going to change that section again to this.

            Else
            {
                Write-Verbose "CPUCount set to 0 or not specified, CPUCount set to 1 by default"
            }

I verified that the Hardware Profile has 1 CPU, the DVD Drive is Enabled, and the Virtual Network is not set. Not setting a VMNetwork appears to mean that no settings in VirtualNetworkAdapters are set, which is good to know.

Ok, with that all done, I run it again because I remember there was a problem with this. Here are the important parts from this test.

PS C:\Scripts> Set-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
VERBOSE: VMMServer is MY-VMM-SERVER1
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile exists
VERBOSE: The Hardware Profile was found
VERBOSE: Hardware Profile DSCWEB Hardware Profile Should be Absent
*DISPLAYS HARDWARE PROFILE INFORMATION*
VERBOSE: Hardware Profile DSCWEB Hardware Profile is Absent

Uh. What. That looks to be a massive fail on my part. Although, I don’t understand what the problem is here. It checks if it exists, it finds that it exists, and then it is going through the block where $Ensure = Absent, even though I have $Ensure = Present. Of course, then it is removing the profile as well, which is fun. I add this line to my code right above the line for If($Ensure = Absent), Write-Verbose “Ensure set to $Ensure”.

And I get this, which is even more puzzling.

PS C:\Scripts> Set-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
VERBOSE: VMMServer is MY-VMM-SERVER1
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile exists
VERBOSE: The Hardware Profile was found
VERBOSE: Ensure set to Present
VERBOSE: Hardware Profile DSCWEB Hardware Profile Should be Absent

It says that $Ensure = Present, so why the hell is it going through that block of code? I don’t understand! If I set it to Absent it does the same thing, which is fine, but why is it doing it when it is set to present????

I feel like this is a good place to stop for now so I can think a little about this and try to figure out what is going on.

PowerShell DSC Journey – Day 15

So when I left off yesterday I appeared to have my Get and Test TargetResource functions working properly, now it’s time to work on the Set-TargetResource function, which is probably where the fun will happen.

When I go to set the Resource, I have a couple of different scenarios I need to work through.

  • Hardware Profile exists, ensure equals absent
  • Hardware Profile exists, ensure equals present
  • Hardware Profile does not exist, ensure equals present

And here is what I came up with those 3 scenarios after working my way through it (and looking at the xVMHyperV Resource).  I almost immediately ran into a problem where there doesn’t seem to be a clear way to tell the Hardware Profile that it needs a DVD drive (at least not using the Set-SCHardwareProfile cmdlets).  A little research led me to the Set-SCVirtualDVDDrive cmdlets which can be associated with “a virtual machine, virtual machine template, or hardware profile used in a Virtual Machine Manager (VMM) environment.” which is perfect.  When I set the New DVD Drive I must specify a LUN and a BUS.  I am going to need some extra logic eventually to deal with what happens if you have multiple disks or whatever else, but for right now this will have to do.  I don’t even want to admit how long this took me to work through, but it appears to work.

If($HWProfile.Name -match $Name)
        {
            Write-Verbose "The Hardware Profile was found"
            $ResourceHWProfile = Get-SCHardwareProfile -VMMServer $VMMServer | Where-Object Name -eq $Name
            $HWProfileID = $ResourceHWProfile.ID

            #If Hardware Profile Should not exist, remove it
            If($Ensure = "Absent")
            {
                Write-Verbose "Hardware Profile $Name Should be $Ensure"
                Get-SCHardwareProfile -VMMServer $VMMServer -ID $HWProfileID | Remove-SCHardwareProfile
                Write-Verbose "Hardware Profile $Name is $Ensure"
            }
            Else
            {
                #Hardware Profile should be present with the correct settings.  Check the settings.
                    If($DVDDrive -eq $HWProfile.VirtualDVDDrives.Enabled)
                    {
                        Write-Verbose "DVDDrive is already set properly"
                    }
                    Else
                    {
                        If($DVDDrive -eq $True)
                        {
                            New-SCVirtualDVDDrive -HardwareProfile $Name -LUN 1 -Bus 0
                            Write-Verbose "DVDDrive has been created"
                        }
                        Else
                        {
                            Get-SCVirtualDVDDrive -HardwareProfile $Name | Remove-SCVirtualDVDDrive
                            Write-Verbose "DVDDrive has been removed"
                        }
                    }

CPU Count is easy and straightforward, which was nice.

                    If($CPUCount -ne $HWProfile.CPUCount)
                    {
                        Write-Verbose "CPUCount is $HWProfile.CPUCount, should be $CPUCount"
                        Set-SCHardwareProfile -HardwareProfile $Name -CPUCount $CPUCount
                        Write-Verbose "CPUCount Set to $CPUCount"                                        
                    }

However, I ran into the same problem with VMNetwork that I did with the DVDDrive (which doesn’t make me happy). But since I already had that experience it was much quicker to come up with what to do about it.

If($VMNetwork -ne $HWProfile.VirtualNetworkAdapters.VMNetwork)
                    {
                        Write-Verbose "VMNetwork set to $HWProfile.VirtualNetworkAdapters.VMNetwork, should be $VMNetwork"
                        Get-SCVirtualNetworkAdapter -VMMServer $VMMSErver -HardwareProfile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
                        Write-Verbose "VMNetwork set to $VMNetwork"
                    }

Now that that is done (I feel like I just went through the ringer), all I really need to do for the next part is copy and paste the commands from above into the area where I am creating a Hardware Profile if none exists.

Write-Verbose "The Hardware Profile was not found.  Creating new Hardware Profile $Name"

            New-SCHardwareProfile -VMMServer $VMMSErver -Name $Name

            If($DVDDrive -eq $True)
            {
                New-SCVirtualDVDDrive -HardwareProfile $Name -LUN 1 -Bus 0
                Write-Verbose "DVDDrive has been created"
            }
            Else
            {
                Write-Verbose "DVDDrive is set to $False, no DVDDrive created"
            }

            Write-Verbose "Setting CPUCount to $CPUCount"
            Set-SCHardwareProfile -HardwareProfile $Name -CPUCount $CPUCount
            Write-Verbose "CPUCount Set to $CPUCount" 

            Write-Verbose "Setting VMNetwork to $VMNetwork"
            Get-SCVirtualNetworkAdapter -VMMServer $VMMSErver -HardwareProfile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
            Write-Verbose "VMNetwork set to $VMNetwork"

So I guess now we need to test this thing and see if it blows up. I am going to start with my original 3 tests and then add in some variations (providing those still work). I run the first one for an existing Hardware Profile and find out quickly I need to specify the Ensure parameter. Which means I should probably make that a mandatory property (I think?). I don’t see anyone leaving that out unintentionally but maybe I should add in some logic to check for that? I guess I will figure that out later.

PS C:\Scripts> Set-TargetResource -Name "DSCWEB Hardware Profile" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
VERBOSE: VMMServer is MY-VMM-SERVER1
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile exists

Winning! Test #2! This does not go as well.

PS C:\Scripts> Set-TargetResource -Name "DSCWEB Hardware Profile 2" -VMMServer MY-VMM-SERVER1 -Ensure Present -Verbose
VERBOSE: VMMServer is MY-VMM-SERVER1
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile 2
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile 2 exists

Alright, so it’s basically exiting after checking if the Hardware Profile exists. In this test, the profile does not exist, so it should jump down to the Else block and create it, however that is not happening. I am ashamed to admit this but I had an extra closing bracket in the top half of the script, so it was exiting before creating the profile. When I run it again, it works, sort of. The profile is created, but it throws errors because I didn’t specify a CPU count or a VMNetwork, because they aren’t mandatory. So, I need to do some updates here I think.

            If($CPUCount -gt 0)
            {
                Write-Verbose "Setting CPUCount to $CPUCount"
                Set-SCHardwareProfile -HardwareProfile $Name -CPUCount $CPUCount
                Write-Verbose "CPUCount Set to $CPUCount" 
            }
            Else
            {
                Write-Verbose "CPU Count set to 0 or not specified, CPU Count should be at least 1"
            }

            If($VMNetwork -ne $null)
            {
                Write-Verbose "Setting VMNetwork to $VMNetwork"
                Get-SCVirtualNetworkAdapter -VMMServer $VMMSErver -HardwareProfile $Name | Set-SCVirtualNetworkAdapter -VirtualNetwork $VMNetwork
                Write-Verbose "VMNetwork set to $VMNetwork"
            }
            Else
            {
                Write-Verbose "VMNetwork not specified"
            }

For the second test, it created the Hardware Profile, with no errors, however it added “VERBOSE: Hardware Profile DSCWEB Hardware Profile 2 is Absent” after displaying the Hardware Profile, which uh….doesn’t make any sense?

PS C:\Scripts> Set-TargetResource -Name "DSCWEB Hardware Profile 2" -VMMServer MY-VMM-SERVER1-Ensure Present -Verbose
VERBOSE: VMMServer is MDC-SC-VMM01
VERBOSE: Hardware Profile Name is DSCWEB Hardware Profile 2
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\virtualmachinemanager.R2Aliases.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\VirtualMachineManagerLibraryClientCleanup.ps1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\virtualmachinemanager.R2AdvFunc.psm1'.
VERBOSE: Loading module from path 'C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager\..\..\Microsoft.SystemCenter.VirtualMachineManager.dll'.
VERBOSE: Checking if the Hardware Profile DSCWEB Hardware Profile 2 exists
VERBOSE: The Hardware Profile was found
VERBOSE: Hardware Profile DSCWEB Hardware Profile 2 Should be Absent


CPUCount                            : 1
Memory                              : 512
DynamicMemoryEnabled                : False
DynamicMemoryMaximumMB              : 
DynamicMemoryBufferPercentage       : 
MemoryWeight                        : 
VirtualVideoAdapterEnabled          : False
MonitorMaximumCount                 : 
MonitorResolutionMaximum            : 
BootOrder                           : 
FirstBootDevice                     : 
SecureBootEnabled                   : 
UndoDisksEnabled                    : False
CPUType                             : 3.60 GHz Xeon (2 MB L2 cache)
IsHighlyAvailable                   : False
HAVMPriority                        : 
IsDRProtectionRequired              : False
RecoveryPointObjective              : 
LimitCPUFunctionality               : False
LimitCPUForMigration                : False
ExpectedCPUUtilization              : 20
DiskIO                              : 0
NetworkUtilization                  : 0
RelativeWeight                      : 100
CPUReserve                          : 0
CPUMax                              : 100
CPUPerVirtualNumaNodeMaximum        : 
MemoryPerVirtualNumaNodeMaximumMB   : 
VirtualNumaNodesPerSocketMaximum    : 
DynamicMemoryMinimumMB              : 
NumLockEnabled                      : 
NumaIsolationRequired               : 
Generation                          : 1
VirtualDVDDrives                    : {}
ShareSCSIBus                        : False
VirtualNetworkAdapters              : {}
VirtualFibreChannelAdapters         : {}
VirtualFloppyDrive                  : 
VirtualCOMPorts                     : {COM1, COM2}
VirtualSCSIAdapters                 : {}
CapabilityProfile                   : 
CapabilityProfileCompatibilityState : Compatible
GrantedToList                       : {}
UserRoleID                          : 75700cd5-893e-4f68-ada7-50ef4668acc6
UserRole                            : Administrator
Owner                               : NFM\jacob.benson
ObjectType                          : HardwareProfile
Accessibility                       : Public
Name                                : DSCWEB Hardware Profile 2
IsViewOnly                          : False
Description                         : 
AddedTime                           : 6/12/2014 3:57:41 PM
ModifiedTime                        : 6/12/2014 3:57:41 PM
Enabled                             : True
MostRecentTask                      : Create hardware profile
ServerConnection                    : Microsoft.SystemCenter.VirtualMachineManager.Remoting.ServerConnection
ID                                  : 694bdd69-0733-40a3-ac3e-a49615c594b7
MarkedForDeletion                   : True
IsFullyCached                       : False
MostRecentTaskIfLocal               : Create hardware profile

VERBOSE: Hardware Profile DSCWEB Hardware Profile 2 is Absent

Looking at the verbose messages closer, I have a couple of issues that I need to get cleaned up. Which will be a good thing to do tomorrow.