Comparing two CSV files in powershell and creating an output









up vote
1
down vote

favorite












I'm currently working on a script to import 2 csv files, compare the fqdn columns and output the results to a file.



The issue is after many hours of testing I'm at the point that it looks like my script is working up until the point of getting the path for each file that needs to be imported but I can't seem to get the import-csv command to do what I need it to.



I'd appreciate any guidance you can provide.



My script so far and the error I'm getting are below:



$CMDB_Installed = Get-ChildItem -Path C:Usersnha1DesktopReportsCMDBInstall | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String

$SCOM_AgentList = Get-ChildItem -Path C:Usersnha1DesktopReportsSCOMUAT | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String



$SL = Import-Csv $SCOM_AgentList

$CL = Import-Csv $CMDB_Installed



Compare-Object -ReferenceObject $CL -DifferenceObject $SL -Property fqdn |

Export-Csv -NoTypeInformation -Path = C:Usersnha1DesktopReportsAuditOutputUATNeedsAgent+SCOMUATHosts-NotinSCOM$(get-date -format yyyy-MM-dd).csv


Error Message:



import-csv : Illegal characters in path.

At line:4 char:7

+ $SL = import-csv $SCOM_AgentList

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException

+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand


import-csv : Illegal characters in path.

At line:5 char:7

+ $CL = import-csv $CMDB_Installed

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException

+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand









