Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function Powershell使用;“运行时”;根据调用的参数调用函数的参数_Function_Powershell_Jenkins - Fatal编程技术网

Function Powershell使用;“运行时”;根据调用的参数调用函数的参数

Function Powershell使用;“运行时”;根据调用的参数调用函数的参数,function,powershell,jenkins,Function,Powershell,Jenkins,如何将“运行时”参数用作函数中的参数。 现在,我的代码将返回所有数据/值,而不管为“at run”参数输入什么 例如,我希望将“at run”参数中的$Server\u名称与foreach循环匹配,以便只返回具有匹配$Server\u名称的数据 function StartServer{ Param ( [Parameter (Mandatory=$true)] [STRING] $Region, [Parameter (Mandatory=$true)

如何将“运行时”参数用作函数中的参数。 现在,我的代码将返回所有数据/值,而不管为“at run”参数输入什么

例如,我希望将“at run”参数中的$Server\u名称与foreach循环匹配,以便只返回具有匹配$Server\u名称的数据

function StartServer{

    Param (
        [Parameter (Mandatory=$true)] [STRING] $Region,
        [Parameter (Mandatory=$true)] [STRING] $Env,
        #[STRING] $Env,
        [Parameter (Mandatory=$true)] [STRING] $ScriptName, #$ScriptName 
        [Parameter (Mandatory=$true)] [STRING] $Server_Name
        #[STRING] $Server,
        #[STRING] $Services
    )
    #Write-Output ("Region: "+ $Region + " Env: " + $Env  + " ScriptName: " + $ScriptName + " Server_Name: " + $Server)

    #$startDate = Get-Date;

    $Sourcefile = "C:\Users\ Scripts\CLEAN10182019.csv"
    $StartProperties  = (Import-Csv $Sourcefile) 

    $Sorted = $StartProperties |select Server_Name,Service_Name, Start_Order, Start_Flag, Start_Wait  | Sort-Object -Property Server_Name, {[int]$_.Start_Order}|Format-Table
    #$Sorted

    foreach($line in $StartProperties)
        { 
        $Server_Name = [string]$line.'Server_Name'
        $Service_Name = [string]$line.'Service_Name'
        $Start_Order = [string]$line.'Start_Order'
        $Start_Wait= [string]$line.'Start_Wait' 

        $file = #"Matches? " + $Server_Name
                "Start " + $Service_Name + "  " + " Timeout " + $Start_Wait

        Write-Output $file 

        }


}
StartServer

我能想到的最快、最简单的方法可能是仅在传入服务名称时执行代码-

foreach($line in $StartProperties) { 
    if ([string]$line.'Server_Name' -eq $Server_Name) {
        $Server_Name = [string]$line.'Server_Name'
        $Service_Name = [string]$line.'Service_Name'
        $Start_Order = [string]$line.'Start_Order'
        $Start_Wait= [string]$line.'Start_Wait' 

        $file = #"Matches? " + $Server_Name
                "Start " + $Service_Name + "  " + " Timeout " + $Start_Wait

        Write-Output $file 
    }

}

这看起来像您正在寻找的吗?

如果我理解正确,您希望只匹配那些
Server\u Name
列与
$Server\u Name
参数值匹配的CSV行:

Import-Csv $Sourcefile | Where-Object Server_Name -eq $Server_Name | ForEach-Object {
  # Synthesize and output a filename.
  "Start " + $_.Service_Name + "  " + " Timeout " + $_.Start_Wait
}
如果要将合成字符串作为对象的一部分输出,并在输出时以表格形式显示,请执行以下操作:

Import-Csv $Sourcefile | Where-Object Server_Name -eq $Server_Name | ForEach-Object {
   # Create a custom object with the properties of interest:
  [pscustomobject] @{
    # Other properties
    Server_Name = $_.Server_Name # ...
    FileName = "Start " + $_.Service_Name + "  " + " Timeout " + $_.Start_Wait
  }
}
如果您的输出对象具有4个或更少的属性,则它们会自动打印,就像它们已通过管道传输到
格式表
,因此您将得到如下结果:

Import-Csv $Sourcefile | Where-Object Server_Name -eq $Server_Name | ForEach-Object {
   # Create a custom object with the properties of interest:
  [pscustomobject] @{
    # Other properties
    Server_Name = $_.Server_Name # ...
    FileName = "Start " + $_.Service_Name + "  " + " Timeout " + $_.Start_Wait
  }
}
Server\u Name文件名
----------- --------
服务器1启动服务1超时20
服务器2启动服务2超时10
...

作为旁白:仅使用
Format-*
cmdlet设置显示格式;如果必须以编程方式处理数据,请不要使用它们<代码>格式-*cmdlet输出格式说明,而不是数据-请参阅。因此,从
$Sorted=…
行末尾删除
| Format Table
!输出仅返回列出的服务器名称。如果有办法在$server\u Name变量中包含多个服务器名称,请将参数更改为字符串数组-[String[]]而不是[String]。然后,“if([string]$line.'Server_Name'-eq$Server_Name){”变成了“if([string]$line.'Server_Name'-in$Server_Name){”。然后使用-Server_Name Server1、Server2、,Server3@JessTorres:
“开始”+$\服务\u名称+++“超时”+$\uu.Start\u Wait
是一个字符串串联表达式,它构建一个隐式输出的输出字符串,就像
“a”+“b”
将返回
ab
。它以什么方式不连接?本质上,这个表达式结合了问题代码中的两个语句:
$file=…
写入输出$file
@JessTorres:如果您想要类似于表的输出格式,那么您需要输出具有属性的对象,而不仅仅是单个字符串可以显式地通过管道传输到
格式表
(尽管使用4个或更少的属性,您可以隐式地获得表输出)。