简体   繁体   English

功能内的功能-Powershell

[英]Function within a Function - Powershell

OK I am going to try to explain this as best as I can. 好的,我将尽我所能尽力解释这一点。 What started out as a simple script has turned into a huge mess and now I cannot figure out how to get it working. 最初从一个简单的脚本变成了一个巨大的混乱,现在我不知道如何使它工作。 I have been coming here for answers for some time so maybe you guys can help. 我来这里已有一段时间了,也许你们可以帮忙。

What I am trying to do is a import a list of systems and check to see if they are online. 我想做的是导入系统列表,并检查它们是否在线。 If they are online they go in one list and if not they go in another. 如果他们在线,则进入一个列表,如果不是,则进入另一个列表。

foreach ($server in $servers) {
    if (Test-Connection $server -Count 1 -ea 0 -Quiet) { 
        Write-Host "$server Is Up" -ForegroundColor Green
        $server | out-file -Append $liveSystems -ErrorAction SilentlyContinue 
    } else { 
        Write-Host "$server Is Down" -ForegroundColor Red
        $server | out-file -Append $inactive -ErrorAction SilentlyContinue  
    }
}

From there I check to see if the application I need installed is on the systems. 从那里,我检查我需要安装的应用程序是否在系统上。 That is where things start to go off-track. 那就是事情开始偏离轨道的地方。 When I run the function to process the $liveSystems file all I get is the last line of the file (or the same system over and over) and not each system as it should be. 当我运行该函数来处理$ liveSystems文件时,我得到的只是文件的最后一行(或一遍又一遍的同一系统),而不是每个系统都应有的样子。

function Is-Installed( $program ) {

    $x86 = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
            Where-Object { $_.GetValue( "DisplayName" ) -like "*$program*" } ).Length -gt 0;

    $x64 = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") |
        Where-Object { $_.GetValue( "DisplayName" ) -like "*$program*" } ).Length -gt 0;
}

$program

function process-file1 {

    param($filename)

    Get-Content $filename -PipelineVariable line | ForEach-Object {
      Is-Installed -program "My_Service"
        if (Is-Installed -eq "True") {
            Write-Host "$server has agent installed" -ForegroundColor Green
            $server | Out-File $installed -ErrorAction SilentlyContinue
        }
        else
        {
            Write-Host "$server does not have agent installed" -ForegroundColor Red
            $server | Out-File -Append $notInstalled -ErrorAction SilentlyContinue
        }
    }
}

process-file1 -filename $liveSystems

Once I can get the systems to process through the list of installed and not installed I am trying to take the list of installed systems and check which ones have the service running and which ones do not. 一旦我可以通过已安装和未安装的列表来处理系统,我将尝试获取已安装系统的列表,并检查哪些系统在运行服务,哪些系统未在运行服务。

$array = @()            
foreach($i in (gc $installed)) {            
    $svc = Get-Service my_service -ComputerName $i -ea "0"            
    $obj = New-Object psobject -Property @{            
        Name = $svc.name            
        Status = $svc.status            
        Computer = $i            
    }            
    $array += $obj            
}                      
$array | Select Computer,Name,Status | Export-Csv -Path $resultsFile - 
NoTypeInformation

Last but not least I run through that list of running and not running and attempt to start the service on systems that are not running. 最后但并非最不重要的一点是,我遍历了正在运行和未运行的列表,并尝试在未运行的系统上启动服务。

function process-CSVfile2 {
    param($filename)
    Import-Csv $filename |
    ForEach-Object -PipelineVariable object {
        if($_.Status -eq "Running") {
            Write-Host "Your Service is currently Running on" $_.Computer
        }
        if($_.Status -eq "Stopped") {
            $serviceName = 'my_service'
            $service = Get-CimInstance Win32_Service -ComputerName $_.Computer -Filter "Name=$serviceName"
            $service.Start()
            $service.WaitForStatus("Started",'00:00:30')
            Start-Sleep 10
        }
    }
}

Several of these blocks run separately but when put together they will not run. 这些块中的几个单独运行,但是放在一起时它们将不运行。 I can't seem to get past the second block where it just looks at the same line over and over. 我似乎无法越过第二个方块,它只会一遍又一遍地看同一行。

In addition there is a piece I have been trying to get working that would install the application on systems that do not have the service installed but that is not working either but I will save that for a different time. 另外,我一直在尝试一件可以将应用程序安装在未安装服务但也无法正常工作的系统上的方法,但是我将其保存了不同的时间。

If anyone can help me with this I would really appreciate it. 如果有人可以帮助我,我将非常感激。 After 3 days of trying to get it running I am at my wits end. 经过3天的尝试使其运行起来,我精疲力尽。

I'd create objects and properties instead of files with computers online etc... Something like: 我会用在线计算机等创建对象和属性,而不是文件。

$Computers=New-Object -TypeName System.Collections.ArrayList
$Servers = @(Get-Content -path c:\servers.txt)
$Servers = $Servers | ? {$_} | select-object -uniqe |ForEach-Object {$_.TrimEnd()}
$Servers|ForEach-Object {
$tempobj=New-Object -TypeName PSObject
$tempobj | Add-Member -type NoteProperty -name Name -value $_
$tempobj | Add-Member -type NoteProperty -name isOnline -value $FALSE
$tempobj | Add-Member -type NoteProperty -name Installed -value $FALSE
$tempobj | Add-Member -type NoteProperty -name serviceRunning -value $FALSE
            [void]$Computers.Add($tempobj)

then You could work on array (no need for additional files) 那么您就可以处理数组(不需要其他文件)

$Computers|Where-Object {$_.isOnline -eq $TRUE}

etc 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM