如何在Powershell中使用computer name变量创建文件夹并命名?

如何在Powershell中使用computer name变量创建文件夹并命名?,powershell,Powershell,我是新来的。我希望代码输出结果到一个新的文件夹,并命名为计算机名称变量。如果文件夹已经存在,它应该将结果输出到该文件夹中 最好的方法是什么 到目前为止,我有以下输出代码: $dir = "C:\" $count = @{} $size = @{} $hostname = @{} gci $dir -recurse |%{ [int]$count[$_.extension] += 1 [int64]$size[$_.extension] += $_.length } $results = @()

我是新来的。我希望代码输出结果到一个新的文件夹,并命名为计算机名称变量。如果文件夹已经存在,它应该将结果输出到该文件夹中

最好的方法是什么

到目前为止,我有以下输出代码:

$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
$dir=“C:\”
$count=@{}
$size=@{}
$hostname=@{}
gci$dir-递归|%{
[int]$count[$\.extension]+=1
[int64]$size[$\扩展名]+=$\长度
}
$results=@()
$count.keys |排序|%{
$result=”“|选择扩展名、计数、大小、主机名
$result.extension=$_
$result.count=$count[$\uU1]
$result.size=[math]::四舍五入($size[$\]/1Gb,3)
$result.hostname=$(获取内容环境:computername)
$results+=$result
}
$results | ft-自动
$dirName=“C:\inetpub\wwwroot\${Env:ComputerName}”
如果(!(测试路径$dirName)){mkdir$dirName}
$a=“”
$a=$a+“正文{背景色:#A987CC;}”
$a=$a+“表{边框宽度:1px;边框样式:纯色;边框颜色:黑色;边框折叠:折叠;.center{margin:auto;宽度:70%;};}”
$a=$a+“TH{边框宽度:1px;填充:0px;边框样式:纯色;边框颜色:黑色;背景颜色:#99CCFF}”
$a=$a+“TD{边框宽度:1px;填充:0px;边框样式:纯色;边框颜色:黑色;背景颜色:淡黄色}”
$a=$a+“”
$results | sort object-property size-Descending | select object-first 30 |转换为Html扩展名、计数、大小、主机名“$a”-title“JUST TESTING:)”-body“我在这里!:)”|
设置内容C:\inetpub\wwwroot\${Env:ComputerName}\“$Env:ComputerName-$(获取日期-f dd-MM yyy-hh-MM)”.htm

可能吧?

看起来您创建了一个脚本,用于报告特定扩展名文件占用的磁盘空间。我建议使用
Group-Object
cmdlet按扩展名对所有文件进行分组,并使用
Measure-Object
cmdlet对文件大小进行合计,而不是自己计算这些信息。我也会在这里使用
字符串
作为
块来代替字符串连接

$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)
$dir=“C:\”
#获取所有文件,忽略错误,因为我们无法访问某些目录
Get ChildItem$dir-Recurse-ErrorAction SilentlyContinue|
#我们不想要任何目录
其中对象{-not$\ PsIsContainer}|
#分门别类
组对象扩展|
ForEach对象{
#此组/扩展名中所有文件的长度总和
$totalSize=$\组|
测量对象-和长度|
选择对象-展开和
#为报表添加动态属性Size和SizeInDB属性
$_ | 
添加成员NoteProperty-名称大小-值$totalSize-PassThru|
添加成员ScriptProperty-Name SizeInGB-Value{[math]::round($this.Size/1GB,3)}-PassThru|
添加成员NoteProperty-Name主机名-Value($env:ComputerName)-PassThru
} |
#按大小排序,顶部最大
排序对象大小-降序|
#将结果保存在$results变量中
Tee对象-变量结果
#在脚本输出中显示报告
格式表名、计数、大小b-自动调整大小
$dirName=“C:\inetpub\wwwroot\${Env:ComputerName}”
if(!(测试路径$dirName))
{ 
mkdir$dirName
}
$style=@'
正文{背景色:#A987CC;}
表{边框宽度:1px;边框样式:实心;边框颜色:黑色;边框折叠:折叠;.center{margin:auto;宽度:70%;};}
TH{边框宽度:1px;填充:0px;边框样式:纯色;边框颜色:黑色;背景颜色:#99CCFF}
TD{边框宽度:1px;填充:0px;边框样式:纯色;边框颜色:黑色;背景颜色:淡黄色}
"
'@
$results|
#抓取占用最多空间的30个扩展并创建一个HTML报告
选择对象-前30个|
转换为Html名称、计数、大小、主机名“$a”-标题“刚刚测试:)”-body“我在这里!”)" | 
设置内容(连接路径$dirName du.html)

您的代码在哪些方面没有发挥应有的作用?如果该代码正在发挥您需要的作用,请继续。您对此有什么特别的担忧吗?谢谢您的回复。是的,我只需要Joey建议的代码,这样就可以生成新文件夹并使用主机名命名,然后在该文件夹中生成输出。我已经更新了我的问题代码,这样您就可以看到整个代码了。谢谢!我已经合并了您的代码id,效果很好!我还在输出字符串中使用了$dirName,以便在文件夹中生成.htm文件。
$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)