CLIXML export and then import again

So I love that you can pipe in and out of things so smoothly in PowerShell.  But what if that piping needed to be stored externally and brought back in by another script or even another computer?

I thought CLIXML was the key but I guess it's not working the way I want.

Take the following example:

Get-Printer | Where {($_.PortName -match "10.110.") -and ($_.Type -match "Connection")} | Export-Clixml -Path ($OutputPath + "\MappedPrinters.XML")

I would hope that this command would work as one might expect:

Import-Clixml -Path ($OutputPath + "\MappedPrinters.XML") | Add-Printer

But that results in several instances of this error:

Add-Printer : One or more parameter values passed to the method were invalid.
At line:1 char:61
+ Import-Clixml -Path ($OutputPath + "\MappedPrinters.XML") | Add-Printer
+                                                             ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (MSFT_Printer:ROOT/StandardCimv2/MSFT_Printer) [Add-Printer], CimException
    + FullyQualifiedErrorId : MI RESULT 4,Add-Printer

Is there a way that this should work when we need to save some objects for later use?

August 26th, 2015 4:20pm

Add-Printer cannot be used like that.

Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 6:14pm

Hi Stonent,

Regarding to the add-printer within powershell, please check the below article it provides you some examples:

https://technet.microsoft.com/en-us/library/hh918353(v=wps.630).aspx

Hope it helps.

Best Regards,

Elaine

August 28th, 2015 4:51am

Hi Stonent,

yes actually this can be made to work. Not the way you are using Add-Printer (it can't take this kind of input from the pipeline), but some other way. The properties are there, their types should remain valid.

Here an example that might be made to work, even though it probably won't work unchanged:

$Printers = Import-Clixml "printers.xml"
foreach ($Printer in $Printers)
{
    Add-Printer @Printer
}

With splatting (using that "@" symbol instead of "$") you bind the properties on an object to parameters that have the same name as the property name. You can remove or rename properties using the Select-Object cmdlet.

Soo, with a little trial and error, this can probably be made to work just fine.

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 8:55am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics