Powershell工作流程序列

时间:2014-08-03 03:46:35

标签: powershell workflow

我编写了以下脚本,该脚本从文本文件中选择服务器列表,并执行Ping,RDP,远程注册表和WMI检查。我使用哈希表来获得结果。我正在将结果输出到控制台和文本文件。我观察到的是,对于~20台服务器执行4次检查,脚本的执行需要超过15分钟。我阅读了Powershell Workflow概念并与多台服务器并行执行检查。我需要一些关于我的脚本的帮助,请问我如何使用工作流程和foreach -parallel

workflow ftccheck
{
param($servers)

#Text file to pick the server names
$servers = Get-Content "C:\temp\serverlist.txt"

#Format DateTime to String
$datetime = Get-Date
$datestring = (Get-Date).ToString("dd-MM-yyyy-hh-mm-ss")

#Create the output files
$offlinefile = "offline_$datestring.txt"
New-Item -ItemType File -Path C:\temp -Name $offlinefile

$onlinefile = "online_$datestring.txt"
New-Item -ItemType File -Path C:\temp -Name $onlinefile

#Write Date and Time to the output files at each iteration
Write-Output $datetime | Out-File C:\temp\$offlinefile -Append
Write-Output $datetime | Out-File C:\temp\$onlinefile -Append

$offcount = 0
$oncount = 0

foreach -parallel ($server in $servers)
{
InlineScript
{
    Write-Host `r
    Write-Host $server
    Write-Host `r

    #PING
    $ping = Test-Connection -ComputerName $server -Quiet -Count 1

    #RDP
    $rdp  = Test-NetConnection -ComputerName $server -CommonTCPPort RDP -InformationLevel Quiet

    #REMOTE REGISTRY
    $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$server)
    $ref = $regkey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")

    #WMI
    $wmi  = GWMI -Query "Select * from Win32_PingStatus where Address = '$server'"

#Create hashtables
$state_noun = @{
   $true  = 'SUCCESS'
   $false = 'FAIL'
 }

$state_verb = @{
   $true  = 'SUCCEEDED'
   $false = 'FAILED'
 }

#Pass results to the hashtable array
$result = @"
PING             :    $($state_noun[$ping])
RDP              :    $($state_noun[$rdp])
Remote Registry  :    $($state_noun[[bool]$ref])
WMI              :    $($state_noun[[bool]$wmi])
"@

If($result.Contains('FAIL')) { 

  Write-Output $server | Out-File C:\temp\$offlinefile -Append
  Write-Output $result | Out-File C:\temp\$offlinefile -Append
  $offcount++          
} 

elseif($result.Contains('SUCCESS')) { 

  Write-Output $server | Out-File C:\temp\$onlinefile -Append
  Write-Output $result | Out-File C:\temp\$onlinefile -Append
  $oncount++  
}

#Write the output on console
Write-Host $result

Write-Host `r
Write-Host $offcount " servers are DOWN"
Write-Host $oncount " servers are UP"

Write-Output $offcount "servers are DOWN" | Out-File C:\temp\$offlinefile -Append
#Write-Output " servers are DOWN" | Out-File C:\temp\$offlinefile -Append

Write-Output $oncount "servers are UP" | Out-File C:\temp\$onlinefile -Append   
#Write-Output " servers are UP" | Out-File C:\temp\$onlinefile -Append   

  }
 }
}

0 个答案:

没有答案