Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell到HTML-我可以';t在每个服务器之间添加中断/新行';磁盘空间';信息_Html_Powershell - Fatal编程技术网

Powershell到HTML-我可以';t在每个服务器之间添加中断/新行';磁盘空间';信息

Powershell到HTML-我可以';t在每个服务器之间添加中断/新行';磁盘空间';信息,html,powershell,Html,Powershell,我有下面的Powershell脚本来查询几个远程VM上的正常运行时间/磁盘空间/内存使用情况,但生成的结果集中在一起,当我将剩余的服务器添加到此脚本(大约要添加30/40多个)时,数据开始变得不可读 [CmdLetBinding()] Param ( [Parameter(Mandatory=$false,Position=1)][string]$path = "C:\LON3 Web Server Report - $(get-date -f dd_MM_yyyy).html",

我有下面的Powershell脚本来查询几个远程VM上的正常运行时间/磁盘空间/内存使用情况,但生成的结果集中在一起,当我将剩余的服务器添加到此脚本(大约要添加30/40多个)时,数据开始变得不可读

[CmdLetBinding()]
Param ( [Parameter(Mandatory=$false,Position=1)][string]$path = "C:\LON3 Web Server Report - $(get-date -f dd_MM_yyyy).html", 
        [Parameter(Mandatory=$false,Position=2)][array]$servers = @("Svr1","Svr2","Svr3")
)

Function Get-UpTime 
{ Param ([string[]]$servers) 
  Foreach ($s in $servers)  
   {  
     $os = Get-WmiObject -class win32_OperatingSystem -cn $s  
     New-Object psobject -Property @{  
       Uptime = ((get-date) - $os.converttodatetime($os.lastbootuptime)).Days 
       Hostname=$s  
         }
        }     
       } 

Function Get-DiskSpace 
{ 
 Param ([string[]]$servers) 
  Foreach ($s in $servers)  
   {  
     Get-WmiObject -Class win32_volume -cn $s |
       Select-Object @{LABEL='Server';EXPRESSION={$s}}, 
         driveletter, label,
         @{LABEL=" Total (GB)"; Expression = {" {0:N2} " -f ($_.capacity / 1GB)}}, 
         @{LABEL=" Free Space (GB) ";EXPRESSION={" {0:N2} " -f ($_.freespace/1GB)}},
         @{LABEL=" Percent Free (%) ";Expression = { "{0:N2} " -f (($_.FreeSpace / $_.Capacity)*100) }},
         @{LABEL=" Percent Usage (%) ";Expression = {" {0:N2} " -f ((($_.Capacity - $_.freespace)/$_.Capacity)*100) }}           
    }
}

Function Get-MemoryUsage 
{ 
 Param ([string[]]$servers) 
  Foreach ($s in $servers)  
   {  
     Get-WmiObject -Class win32_OperatingSystem -cn $s |
       Select-Object @{LABEL='Server';EXPRESSION={$s}}, 
         @{LABEL=" Total Physical Memory (MB) "; Expression = {" {0:N0} " -f (($_.totalvisiblememorysize / 1024))}},
         @{LABEL=" Free Physical Memory (MB) "; Expression = {" {0:N0} " -f (($_.freephysicalmemory / 1024))}},
         @{LABEL=" Total Virtual Memory (MB) "; Expression = {" {0:N0} " -f (($_.totalvirtualmemorysize / 1024))}}, 
         @{LABEL=" Free Virtual Memory (MB) "; Expression = {" {0:N0} " -f (($_.freevirtualmemory / 1024))}}     
    }
}

$upTime = Get-UpTime -servers $servers |  
ConvertTo-Html -As Table -Fragment -PreContent " 
  Created on: $(get-date)
  <h2>Server Uptime</h2> " | Out-String 

$disk = Get-DiskSpace -servers $servers |  
ConvertTo-Html -As Table -Fragment -PreContent " 
  <h2>Disk</h2> "| Out-String

$memory = Get-MemoryUsage -servers $servers |  
ConvertTo-Html -As Table -Fragment -PreContent " 
  <h2>Memory Usage</h2> "| Out-String     


$head = @'
<style media='screen'>
body {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
}

