Tuesday, July 10, 2007

thinking in powershell

thanks to Dennis Verweij and his blog Just Powershell it for this informative information on how to begin thinking in powershell

code using commandletts

#-- Get-ChildItemForEach-Object {if (test-path ./$_/rapporten){$dirname=$_.name;Get-ChildItem ./$dirname/reports}}Select-Object @{e={$dirname};n="homedir"},@{e={$_.name};n="report"}Export-Csv $env:systemroot\temp\reports.csv
#--

same code using aliases

#-- ls% {if (test-path ./$_/rapporten){$dirname=$_.name;ls ./$dirname/reports}}select @{e={$dirname};n="homedir"},@{e={$_.name};n="report"}export-csv $env:systemroot\temp\reports.csv
#--

thinking in powershell example referencing the above code

This may look like a lot of typing, but believe me, after a while, you'll type it in the command line as if it is you're native language.

Just type what you're thinking and it will all fall in place :

I want a directorylisting = ls
With the output I want to .. =
For every childitem it want.. = % {
If the directory exists If want to do the following = if (test-path ......){
remember the directory you search = $dirname=$_.name
and list the content of the report directory in this directory = ls ./$dirname/reports
End the "if" = }
End the "foreach"= }
With the output I want to .. =
and from this output only select the homedirectory and the name of the report in the report directory and name these "homedir" and "report" = select @{e={$dirname};n="homedir"},@{e={$_.name};n="report"}
With the output I want to .. =
export this data to a csv file in systemroot\temp and name it reports.csv = export-csv $env:systemroot\temp\reports.csv

No comments: