完成powershell脚本

时间:2016-04-14 01:12:29

标签: powershell

我在尝试完成此脚本时遇到了困难。基本上我想查看剩余的内存,检查前3个内存占用,显示上次重启的时间,显示上次更新,显示自动但已停止的服务,并显示我在服务器上有多少空间硬盘空间。

Write-Host "Getting the information required" -ForeGroundColor green
Function Get-Checks {
  $Output = "C:\users\b2badmin\desktop\checklist\check$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).txt"
  #Get the computer name
  $env:computername | out-file -Append $Output
  #Show Available memory
  Get-Counter -ComputerName localhost '\Memory\Available MBytes' |
    Select-Object -ExpandProperty countersamples |
    Select-Object -Property Path, cookedvalue |
    Out-File -Append $Output
  #Show the processes that are using the most resources top 3
  Get-Process | Sort-Object -Descending WS |
    select -First 3 |
    Format-Table -Property WS,ProcessName |
    Out-File -Append $Output
  #Show last reboot
  Get-WmiObject win32_operatingsystem |
    select csname, @{LABEL=’LastBootUpTime’;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}} |
    Out-File -Append $Output
  #Show the last installed Hotfix for windows updates
  Get-HotFix | Select -Last 1 |
    Format-List -Property InstalledOn,Description,HotfixI |
    Out-File -Append $Output
  #Get the services that are Automatically started and list them if they are stopped
  Get-WmiObject Win32_Service |
    Where-Object { $_.StartMode -eq 'Auto' -and $_.State -ne 'Running' } |
    Format-Table -AutoSize @('Name' 'DisplayName' @{Expression='State';Width=9} @{Expression='StartMode';Width=9} 'StartName') |
    Out-File -Append $Output
  # Show how much room is left on the HDD
  Get-WmiObject Win32_LogicalDisk -ComputerName Localhost |
    Format-Table DeviceID, MediaType,
      @{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f ($_.size/1gb))}},
      @{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f ($_.freespace/1gb))}},
      @{Name="Free (%)";Expression={"{0,6:P0}" -f (($_.freespace/1gb) / ($_.size/1gb))}} -AutoSize |
    Out-File -Append $Output

我一直收到>>提示。我需要什么来完成脚本才能运行?

1 个答案:

答案 0 :(得分:0)

是的,正如David所说的那样,你错过了代码末尾的结束括号}。我试过调试它就得到了这个问题。

P.S。尝试使用以下命令添加Set-PSDebug模式。

Set-PSDebug -Trace 1

这有助于您更好地诊断脚本。 :)