//--------------------------------------------------------------------- private void DisplayImageIfApplicable(ManagementPackElement element) { if (!(element is ManagementPackResource)) { return; } ManagementPackResource resource = (ManagementPackResource)element; if (resource.FileName != null) { if (resource.XmlTag.Equals("Image", StringComparison.InvariantCultureIgnoreCase)) { IDictionary <string, Stream> streams = m_bundle.GetStreams(m_managementPack); foreach (var stream in streams) { if (stream.Key.Equals(resource.Name, StringComparison.InvariantCultureIgnoreCase)) { Image pic = Image.FromStream(stream.Value); m_ImageResourcePictureBox.BackColor = Color.White; m_ImageResourcePictureBox.Image = pic; m_ImageResourcePictureBox.Visible = true; } } } else { m_ImageResourcePictureBox.Visible = false; } } }
private void HandleOutput(ManagementPackResource res) { Regex myTag = new Regex(Tag, RegexOptions.IgnoreCase); // If we don't match our tag, just return if (!myTag.Match(res.XmlTag).Success) { return; } PSObject CompositeResource = new PSObject(res); ManagementPackImage i = res as ManagementPackImage; bool isImage = false; if (i != null) { isImage = true; WriteVerbose("resource is an image!"); } ManagementPackReportResource rep = res as ManagementPackReportResource; bool isReport = false; if (rep != null) { isReport = true; WriteVerbose("resource is an report!"); } if (this.Data || isImage || isReport) { // Just add something if it might be there if (!res.HasNullStream) { try { Stream s = _mg.Resources.GetResourceData(res); int l = (int)s.Length; byte[] data = new byte[l]; s.Read(data, 0, l); s.Close(); s.Dispose(); CompositeResource.Members.Add(new PSNoteProperty("StreamData", data)); CompositeResource.Members.Add(new PSScriptMethod("ConvertToString", ScriptBlock.Create("[char[]]($this.StreamData) -join ''"))); CompositeResource.Members.Add(new PSScriptMethod("Save", ScriptBlock.Create(@" $fs = new-object io.filestream (""$PWD/"" + $this.FileName),OpenOrCreate $result = $fs.write($this.StreamData,0,$this.StreamData.length) $fs.close() $fs.dispose() ""File saved as: "" + $this.Filename "))); } catch { this.WriteWarning("Resource stream " + res.Name + " is unexpectedly null"); } } } CompositeResource.Members.Add(new PSNoteProperty("__TYPE", res.GetType().Name)); this.WriteObject(CompositeResource); }
protected override void ProcessRecord() { base.BeginProcessing(); if (Id != Guid.Empty) { try { ManagementPackResource res = _mg.Resources.GetResource <ManagementPackResource>(Id); HandleOutput(res); } catch (Exception e) { ThrowTerminatingError(new ErrorRecord(e, "Resource Failure", ErrorCategory.InvalidOperation, Id)); } } else if (ManagementPack != null) { if (Name != null) { try { ManagementPackResource res = ManagementPack.GetResource <ManagementPackResource>(Name); HandleOutput(res); } catch (Exception e) { ThrowTerminatingError(new ErrorRecord(e, "Resource Failure", ErrorCategory.InvalidOperation, Name)); } } else { foreach (ManagementPackResource r in ManagementPack.GetResources <ManagementPackResource>()) { HandleOutput(r); } } } else // Get all the resources!!! { foreach (ManagementPackResource r in _mg.Resources.GetResources <ManagementPackResource>()) { HandleOutput(r); } } }
//--------------------------------------------------------------------- private void DisplayScriptIfApplicable(ManagementPackElement element) { if (!(element is ManagementPackResource)) { return; } ManagementPackResource resource = (ManagementPackResource)element; m_ScriptResourceTextBox.Text = String.Empty; if (resource.FileName != null) { // works for now... maybe with "config" files (or "deployableresource"s) we might want to show in browser control since it is XML... if (resource.FileName.EndsWith(".sql", StringComparison.InvariantCultureIgnoreCase) || resource.FileName.EndsWith(".ps1", StringComparison.InvariantCultureIgnoreCase) || resource.FileName.EndsWith(".config", StringComparison.InvariantCultureIgnoreCase)) { string ScriptBody = String.Empty; IDictionary <string, Stream> streams = m_bundle.GetStreams(m_managementPack); foreach (var stream in streams) { if (stream.Key.Equals(resource.Name, StringComparison.InvariantCultureIgnoreCase)) { StreamReader reader = new StreamReader(stream.Value); ScriptBody = reader.ReadToEnd(); } } if (String.IsNullOrEmpty(ScriptBody)) { return; } m_ScriptResourceTextBox.Text = ScriptBody; } } }