2

I'm trying to create a batch script with powershell included, but it does not work, as below

@echo off
rename D:\temp\*.csv temp.csv
powershell.exe -Command "& {Import-Csv D:\temp\temp.csv | select ip,hostname | Export-Csv -Path D:\temp\temp01.csv –NoTypeInformation}"
del /F /S /Q D:\temp\temp.csv
powershell -command "& {Rename-Item D:\temp\temp01.csv D:\temp\temp.txt}"
type D:\temp\temp.txt | findstr /v n/s | findstr /v n/a | findstr /v Hostname >> D:\temp\temp01.txt
del /F /S /Q D:\temp\temp.txt
rename D:\temp\temp01.txt temp.txt
powershell -command "& {Rename-Item D:\temp\temp.txt dad.csv}"
powershell -command "& {Get-Content D:\temp\dad.csv | where {$_ -match 'LWKS'} | Set-Content D:\temp\lwks.csv}"
powershell -command "& {Get-Content D:\temp\dad.csv | where {$_ -match 'WKS'} | Set-Content D:\temp\wks.csv}"
exit

But, it worked very well if I did run individual command from the above batch script using cmd. The temp.csv can be found here Thanks for your help.

Muath
  • 4,351
  • 12
  • 42
  • 69
  • 5
    How, excactly, the script does not work? – vonPryz Jul 11 '13 at 09:43
  • my ultimate goal are: #1, remove columns with header name "Ping" and "Ports" + remove the rows contains string "[n/a]" and "[n/s]" "Hostname" in the temp.csv file >>> save it to dad.csv #2, in the new dad.csv file, remove any rows does not contains "lwks" >>> save to lwks.csv, do the same with any row does not contains "wks" >>> save it to wks.csv #3, due to the limit knowledge in scripting, I'm unable to put them all in PowerShell, thanks for your help – user1928098 Jul 12 '13 at 01:16

1 Answers1

0

Ok, your original script was horribly inefficient and badly designed. Here is the powershell to do what you want, no temp files needed. Normally, I wouldn't have nearly this many comments, but I want to make sure everyone understands what I did. The output files will be in the working directory from where you execute this method.

Usage(in cmd/batch): powershell -command "& { . \FileContainsThisFunction.ps1; Extract-Hosts original.csv }"

Usage(in powershell): . \FileContainsThisFunction.ps1; Extract-Hosts original.csv

function Extract-Hosts {
    param(
        [Parameter(Mandatory=$true)]
            [String]$InputFile
    )

    if(-not (Test-Path $InputFile)) { throw ("Input file doesn't exist: {0}" -f $InputFile) }

    # Extract filename without path or extension
    $BaseName = Get-Item $InputFile | Select -ExpandProperty Basename

    # Create a custom object that conains the outfilename and the content for lwks and wks
    $OutLwks = @{ File = ('{0}-lwks.csv' -f $Basename); Content = @() }
    $OutWks = @{ File = ('{0}-wks.csv' -f $Basename); Content = @() }

    # First, delete the output files if they exist
    $OutLwks, $OutWks | ForEach { if (Test-Path -Path:($_.File)) { Remove-Item $_.File } }

    # Import the original csv into the pipeline
    Import-Csv $InputFile |
        # We only care about the IP and Hostname columns
        Select -Property IP, Hostname | 
        # Where the hostname is not empty, nor contains n/a or n/s
        Where { $_.Hostname -iNotMatch '(^$|n/a|n/s)' } | 
        ForEach-Object {
            # If it contains lwks, add it to that list
            if ($_ -imatch 'lwks') { 
                ($OutLwks.Content += $_) 
            }
            # if it contains wks but NOT lwks, add to the other list
            elseif ($_ -imatch 'wks') { 
                ($OutWks.Content += $_) 
            }
        } | Out-Null # Sends objects to null after pipeline processing. 

    # Splat each one into the appropriate CSV file
    $OutLwks, $OutWks | ForEach-Object { 
        $_.Content | Export-Csv -Path $_.File -NoTypeInformation }
    }

Edit: Fixed typo in second Content addition, it should have read OutWks.Content += $_ Edit+: Replaced Where magic with Foreach to make it easier to understand; Added Out-Null to suppress output after pipeline.

Hopefully, this script will take you one step closer to writing rich pipeline processors in powershell.

Eris
  • 7,378
  • 1
  • 30
  • 45
  • Hi Eris, your script worked perfectly, but just done in a half. It was generated 2 files afterward: original-lwks.csv (thanks, 100% done) + original-wks.csv (null content = 0%). Kindly help me for this case, thank you! – user1928098 Jul 14 '13 at 00:58
  • nothing changes, could you please give a try to run your script, the original-dad.csv still have null content. Thanks! – user1928098 Jul 14 '13 at 14:20
  • Awesome, it worked perfectly fine, thanks a billion Eris. Honestly, could you please give me your hand one more time, for these 2 files original-lwks.csv and original-wks.csv, kindly help me to extract the column of IP address without header to txt file, for example: original-lwks.txt and original-wks.txt – user1928098 Jul 15 '13 at 01:13
  • That would be a different question. The script above answers the question as written. – Eris Jul 15 '13 at 01:49
  • yes anyway, that would be enough for me. Thank you so much for your kind help Eris. – user1928098 Jul 15 '13 at 02:31