Powershell - 免费磁盘空间 - bgcolor结果如何变化 [英] Powershell - free disk space - bgcolor result how change

查看:68
本文介绍了Powershell - 免费磁盘空间 - bgcolor结果如何变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数Get-ComInfo {
param(

电脑
$电脑


$ FreespaceWarning = 30; $ orangeColor =#FBB917

#Here LOW Space thresold小于卷的总大小的10%
$ PercentFree = @ {Name =FreeSpace(GB); Expression = {{0:N1} - f($ _。freespace / 1GB)}} Get-WmiObject Win32_LogicalDisk -filterDriveType = 3 - 计算机$ computers |
选择SystemName,DeviceID,VolumeName,$ PercentFree,@ {Name =Size(GB); Expression = {{0:N1}-f($。size / 1gb)}},@ {name =PercentFree(%); Expression = {int}},@ {Name =LOW SPACE; Expression = {{0:N1}-f($。freespace / $ _。size -lt .1)

}

Get-Content U:\ Users\test\Desktop\servers.txt | ForEach-Object {Get-ComInfo -computers $ _} | ConvertTo-HTML | Out-File U:\Users\test\Desktop\Drives.htm

我想要HTML中可用空间小于5GB的计算机也会以红色显示。谁能帮我?因为,我没有想法。

解决方案

有两种方法可以做到这一点。


  1. 使用 ConvertTo-HTML cmdlet ,然后操作(添加
    css)html。

  2. 构建自己的HTML。

我建议你采用第二种方法。从你的html模板开始,为你的两个模板开始(一个正常,一个用于低磁盘空间的条目)。现在,您可以使用字符串格式构建html,并使用 Out-File cmdlet 将其写入光盘。一>。



示例:

  $ html = 
@'
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Strict // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< title> HTML表格< / title>
< style type =text / css>
.alert {{
background-color:#FBB917}}
< / style>
< / head>
< body>
< table>
colgroup>< col />< col />< col />< col />< col />< col />< col /> < / COLGROUP>第b个第i个第i个系统名称第t个第i个第i个系统名称第t个DeviceID第r个卷名第r个第n个空闲空间(GB)< th>< th>< th size> size(GB)< th> PercentFree(%)< th> LOW SPACE< / th>< / tr>
{0}
< / table>
< / body>< / html>
'@

$ entryTemplate ='< tr>< td> {0}< / td>< td> {1}< / td>< td> {2}< / TD>< TD> {3}< / TD>< TD> {4}< / TD>< TD> {5}< / TD>< TD> {6- }< / td>< / tr>'
$ alertEntryTemplate ='< tr class =alert>< td> {0}< / td>< td> {1}< ; / TD>< TD> {2}< / TD>< TD> {3}< / TD>< TD> {4}< / TD>< TD> {5}< / td>< td> {6}< / td>< / tr>'


函数Get-ComInfo {
param(
$ computers


$ FreespaceWarning = 30; $ orangeColor =#FBB917

#Here LOW Space thresold小于卷的总大小的10%
$ PercentFree = @ {Name =FreeSpace(GB); Expression = {{0:N1}-f($ _。freespace / 1GB)}}
Get-WmiObject Win32_LogicalDisk -filterDriveType = 3--computer $ computers |
选择SystemName,DeviceID,VolumeName,$ PercentFree,@ {Name =Size(GB); Expression = {{0:N1}-f($ _。size / 1gb)}},@ {表达式= {{0:N1}-f($ _。freespace / $ _。size -lt。 1)}}

}

$ entries = Get-Content U:\ Users\test\Desktop\servers.txt | %{Get-ComInfo -computers $ _} | %{
if([float] :: Parse($ _。'FreeSpace(GB)')-le 5){
$ alertEntryTemplate -f $ _。SystemName,$ _。DeviceID,$ _ .VolumeName,$ _''FreeSpace(GB)',$ _''Size(GB)',$ _''PercentFree(%)',$ _。'LOW SPACE'
}
else {
$ entryTemplate -f $ _。SystemName,$ _。DeviceID,$ _。VolumeName,$ _。'FreeSpace(GB)',$ _。'Size(GB)',$ _。'PercentFree( %)',$ _。'低空'
}
}

$ html -f($ entries -join'')| out-file U:\ Users\test\Desktop\Drives.htm


I have this script:

Function Get-ComInfo {   
param(

Computers
$computers

)
$FreespaceWarning = 30; $orangeColor = "#FBB917"

#Here LOW Space thresold is lessthan 10% of the total size of the Volume
$PercentFree = @{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace /1GB)}} Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $computers |
Select SystemName,DeviceID,VolumeName,$PercentFree,@{Name="Size(GB)";Expression={"{0:N1}" -f($.size/1gb)}},@{name="PercentFree(%)";Expression={int}}, @{Name="LOW SPACE";Expression={"{0:N1}" -f($.freespace / $_.size -lt .1)}}

}

Get-Content U:\Users\test\Desktop\servers.txt | ForEach-Object { Get-ComInfo -computers $_} | ConvertTo-HTML | Out-File U:\Users\test\Desktop\Drives.htm

I want a computer that has less than 5GB of free space too appear in red color in the HTML. Who can help me? Because, I dont have ideas.

解决方案

There are two ways to do this.

  1. Write your HTML using the ConvertTo-HTML cmdlet and manipulate (add css) the html after.
  2. Build your own HTML.

I would recommend you the second approach. Start with a template for your html and for your two for your entries (one "normal" and one for the entries with low disc space). Now you can build your html using string format and write it to disc using the Out-File cmdlet.

Example:

$html=
@'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
 <style type="text/css">
  .alert {{
    background-color: #FBB917 }}
  </style>
</head>
<body>
<table>
<colgroup><col/><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>SystemName</th><th>DeviceID</th><th>VolumeName</th><th>FreeSpace(GB)</th><th>Size(GB)</th><th>PercentFree(%)</th><th>LOW SPACE</th></tr>
{0}
</table>
</body></html>
'@

$entryTemplate = '<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>'
$alertEntryTemplate = '<tr class="alert"><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>'


Function Get-ComInfo {   
param(
$computers

)
$FreespaceWarning = 30; $orangeColor = "#FBB917"

#Here LOW Space thresold is lessthan 10% of the total size of the Volume
$PercentFree = @{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace /1GB)}} 
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $computers |
Select SystemName,DeviceID,VolumeName,$PercentFree,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{name="PercentFree(%)";Expression={int}}, @{Name="LOW SPACE";Expression={"{0:N1}" -f($_.freespace / $_.size -lt .1)}}

}

$entries = Get-Content U:\Users\test\Desktop\servers.txt | % { Get-ComInfo -computers $_ } | % {
    if ([float]::Parse($_.'FreeSpace(GB)') -le 5) {
        $alertEntryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }
    else {
        $entryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }
}

$html -f ($entries -join ' ') | out-file U:\Users\test\Desktop\Drives.htm

这篇关于Powershell - 免费磁盘空间 - bgcolor结果如何变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