Powershell中的哈希表和RunSpaces

时间:2019-03-05 15:13:53

标签: powershell hashtable

从哈希表的结果更新列表中的数据时遇到一些问题。我99%的确定这是由于对我正在做的事情缺乏了解。

我正在从CSV生成服务器的$ list。 CSV包含服务器名称,域,描述以及一些其他空白列,以供以后使用。

简而言之,我想做什么:我需要从远程服务器列表中拉下进程。为此,我将列表和功能中的每个服务器都放入其自己的运行空间中,哈希表正在按预期进行更新。但是我无法更新我拥有的原始$ list。

这是我的代码:

Function OpenFile ($FilePath) {

$OFDiag = new-object system.windows.forms.openfiledialog
$OFDiag.filter = 'CSV (*.csv) | *.csv'
$OFDiag.ShowDialog() | out-null
$OFDiag.filename

} 

# Create Primary Variables

$FilePath = OpenFile 
$list = (get-content $FilePath) -replace '\(',' -' #ALL Servers and Groups need to remove parenthesis
$list = $list -replace '\)' #finish up removing the parenthesis
$list = $list -replace ' Or WorkGroup'
$list = convertFrom-CSV $list | select 'Name', 'Computer_Description', 'Domain' #Need to convert the list into a CSV formatted table.
$list = $list | sort Name 
$list | Add-Member -NotePropertyName 'LastReboot' -NotePropertyValue $null
$list | Add-Member -NotePropertyName 'LastDeployment' -NotePropertyValue $null 
$list | Add-Member -NotePropertyName 'RebootStatus' -NotePropertyValue $null
$list | Add-Member -NotePropertyName 'DownProcess' -NotePropertyValue $null
$list | Add-Member -NotePropertyName 'EnabledStatus' -NotePropertyValue $null
$list | Add-Member -NotePropertyName 'RDP' -NotePropertyValue $null
$list | Add-Member -NotePropertyName 'SchedTask' -NotePropertyValue $null
$servers = $list | %{$_.Name} | sort #ALL SERVERS - ONLY Servernames



$ServProSel = {
    #
    #  Checks for Running Services and Processes.
    # This Makes a determination as to what service/process groups should be checked. 
    # The Information as to what processes to look for are sent to the ProSer_Check function
    # information from there is sent to the ServerStatus Tab
    #
    #Write-Host 'starting ServerProSel'


   Param ($computer,$cred,$grpName,$hash)

    #$cred = $(get-Variable "$Domain" -valueOnly)
    $ck =@{} #$(Get-Variable -name "SCP_$serName" -ValueOnly)


    Function ProSer_Check {

            # This is the actual function that is run on the remote system to check
            # for processes and services.

            param ( [array] $Prcs,
                    [string] $Computer )
            $script:chkres =@()
            foreach ($p in $Prcs){
                $script:res = Get-Process -name $p -ErrorAction SilentlyContinue
                if (!$res) {
                    $chk = "$p -DOWN`r`n"
                    $chkres += $chk
                        }
                }
            if ($chkres.count -eq 0){
                $chkres = "All Processes Up"}

            Return $chkres

            }


    switch -Regex ($grpName){

        'Demonstration' {
            $Prcs = @('Process.Service'); break}
        'Historian' {
            $Prcs =@('Process.Service'); break}
        'Models' {
            $Prcs =@('UpdaterServer'); break}
        'Inflictor' {
            $Prcs =@('Automation.EngineService','Automation.Manager.Service','Automation.SmfLauncher','postgres','Redundancy.Server','WatchDog.Service'); break}
        'Simulator' {
            $Prcs =@('proc','moni','server','serve','clerk','web'); break}
        'WebServer' {
            $Prcs =@('w3wp','VShell'); break}
        default {
            $Prcs =@('svchost'); break}
        }

    $R = invoke-command -credential $cred -computername $Computer -scriptblock ${function:ProSer_Check} -ArgumentList  $Prcs,$Computer



    $hash[$Computer]=([string]$R)

}



$Script:runspaces = New-Object System.Collections.ArrayList   
$Global:hash = [hashtable]::Synchronized(@{}) 

$global:sessionstate = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$global:runspacepool = [runspacefactory]::CreateRunspacePool(1, 10, $sessionstate, $Host)
$global:runspacepool.Open() 

Function SendToRunSpace {

    $function = $args[0]
    #$function
    $powershell = [powershell]::Create().AddScript($function).AddArgument($computer.name).AddArgument($cred).AddArgument($grpName).AddArgument($hash)

    $powershell.RunspacePool = $global:runspacepool
    #$hash = @{Name=$computer.name;DownProcess = "Waiting.."}

    $temp = "" | Select-Object PowerShell,Runspace,Computer
    $Temp.Computer = $Computer
    $temp.PowerShell = $powershell

    $temp.Runspace = $powershell.BeginInvoke()
    Write-Verbose ("Adding {0} collection" -f $temp.Computer)
    $runspaces.Add($temp) | Out-Null   

}


ForEach ($Computer in $list) {
    $domain = $computer.Domain
    $grpName = $computer.'Computer_Description'
    $cred = $(get-Variable "$Domain" -valueOnly)
    #Create the powershell instance and supply the scriptblock with the other parameters 
    if(!$(Get-Variable "TEST_$domain" -ValueOnly)){
                CredCheck $computer.name $cred
                    }

    #SendToRunSpace $scriptBlock $computer $domain $global:hash
    SendToRunSpace $ServProSel $computer $cred $grpName $global:hash

}

我正在PowerShell ISE中运行此程序,因此我可以即时进行编辑和测试。当我运行此代码时,我会生成$list$hash项目。最终,我想从$hash中获取服务器的值,并更新$list对象中的相应服务器信息。

还是有更好的方法来做到这一点?哈希表是将数据从运行空间同步到当前进程的唯一方法吗?

0 个答案:

没有答案