Home > Enterprise >  Powershell combine parameters inside a script with parameters entered on the command line?
Powershell combine parameters inside a script with parameters entered on the command line?

Time:11-04

Quick one? - As I'm still learning Powershell. I was wondering if it was possible to combine parameters inside a script with parameters entered on the command line? i.e. I have a function like this as an example...

function GetInfo {
    param ($SiteName, $Subnet, $Cred, $ComputerName)
    Write-Host "Checking $Site and $ComputerName"
    <# ... Additional logic to check computername prefix & subnet etc. #>
}

$SiteName = "London1"
$Subnet= "192.168.10.1/24"
$Cred = <supplied>
#$ComputerName = "blah1"
GetInfo $SiteName $Subnet $Cred $ComputerName

$SiteName = "London2"
$Subnet= "192.168.11.1/24"
$Cred = <supplied>
#$ComputerName = "blah2"
GetInfo $SiteName $Subnet $Cred $ComputerName

Now say inside the script I would specify the SiteName, Subnet, Cred.... but on the command line I would like to specify -ComputerName But as I'm using the script & let's say I know that Lon1-PC1 is in "London1" I would like to do this on the calling command:

.\GetPCInf.ps1 -ComputerName "Lon1-PC1"

or

.\GetPCInf.ps1 -ComputerName "Lon2-PC1"

Obviously there will be additional logic inside the script to say that if the -ComputerName prefix is "Lon1" then do X or if -ComputerName prefix is "Lon2" then do Y..

Obviously I know I can just put the computername in the script, save it & run it.

So far when I try, nothing happens in relation to the -Computername return..

I haven't yet tried combining a parameter & Args - but I've read Args is not the best to use so I'm trying to avoid it.

If this can't be done then fair enough, just wondered if someone might know if this can be done, as it saves me typing in:

.\GetPCInf.ps1 -SiteName London1 -Subnet "192.168.10.1/24" -Cred mycreds -ComputerName "Lon1-PC1"

each time I want to run it....

I suppose I could create batch files to call the script & put %1 for computername in the batch file & call it that way, but just curious really..

Many thanks.

CodePudding user response:

So far when I try, nothing happens in relation to the -Computername return..

Scripts are just functions stored in files - and they support param blocks and parameter declarations just like functions - so declare a $ComputerName parameter:

# GetPCInf.ps1
param(
  [Parameter(Mandatory = $true)]
  [string]$ComputerName
)

# define GetInfo function
function GetInfo {
    param ($SiteName, $Subnet, $Cred, $ComputerName)
    Write-Host "Checking $Site and $ComputerName"
    <# ... Additional logic to check computername prefix & subnet etc. #>
}

# define table of arguments to pass to GetInfo
$GetInfoArgs = @{
  ComputerName = $ComputerName
}

# add additional arguments based on computer name value
if($ComputerName -like 'Lon1-*'){
  $GetInfoArgs  = @{
    SiteName = "London1"
    Subnet= "192.168.10.1/24"
    Cred = $(<# credential goes here #>)
  }
} elseif($ComputerName -like 'Lon2-*') {
  $GetInfoArgs  = @{
    SiteName = "London2"
    Subnet= "192.168.11.1/24"
    Cred = $(<# credential goes here #>)
  }
} else {
  $GetInfoArgs  = @{
    SiteName = "DefaultSite"
    Subnet= "192.168.12.1/24"
    Cred = $(<# credential goes here #>)
  }
}

# invoke GetInfo function
GetInfo @GetInfoArgs
  • Related