调用带有覆盖从文本文件读取的参数的函数

时间:2018-10-23 17:27:23

标签: function

我当前的脚本从一个文本文件(服务器名称)读取并从该列表中获取服务。但是,我还想添加一个带有参数的函数,以便它首先使用该参数,如果未指定任何参数,则它将从文本文件中读取。

我当前的代码

[string] $ComputerPath = "E:\Scripts-PowerShell_Command_Others\test 
csirt\servernames.otcs.txt"
$ComputerNames = Get-Content -Path $ComputerPath 

try {

if ($ComputerNames -ne $null) {

foreach ($c in $ComputerNames) {
    $process = Get-Service -ComputerName $c -Name OTCS, otcsadmin, otcsclusteragent, spawner, tomcat7, tomcat8, tomcat8-appworks -ErrorAction SilentlyContinue

    Write-Host "Server:" $c -ForegroundColor Yellow

    foreach ($p in $process) {

        $pd = $p.Name
        Write-Output "$($pd)"
        }
        Write-Host "Processes Running: $($process.Count)" -ForegroundColor Yellow
        Write-Output ""
    }
} 
else {
    Write-Output "No computers found in file."
    }
    } catch {
        Write-Host $_ -ForegroundColor Red
        }

0 个答案:

没有答案