-1

How to use the "at-run" parameters as parameters within the function. Right now my code is returning all data/value regardless of what is enter for the "at-run" parameters.

For example, I will like to match the $Server_Name from the "at-run" parameters with the foreach loop, so that only data with the matching $Server_Name is return.

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
jgt05
  • 27
  • 6
  • As an aside: Only ever use `Format-*` cmdlets for _display formatting_; never use them if data must be _programmatically processed_. `Format-*` cmdlets output _formatting instructions_, not _data_ - see [this answer](https://stackoverflow.com/a/55174715/45375). Therefore, remove the `|Format-Table` from the end of the `$Sorted = ...` line. – mklement0 Oct 26 '19 at 04:04

2 Answers2

0

The quickest and easiest way I can think of might be to only execute the code if the service name is passed in-

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 
    }

}

Does that look like what you're looking for?

Mark
  • 414
  • 3
  • 7
  • this works! The output returns only the server name listed. If there a way to include multiple server names in the $Server_Name variable? – jgt05 Oct 28 '19 at 14:26
  • Change your parameter to be an array of strings - [String[]] instead of [String]. Then "if ([string]$line.'Server_Name' -eq $Server_Name) {" becomes "if ([string]$line.'Server_Name' -in $Server_Name) {". Then you run it with -Server_Name Server1,Server2,Server3 – Mark Oct 28 '19 at 14:51
0

If I understand you correctly, you want match only those CSV rows whose Server_Name column matches the $Server_Name parameter value:

Import-Csv $Sourcefile | Where-Object Server_Name -eq $Server_Name | ForEach-Object {
  # Synthesize and output a filename.
  "Start " + $_.Service_Name + "  " + " Timeout " + $_.Start_Wait
}

If you want to output the synthesized string as part of an object, to be presented in tabular form on output, do something like the following:

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
  }
}

If your output objects have 4 or fewer properties, they automatically print as if they had been piped to Format-Table, so you'll get something like the following:

Server_Name FileName
----------- --------
server1     Start service1   Timeout 20
server2     Start service2   Timeout 10
...
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • @JessTorres: `"Start " + $_.Service_Name + " " + " Timeout " + $_.Start_Wait` is a string-concatenation expression that builds a single output string that is implicitly output, just like `"a" + "b"` would return `ab`. In what way does it not concatenate? In essence, this single expression combines two statements from your code in the question: `$file = ...` and `Write-Output $file`. – mklement0 Oct 28 '19 at 14:33
  • @JessTorres: If you want _table_-like output format, you need to output _objects with properties_, not just single strings, possibly explicitly piped to `Format-Table` (though with 4 or fewer properties you _implicitly_ get table output). – mklement0 Oct 28 '19 at 14:35