/// <summary> /// Processes the record. /// </summary> /// <exception cref="PSInvalidCastException">Incorrect object found.</exception> protected override async Task ProcessRecordAsync() { foreach (string input in Input) { var member = new Member(Prefix, input, true); var output = new SyncOutput(member.Name); if (_currentMembers.ContainsKey(member.Name.ToLower())) { // No changes just add to _postMembers so we don't remove it _postMembers.Add(member.Name.ToLower()); } else { var obj = await FindObjectByName(member); string oldName = null; if (obj == null && Rename.IsPresent) { obj = await FindObjectByIP(member); if (obj != null) { oldName = obj.Name; output.Actions = Actions.Rename; output.Comments = $"Renamed {obj.Name}"; try { if (obj is Network n) { n.Name = member.Name; await n.AcceptChanges(Ignore, cancellationToken : CancelProcessToken); } else if (obj is Host h) { h.Name = member.Name; await h.AcceptChanges(Ignore, cancellationToken : CancelProcessToken); } } catch (Koopman.CheckPoint.Exceptions.GenericException e) { output.Error = true; WriteError(new ErrorRecord(e, "Rename", ErrorCategory.WriteError, null)); } } } if (obj == null) { output.Actions = Actions.Create; try { if (member.IsHost()) { var host = new Host(Session) { Name = member.Name, Color = Color, Comments = Comments }; if (member.IsIPv4()) { host.IPv4Address = member.IPAddress; } else { host.IPv6Address = member.IPAddress; } host.Groups.Add(GroupName); foreach (string t in Tags ?? Enumerable.Empty <string>()) { host.Tags.Add(t); } await host.AcceptChanges(Ignore, cancellationToken : CancelProcessToken); } else { var network = new Network(Session) { Name = member.Name, Color = Color, Comments = Comments }; if (member.IsIPv4()) { network.Subnet4 = member.IPAddress; network.MaskLength4 = member.CIDR; } else { network.Subnet6 = member.IPAddress; network.MaskLength6 = member.CIDR; } network.Groups.Add(GroupName); foreach (string t in Tags ?? Enumerable.Empty <string>()) { network.Tags.Add(t); } await network.AcceptChanges(Ignore, cancellationToken : CancelProcessToken); } } catch (Koopman.CheckPoint.Exceptions.GenericException e) { output.Error = true; WriteError(new ErrorRecord(e, "Create", ErrorCategory.WriteError, null)); } } else if (!_currentMembers.ContainsKey(obj.Name.ToLower())) { output.Actions |= Actions.Add; group.Members.Add(obj.Name); try { await group.AcceptChanges(Ignore, cancellationToken : CancelProcessToken); } catch (Koopman.CheckPoint.Exceptions.GenericException e) { output.Error = true; WriteError(new ErrorRecord(e, "AddToGroup", ErrorCategory.WriteError, null)); } } // As _postMembers used to work out which entries to remove from the group If // object was renamed enter old name as it could of already been a member Of the // group under the old name _postMembers.Add(((output.Actions.HasFlag(Actions.Rename)) ? oldName : member.Name).ToLower()); } WriteSyncOutput(output); } }