遇到“Get-Credentials'关于Powershell脚本

时间:2016-10-27 11:10:31

标签: powershell cmdlets

我已创建了以下Powershell脚本,但我收到了“拒绝访问/ RPC服务器不可用”#39;查询这些远程VM时出错。我无法启动WMI并且希望添加一些代码以提示我的Windows Credenaitls,因为这可以远程工作,而不是通过Powershell作为解决方法。

这可能是港口的问题吗? WMI服务是否会被停止,即使使用我的证书也阻止我唱歌?

由于此剧本将由学生使用,我只想添加一个没有默认用户名/密码的提示,但在添加$Cred = Get-Credentials时我收到错误;

  

意外的令牌')'在表达或陈述中。   + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException   + FullyQualifiedErrorId:MissingExpressionAfterToken

我的代码如下 - 我还需要帮助添加部分-credentials $Cred(如果这是正确的);

Param(
$Cred = Get-Credentials,
  [string]$path = "C:\LON3 Web Server Report - $(get-date -f dd_MM_yyyy).html",
  [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 
       Server=$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 - LON3 Web Servers</h1>" -PostContent $upTime, $disk, $memory >> $path  
Invoke-Item $path

1 个答案:

答案 0 :(得分:2)

没有S的凭证,括号内的函数。

Param(
$Cred = $(Get-Credential),
  [string]$path = "C:\LON3 Web Server Report - $(get-date -f dd_MM_yyyy).html",
  [array]$servers = @("Svr1","Svr2","Svr3")
)

顺便提一下,你的参数声明会看起来像脚本约定:

[CmdLetBinding()]
Param ( [Parameter(Mandatory=$false,Position=0)][pscredential]$credential = $(Get-Credential),
        [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")