share|improve this question























  • Allow me to give you the standard advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires 15 or more reputation points). If you found other answers helpful, up-vote them. Accepting (for which you'll gain 2 reputation points) and up-voting help future readers. See this article for more information. If your question isn't fully answered yet, please provide feedback or self-answer.
    – mklement0
    Nov 10 at 19:32














up vote
1
down vote

favorite












I'm currently working on a script to import 2 csv files, compare the fqdn columns and output the results to a file.



The issue is after many hours of testing I'm at the point that it looks like my script is working up until the point of getting the path for each file that needs to be imported but I can't seem to get the import-csv command to do what I need it to.



I'd appreciate any guidance you can provide.



My script so far and the error I'm getting are below:



$CMDB_Installed = Get-ChildItem -Path C:Usersnha1DesktopReportsCMDBInstall | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String

$SCOM_AgentList = Get-ChildItem -Path C:Usersnha1DesktopReportsSCOMUAT | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String



$SL = Import-Csv $SCOM_AgentList

$CL = Import-Csv $CMDB_Installed



Compare-Object -ReferenceObject $CL -DifferenceObject $SL -Property fqdn |

Export-Csv -NoTypeInformation -Path = C:Usersnha1DesktopReportsAuditOutputUATNeedsAgent+SCOMUATHosts-NotinSCOM$(get-date -format yyyy-MM-dd).csv


Error Message:



import-csv : Illegal characters in path.

At line:4 char:7

+ $SL = import-csv $SCOM_AgentList

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException

+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand


import-csv : Illegal characters in path.

At line:5 char:7

+ $CL = import-csv $CMDB_Installed

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException

+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand









share|improve this question























  • Allow me to give you the standard advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires 15 or more reputation points). If you found other answers helpful, up-vote them. Accepting (for which you'll gain 2 reputation points) and up-voting help future readers. See this article for more information. If your question isn't fully answered yet, please provide feedback or self-answer.
    – mklement0
    Nov 10 at 19:32












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm currently working on a script to import 2 csv files, compare the fqdn columns and output the results to a file.



The issue is after many hours of testing I'm at the point that it looks like my script is working up until the point of getting the path for each file that needs to be imported but I can't seem to get the import-csv command to do what I need it to.



I'd appreciate any guidance you can provide.



My script so far and the error I'm getting are below:



$CMDB_Installed = Get-ChildItem -Path C:Usersnha1DesktopReportsCMDBInstall | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String

$SCOM_AgentList = Get-ChildItem -Path C:Usersnha1DesktopReportsSCOMUAT | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String



$SL = Import-Csv $SCOM_AgentList

$CL = Import-Csv $CMDB_Installed



Compare-Object -ReferenceObject $CL -DifferenceObject $SL -Property fqdn |

Export-Csv -NoTypeInformation -Path = C:Usersnha1DesktopReportsAuditOutputUATNeedsAgent+SCOMUATHosts-NotinSCOM$(get-date -format yyyy-MM-dd).csv


Error Message:



import-csv : Illegal characters in path.

At line:4 char:7

+ $SL = import-csv $SCOM_AgentList

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException

+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand


import-csv : Illegal characters in path.

At line:5 char:7

+ $CL = import-csv $CMDB_Installed

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException

+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand









share|improve this question















I'm currently working on a script to import 2 csv files, compare the fqdn columns and output the results to a file.



The issue is after many hours of testing I'm at the point that it looks like my script is working up until the point of getting the path for each file that needs to be imported but I can't seem to get the import-csv command to do what I need it to.



I'd appreciate any guidance you can provide.



My script so far and the error I'm getting are below:



$CMDB_Installed = Get-ChildItem -Path C:Usersnha1DesktopReportsCMDBInstall | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String

$SCOM_AgentList = Get-ChildItem -Path C:Usersnha1DesktopReportsSCOMUAT | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String



$SL = Import-Csv $SCOM_AgentList

$CL = Import-Csv $CMDB_Installed



Compare-Object -ReferenceObject $CL -DifferenceObject $SL -Property fqdn |

Export-Csv -NoTypeInformation -Path = C:Usersnha1DesktopReportsAuditOutputUATNeedsAgent+SCOMUATHosts-NotinSCOM$(get-date -format yyyy-MM-dd).csv


Error Message:



import-csv : Illegal characters in path.

At line:4 char:7

+ $SL = import-csv $SCOM_AgentList

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException

+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand


import-csv : Illegal characters in path.

At line:5 char:7

+ $CL = import-csv $CMDB_Installed

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException

+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand






powershell out-string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 19:31









mklement0

121k20233262




121k20233262










asked Nov 10 at 18:28









Nima

83




83











  • Allow me to give you the standard advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires 15 or more reputation points). If you found other answers helpful, up-vote them. Accepting (for which you'll gain 2 reputation points) and up-voting help future readers. See this article for more information. If your question isn't fully answered yet, please provide feedback or self-answer.
    – mklement0
    Nov 10 at 19:32
















  • Allow me to give you the standard advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires 15 or more reputation points). If you found other answers helpful, up-vote them. Accepting (for which you'll gain 2 reputation points) and up-voting help future readers. See this article for more information. If your question isn't fully answered yet, please provide feedback or self-answer.
    – mklement0
    Nov 10 at 19:32















Allow me to give you the standard advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires 15 or more reputation points). If you found other answers helpful, up-vote them. Accepting (for which you'll gain 2 reputation points) and up-voting help future readers. See this article for more information. If your question isn't fully answered yet, please provide feedback or self-answer.
– mklement0
Nov 10 at 19:32




Allow me to give you the standard advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires 15 or more reputation points). If you found other answers helpful, up-vote them. Accepting (for which you'll gain 2 reputation points) and up-voting help future readers. See this article for more information. If your question isn't fully answered yet, please provide feedback or self-answer.
– mklement0
Nov 10 at 19:32












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted











$CMDB_Installed = ... | Select -expa FullName -First 1 | Out-String 



Don't use Out-String, unless you want a for-display, multi-line string representation.



Your file-path variables therefore contain newlines (line breaks), which Import-Csv complains about, given that newlines in file names are illegal in NTFS (on Windows).




Simply omit the Out-String call, given that Select -expa FullName -First 1 by definition already outputs a string (given that the .FullName property on the objects output by Get-ChildItem is [string]-typed).




To recreate the problem:



PS> Import-Csv "foo`n" # illegal line break in file path
Import-Csv : Illegal characters in path.


To demonstrate that Out-String produces a multi-line string even with a single-line string as input:



PS> ('foo' | Out-String).EndsWith("`n")
True





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242118%2fcomparing-two-csv-files-in-powershell-and-creating-an-output%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted











    $CMDB_Installed = ... | Select -expa FullName -First 1 | Out-String 



    Don't use Out-String, unless you want a for-display, multi-line string representation.



    Your file-path variables therefore contain newlines (line breaks), which Import-Csv complains about, given that newlines in file names are illegal in NTFS (on Windows).




    Simply omit the Out-String call, given that Select -expa FullName -First 1 by definition already outputs a string (given that the .FullName property on the objects output by Get-ChildItem is [string]-typed).




    To recreate the problem:



    PS> Import-Csv "foo`n" # illegal line break in file path
    Import-Csv : Illegal characters in path.


    To demonstrate that Out-String produces a multi-line string even with a single-line string as input:



    PS> ('foo' | Out-String).EndsWith("`n")
    True





    share|improve this answer


























      up vote
      1
      down vote



      accepted











      $CMDB_Installed = ... | Select -expa FullName -First 1 | Out-String 



      Don't use Out-String, unless you want a for-display, multi-line string representation.



      Your file-path variables therefore contain newlines (line breaks), which Import-Csv complains about, given that newlines in file names are illegal in NTFS (on Windows).




      Simply omit the Out-String call, given that Select -expa FullName -First 1 by definition already outputs a string (given that the .FullName property on the objects output by Get-ChildItem is [string]-typed).




      To recreate the problem:



      PS> Import-Csv "foo`n" # illegal line break in file path
      Import-Csv : Illegal characters in path.


      To demonstrate that Out-String produces a multi-line string even with a single-line string as input:



      PS> ('foo' | Out-String).EndsWith("`n")
      True





      share|improve this answer
























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted







        $CMDB_Installed = ... | Select -expa FullName -First 1 | Out-String 



        Don't use Out-String, unless you want a for-display, multi-line string representation.



        Your file-path variables therefore contain newlines (line breaks), which Import-Csv complains about, given that newlines in file names are illegal in NTFS (on Windows).




        Simply omit the Out-String call, given that Select -expa FullName -First 1 by definition already outputs a string (given that the .FullName property on the objects output by Get-ChildItem is [string]-typed).




        To recreate the problem:



        PS> Import-Csv "foo`n" # illegal line break in file path
        Import-Csv : Illegal characters in path.


        To demonstrate that Out-String produces a multi-line string even with a single-line string as input:



        PS> ('foo' | Out-String).EndsWith("`n")
        True





        share|improve this answer















        $CMDB_Installed = ... | Select -expa FullName -First 1 | Out-String 



        Don't use Out-String, unless you want a for-display, multi-line string representation.



        Your file-path variables therefore contain newlines (line breaks), which Import-Csv complains about, given that newlines in file names are illegal in NTFS (on Windows).




        Simply omit the Out-String call, given that Select -expa FullName -First 1 by definition already outputs a string (given that the .FullName property on the objects output by Get-ChildItem is [string]-typed).




        To recreate the problem:



        PS> Import-Csv "foo`n" # illegal line break in file path
        Import-Csv : Illegal characters in path.


        To demonstrate that Out-String produces a multi-line string even with a single-line string as input:



        PS> ('foo' | Out-String).EndsWith("`n")
        True






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 19:32

























        answered Nov 10 at 19:21









        mklement0

        121k20233262




        121k20233262



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242118%2fcomparing-two-csv-files-in-powershell-and-creating-an-output%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric