Powershell import-clixml from string

Can I run the import-clixml on a string or xml object? It requires the file path to be entered to create ps objects and not be able to get input from the xml object. Since there is a cmdlet convertto-xml that serializes the ps object to an xml object, why is there no envelope from xml that would do the opposite?

I know the System.Xml.Serialization.XmlSerializer class that will do just that, however I would like to use cmdlets for this.

Is there a way to do this using cmdlets (perhaps only with import-clixml ), without creating temporary files?

+4
source share
3 answers

An obvious attempt would be to simply indicate the path to the variable:

 PS Home:\> $xmldata = gci | ConvertTo-Xml PS Home:\> Import-Clixml Variable:\xmldata Import-Clixml : Cannot open file because the current provider (Microsoft.PowerShell.Core\Variable) cannot open a file. At line:1 char:14 + Import-Clixml <<<< Variable:\xmldata + CategoryInfo : InvalidArgument: (:) [Import-Clixml], PSInvalidOperationException + FullyQualifiedErrorId : ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ImportClixmlCommand 

... which sadly fails. Therefore, I would suggest that there really is no way to work without temporary files.

The main theme of ConvertTo-XML is the ability to further process XML in PowerShell after converting the object to XML. So the question is, why can't you just make changes to the object directly, and not manipulate XML and transform it?

Otherwise, you can put temporary files in a function.

+1
source

Someone wrote the ConvertTo-CliXml function .

Thus, it should be possible to write the ConvertFrom-CliXml function in a similar way. You should examine the Export-CliXml cmdlet in the reflector to make sure nothing needs to be done.

There is also a bug report in Connect about this:

I know that this will not solve your problem immediately, but the only way would be to use temporary files to input and output * -CliXml cmdlets.

+1
source

I wrote this based on ConvertFrom-CliXml. This seems to work, although I have not tested it very carefully.

 function ConvertFrom-CliXml { param( [parameter(position=0, mandatory=$true, valuefrompipeline=$true)] [validatenotnull()] [string]$string ) begin { $inputstring = "" } process { $inputstring += $string } end { $type = [type]::gettype("System.Management.Automation.Deserializer") $ctor = $type.getconstructor("instance,nonpublic", $null, @([xml.xmlreader]), $null) $sr = new-object io.stringreader $inputstring $xr = new-object xml.xmltextreader $sr $deserializer = $ctor.invoke($xr) $method = @($type.getmethods("nonpublic,instance") | where-object {$_.name -like "Deserialize"})[1] $done = $type.getmethod("Done", [reflection.bindingflags]"nonpublic,instance") while (!$done.invoke($deserializer, @())) { try { $method.invoke($deserializer, "") } catch { write-warning "Could not deserialize object: $_" } } } } 
0
source

Source: https://habr.com/ru/post/1302962/


All Articles