powershell in batch file -


i'm trying create batch script powershell included, not work, 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 | {$_ -match 'lwks'} | set-content d:\temp\lwks.csv}" powershell -command "& {get-content d:\temp\dad.csv | {$_ -match 'wks'} | set-content d:\temp\wks.csv}" exit 

but, worked if did run individual command above batch script using cmd. temp.csv can found here help.

ok, original script horribly inefficient , badly designed. here powershell want, no temp files needed. normally, wouldn't have many comments, want make sure understands did. output files in working directory execute 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 custom object conains outfilename , content lwks , wks     $outlwks = @{ file = ('{0}-lwks.csv' -f $basename); content = @() }     $outwks = @{ file = ('{0}-wks.csv' -f $basename); content = @() }      # first, delete output files if exist     $outlwks, $outwks | foreach { if (test-path -path:($_.file)) { remove-item $_.file } }      # import original csv pipeline     import-csv $inputfile |         # care ip , hostname columns         select -property ip, hostname |          # hostname not empty, nor contains n/a or n/s         { $_.hostname -inotmatch '(^$|n/a|n/s)' } |          foreach-object {             # if contains lwks, add list             if ($_ -imatch 'lwks') {                  ($outlwks.content += $_)              }             # if contains wks not lwks, add other list             elseif ($_ -imatch 'wks') {                  ($outwks.content += $_)              }         } | out-null # sends objects null after pipeline processing.       # splat each 1 appropriate csv file     $outlwks, $outwks | foreach-object {          $_.content | export-csv -path $_.file -notypeinformation }     } 

edit: fixed typo in second content addition, should have read outwks.content += $_ edit+: replaced magic foreach make easier understand; added out-null suppress output after pipeline.

hopefully, script take 1 step closer writing rich pipeline processors in powershell.


Comments