Input Variables to File

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Jim Tibbets Jim Tibbets 2 years, 2 months ago.

  • Author
    Posts
  • #19224
    Profile photo of Jim Tibbets
    Jim Tibbets
    Participant
    $PSComputerName = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Select-Object PSComputerName | ft -HideTableHeaders
    $IPAddress = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Select-Object IPAddress | ft -HideTableHeaders
    $IPSubnet = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Select-Object IPSubnet | ft -HideTableHeaders
    

    I'm making a script to back up the IP Address and Subnet Mask on a particular machine. Then after I reinstall I plan to restore all of this data to the reinstalled machine. I've got the needed data as well as the computer name in variables, however I've had hit and miss luck with this... Initially I wanted a CSV (I don't know why, I just did) but at this point I don't care, as long as I can get the data back... I don't want to do it the traditional way either because I will have to repeat this process on 10-11 more machines... This is my first post on here, and I look forward to any input! Thanks in advance!

  • #19227
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Once you've formatted something (FT), it's almost impossible to do anything useful with it. See "The Big Book of PowerShell Gotchas" for an explanation of that.

    I'd suggest exporting to CliXML rather than formatting. CliXML is a good format for preserving data, and can be re-imported fairly easily.

    Also, there's no need to execute the query three times – that's wasteful. Just get Win32_NetworkAdapterConfiguration, don't Select anything, and pipe it to Export-CliXML.

    Get-WmiObject Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | Export-CliXML data.xml
    

    You can then import it.

    $data = Import-CLiXML data.xml
    $computername = $data.pscomputername
    

    All the properties will remain accessible.

  • #19263
    Profile photo of Jim Tibbets
    Jim Tibbets
    Participant

    Oh my God! Don Jones answered my question... I heard your name on MVA...

    Anyway, THANKS! I did that. I decided before I saw this response that I was going to experiment with XML; this reinforced that idea and it works better than I originally planned for it to work!

    Thanks for the quick response!

You must be logged in to reply to this topic.