protected override void ProcessRecord() { foreach (ManagementPackSecureReference secureReference in _secureReferences) { //First get the MP that the Secure Reference that was passed in is stored in so that we can create some SecureReferenceOverrides in it. ManagementPack mpSecureReferenceMP = secureReference.GetManagementPack(); //Before we create a new SecureReferenceOverride we need to check to see if one already exists. bool boolSecureRefOverrideAlreadyExists = false; //Loop through each Override in the MP... ManagementPackElementCollection <ManagementPackOverride> listOverrides = mpSecureReferenceMP.GetOverrides(); foreach (ManagementPackOverride mpOverride in listOverrides) { //...if it is a ManagementPackSecureReferenceOverride... if (mpOverride is ManagementPackSecureReferenceOverride) { //...then cast it to a ManagementPackSecureReferenceOverride... ManagementPackSecureReferenceOverride mpSecRefOverride = mpOverride as ManagementPackSecureReferenceOverride; //...and then compare it to the SecureReference that was passed in... if (mpSecRefOverride.SecureReference.Id == secureReference.Id) { //...if it is the same one then get a list of all the SecureData objects so we can compare with those... IList <SecureData> secureDataList = _mg.Security.GetSecureData(); foreach (SecureData secureData in secureDataList) { //...by comparing the SecureStorageID of each of the existing and the .Value of the SecureData we just created... if (String.Compare (BitConverter.ToString(secureData.SecureStorageId, 0, secureData.SecureStorageId.Length).Replace("-", ""), mpSecRefOverride.Value, StringComparison.Ordinal ) == 0 ) { //...and if you find a match... WindowsCredentialSecureData windowsCred = secureData as WindowsCredentialSecureData; if (windowsCred != null) { //...then set the bool to true so we know that there is already a SecureReferenceOverride with this same exact SecureData // so we dont need to create a new SecureReferenceOverride in this case. boolSecureRefOverrideAlreadyExists = true; } } } } } } //Do we need to create a new SecureReferenceOverride? if (!boolSecureRefOverrideAlreadyExists) { //Yes, we need to create a new SecureReferenceOverride... //First create the SecureReferenceOverride object by setting its ID ManagementPackSecureReferenceOverride secureOverride = new ManagementPackSecureReferenceOverride(mpSecureReferenceMP, String.Format("SecureReferenceOverride.{0}", Guid.NewGuid().ToString("N"))); //Then tell it that it's scope is for all objects by setting the class context to System.Entity secureOverride.Context = _mg.EntityTypes.GetClass(SystemClass.Entity); //Set the SecureReference equal to the SecureReference that was passed in. secureOverride.SecureReference = secureReference; //Give it a display name - doesnt need to be anything fancy since it doesnt show anywhere in the UI. secureOverride.DisplayName = "SecureReferenceOverride_" + Guid.NewGuid().ToString(); //Convert to a byte array secureOverride.Value = BitConverter.ToString(_credentialSecureData.SecureStorageId, 0, _credentialSecureData.SecureStorageId.Length).Replace("-", ""); //Now allow this SecureData to be downloaded to all the management servers ApprovedHealthServicesForDistribution <EnterpriseManagementObject> approved = new ApprovedHealthServicesForDistribution <EnterpriseManagementObject>(); approved.Result = ApprovedHealthServicesResults.All; //Tell SCSM that we are going to update (or submit new) this SecureReferenceOverride secureReference.Status = ManagementPackElementStatus.PendingUpdate; //Post it to the database. This will show up on the SecureReferenceOverride table in the database and in the <Overrides> section of the MP XML string secureReferenceInfo = secureReference.Name; if (secureReference.DisplayName != null) { secureReferenceInfo = secureReference.DisplayName; } if (ShouldProcess(secureReferenceInfo)) { _mg.Security.SetApprovedHealthServicesForDistribution <EnterpriseManagementObject>(_credentialSecureData, approved); mpSecureReferenceMP.AcceptChanges(); } } } }