0

I'm new to Powershell. I would like the code to output results to a new folder and name it with a computer name variable. If the folder already exists, it should just output the result into that folder.

What is the best way to do it?

So far I have the following output code:

$dir = "C:\"

$count = @{}
$size = @{}
$hostname = @{}
gci $dir -recurse |%{
[int]$count[$_.extension] += 1
[int64]$size[$_.extension] += $_.length
}
$results = @()
$count.keys | sort |% {
$result = ""|select extension,count,size,hostname
$result.extension = $_
$result.count = $count[$_]
$result.size = [math]::round($size[$_] /1Gb, 3)
$result.hostname = $(get-content env:computername)
$results += $result
}
$results | ft -auto

$dirName = "C:\inetpub\wwwroot\${Env:ComputerName}"
if (!(Test-Path $dirName)) { mkdir $dirName }


$a = "<style>"
$a = $a + "BODY{background-color:#A987CC;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;.center { margin:auto; width:70%; };}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#99CCFF}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
$a = $a + "</style>"


$results | sort-object -property size -Descending | select-object -first 30 | ConvertTo-Html extension,count,size, hostname "$a" -title "JUST TESTING :)" -body "I WAS HERE! :)" | 
Set-Content C:\inetpub\wwwroot\${Env:ComputerName}\"$env:computername-$(get-date -f dd-MM-yyyy-hh-mm)".htm
3
  • And in what way is your code not working as it should? Commented Jul 5, 2013 at 13:51
  • If that code is doing what you need it to do, go with it. Do you have a specific concern with it? Commented Jul 5, 2013 at 13:54
  • thanks for your reply. Yes, I just needed the code that Joey suggested, so that new folder is generated and named with the hostname and then output was generated in that folder. I've updated my question code so that you can see the whole code. Commented Jul 5, 2013 at 15:33

2 Answers 2

1
$dirName = "C:\inetpub\wwwroot\${Env:ComputerName}"
if (!(Test-Path $dirName)) { mkdir $dirName }

maybe?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I've incorporated your code id it works great! I also used $dirName in the output string so that .htm file is generated in the folder.
0

It looks like you've created a script that reports on the disk space taken up by files of a specific extension. Instead of calculating that information yourself, I recommend the Group-Object cmdlet to group all the files by their extension, and the Measure-Object cmdlet to sum their sizes. I would also use a here string for your <style> block in place of string concatenation.

$dir = "C:\"

# Get all the files, ignoring errors because we won't have access to some directories
Get-ChildItem $dir -Recurse -ErrorAction SilentlyContinue |
    # We don't want any directories
    Where-Object { -not $_.PsIsContainer } |
    # Group by extension
    Group-Object Extension |
    ForEach-Object {
        # Sum the lenghts of all files in this group/extension
        $totalSize = $_.Group | 
                         Measure-Object -Sum Length | 
                         Select-Object -Expand Sum
        # Add dynamic properties Size and SizeInDB properties for our report
        $_ | 
            Add-Member NoteProperty -Name Size -Value $totalSize -PassThru |
            Add-Member ScriptProperty -Name SizeInGB -Value { [math]::round($this.Size/1GB,3) } -PassThru |
            Add-Member NoteProperty -Name HostName -Value ($env:ComputerName) -PassThru
    } |
    # Sort by size, biggest at the top
    Sort-Object Size -Descending |
    # Save the results in the $results variable
    Tee-Object -Variable results
    # Show a report in the script output
    Format-Table Name,Count,SizeInGB -AutoSize

$dirName = "C:\inetpub\wwwroot\${Env:ComputerName}"
if (!(Test-Path $dirName))
{ 
    mkdir $dirName 
}

$style = @'
<style>
    BODY{background-color:#A987CC;}
    TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;.center { margin:auto; width:70%; };}
    TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#99CCFF}
    TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}
</style>"
'@

$results |
    # Grab the 30 extensions taking up the most space and create an HTML report
    Select-Object -First 30 |
    ConvertTo-Html Name,Count,SizeInGB,HostName "$a" -title "JUST TESTING :)" -body "I WAS HERE! :)" | 
    Set-Content (Join-Path $dirName du.html)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.