table{
border-collapse: collapse;
border: none;
font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;
color: black;
margin-bottom: 10px;
}

table td{
font-size: 12px;
padding-left: 0px;
padding-right: 20px;
text-align: left;
}

table th {
font-size: 12px;
font-weight: bold;
padding-left: 0px;
padding-right: 20px;
text-align: left;
}

h2{ clear: both; font-size: 130%;color:#354B5E; }

h3{
clear: both;
font-size: 75%;
margin-left: 20px;
margin-top: 30px;
color:#475F77;
}

p{ margin-left: 20px; font-size: 12px; }

table.list{ float: left; }

table.list td:nth-child(1){
font-weight: bold;
border-right: 1px grey solid;
text-align: right;
}

table.list td:nth-child(2){ padding-left: 7px; }
table tr:nth-child(even) td:nth-child(even){ background: #BBBBBB; }
table tr:nth-child(odd) td:nth-child(odd){ background: #F2F2F2; }
table tr:nth-child(even) td:nth-child(odd){ background: #DDDDDD; }
table tr:nth-child(odd) td:nth-child(even){ background: #E5E5E5; }
div.column { width: 320px; float: left; }
div.first{ padding-right: 20px; border-right: 1px grey solid; }
div.second{ margin-left: 30px; }
table{ margin-left: 20px; }
–>
</style>
<style media='print'>

</style>
'@

ConvertTo-Html -Head $head -PreContent "<h1>Daily Status Report - SQL Servers</h1>" -PostContent $upTime, $disk, $memory >> $path  
Invoke-Item $path
[CmdLetBinding()]
Param([Parameter(Mandatory=$false,Position=1)][string]$path=“C:\LON3 Web服务器报告-$(get date-f dd_MM_yyyy).html”,
[参数(必需=$false,位置=2)][array]$servers=@(“Svr1”、“Svr2”、“Svr3”)
)
函数获取正常运行时间
{Param([string[]]$servers)
Foreach($s,单位为$server)
{  
$os=获取WMIOObject-类win32_操作系统-cn$s
新对象psobject-属性@{
正常运行时间=((获取日期)-$os.converttodatetime($os.lastbootuptime))。天
主机名=$s
}
}     
} 
函数获取磁盘空间
{ 
Param([string[]]$servers)
Foreach($s,单位为$server)
{  
获取WmiObject-类win32_卷-cn$s|
选择对象@{LABEL='Server';表达式={$s},
字母、标签、,
@{LABEL=“Total(GB)”;表达式={{0:N2}”-f($\容量/1GB)},
@{LABEL=“空闲空间(GB)”;表达式={{0:N2}”-f($\ freespace/1GB)},
@{LABEL=“自由百分比(%)”;表达式={{0:N2})-f($\.FreeSpace/$\.Capacity)*100)},
@{LABEL=“使用率百分比(%)”;表达式={{0:N2})-f(($.Capacity-$.freespace)/$.Capacity)*100)}
}
}
函数Get MemoryUsage
{ 
Param([string[]]$servers)
Foreach($s,单位为$server)
{  
获取WmiObject-类win32_OperatingSystem-cn$s|
选择对象@{LABEL='Server';表达式={$s},
@{LABEL=“总物理内存(MB)”;表达式={{0:N0}”-f($\ totalvisiblememorysize/1024))},
@{LABEL=“空闲物理内存(MB)”;表达式={{0:N0})-f(($.freephycalmemory/1024))},
@{LABEL=“总虚拟内存(MB)”;表达式={{0:N0}”-f($\ totalvirtualmemorysize/1024))},
@{LABEL=“空闲虚拟内存(MB)”;表达式={{0:N0}”-f($\ freevirtualmemory/1024))}
}
}
$upTime=获取正常运行时间-服务器$servers |
转换为Html-作为表-片段-预内容“
创建日期:$(获取日期)
服务器正常运行时间“|输出字符串
$disk=获取磁盘空间-服务器$servers |
转换为Html-作为表-片段-预内容“
磁盘“|输出字符串
$memory=Get MemoryUsage-servers$servers |
转换为Html-作为表-片段-预内容“
内存使用“|输出字符串
$head=@'
身体{
字体系列:Verdana、Geneva、Arial、Helvetica、sans serif;
}
桌子{
边界塌陷:塌陷;
边界:无;
字体:10号Verdana,日内瓦,Arial,Helvetica,无衬线;
颜色:黑色;
边缘底部:10px;
}
表td{
字体大小:12px;
左侧填充:0px;
右边填充:20px;
文本对齐:左对齐;
}
表th{
字体大小:12px;
字体大小:粗体;
左侧填充:0px;
右边填充:20px;
文本对齐:左对齐;
}
h2{清除:两个;字体大小:130%;颜色:354B5E;}
h3{
明确:两者皆有;
字体大小:75%;
左边距:20px;
边缘顶部:30px;
颜色:#475F77;
}
p{左边距:20px;字体大小:12px;}
表.列表{float:left;}
表4.1 td:n个孩子(1){
字体大小:粗体;
右边框:1px灰色实体;
文本对齐:右对齐;
}
table.list td:n子(2){左填充:7px;}
表tr:n个孩子(偶数)td:n个孩子(偶数){背景:#bbbbbbbb;}
表tr:n个孩子(奇数)td:n个孩子(奇数){背景:#f2f2;}
表tr:n个孩子(偶数)td:n个孩子(奇数){背景:#dddddddd;}
表tr:n个孩子(奇数)td:n个孩子(偶数){背景:#e5;}
div.column{width:320px;float:left;}
div.first{右填充:20px;右边框:1px灰色实心;}
第二节{左边距:30px;}
表{左边距:20px;}
–>
'@
转换为Html-Head$Head-PreContent“每日状态报告-SQL Server”-后内容$upTime,$disk,$memory>>$path
调用项$path
下图使其更容易理解。我基本上需要在每台服务器之间显示一个“新行”或“中断”(用“红线”标记),但我无法找出实现这一点的Powershell/HTML编码


完成分离的最简单方法可能只是在每个集合的底部添加一个空对象,这至少会在每个服务器之间给您一个换行符。因此,您可以在
Get DiskSpace
函数的底部添加类似的内容

Function Get-DiskSpace 
{ 
 Param ([string[]]$servers) 
  Foreach ($s in $servers)  
   {  
     Get-WmiObject -Class win32_volume -cn $s |
       Select-Object @{LABEL='Server';EXPRESSION={$s}}, 
         driveletter, label,
         @{LABEL=" Total (GB)"; Expression = {" {0:N2} " -f ($_.capacity / 1GB)}}, 
         @{LABEL=" Free Space (GB) ";EXPRESSION={" {0:N2} " -f ($_.freespace/1GB)}},
         @{LABEL=" Percent Free (%) ";Expression = { "{0:N2} " -f (($_.FreeSpace / $_.Capacity)*100) }},
         @{LABEL=" Percent Usage (%) ";Expression = {" {0:N2} " -f ((($_.Capacity - $_.freespace)/$_.Capacity)*100) }}           
}
    [PsCustomObject]@{Server = "";driveletter = "";label = ""; "Total (GB)" = ""; "Free Space (GB)" = ""; "Percent Free (%)" = ""; "Percent Usage (%)" = ""}
}


请注意,这可能不是最好的方法,因为很难将该方法扩展到其他脚本,但它将实现您所需的功能。

抱歉,似乎我输入了错误的报价。但是,如果您像我在答案中添加的那样修改
Get DiskSpace
函数,它应该可以工作。您真是个传奇人物!谢谢,工作做得很好!这让我很恼火,因为据说30多台服务器中的每台都有4个驱动器!试图找出一个真正简单的Powershell表单,在上面有4个按钮来运行其中4个scrips/。ps1很高兴我能帮上忙!如果您刚刚开始使用WPF创建GUI,您可能想看看这里的一些文章-(浏览网站以查找他的WPF文章可能有点棘手,但其中有一些)或者,对于一个更基本的入门知识,您可以查看我在这里所做的一篇关于这个主题的博文——如果您使用这种方法,GUI将在脚本在后台运行时锁定,您可以使用运行空间来克服这一点,但所涉及的代码要多得多。