In a post I did a few months ago, I wrote how it you can retrieve data from Service Manager via a PowerShell script, but I didn’t show much more than just plain output. Earlier this week I was asked if it would be possible to export configuration items in a CSV file. By using the script I wrote months ago, we have all the tools we need. Let’s review the previous script:
We’ll call this script get-object (and it assumes that you’ve already loaded the Microsoft.EnterpriseManagement.Core.dll), and just to remind you, here’s how it works:
PS> get-object microsoft.windows.computer|format-table DisplayName,Netbios*,PrincipalName -au DisplayName NetbiosComputerName NetbiosDomainName PrincipalName ----------- ------------------- ----------------- ------------- JWT-SCDW$ JWT-SCDW WOODGROVE JWT-SCDW.woodgrove.com WIN-752HJBSX24M.woodgrove.com WIN-752HJBSX24M WOODGROVE WIN-752HJBSX24M.woodgrove.com
You provide a class name to it and it retrieves all the instances of that class. Now, to save these instances in a CSV file, I just need to use the Export-CSV cmdlet that is part of the PowerShell distribution and viola!
PS> get-object microsoft.windows.computer|Export-CSV Microsoft.Windows.Computer.csv PS> get-content .\Microsoft.Windows.Computer.csv #TYPE EnterpriseManagementObject#Microsoft.Windows.Computer "PrincipalName","DNSName","NetbiosComputerName","NetbiosDomainName","IPAddres... "JWT-SCDW.woodgrove.com","JWT-SCDW.woodgrove.com","JWT-SCDW","WOODGROVE",,,"S... "WIN-752HJBSX24M.woodgrove.com","WIN-752HJBSX24M.woodgrove.com","WIN-752HJBSX...
You can use this to create a copy of your data, or use it as a way to exchange your data with other applications. Because of the way that other applications use CSV files, you may need to remove the first line of the CSV file which describes what the object was from the PowerShell perspective, but otherwise, you should be able to use the CSV file easily!