I am working on a script for which I have written a GUI. The user fills out the fields and then the GUI passes those by way of parameters to the underlying script. Below is a reproducer of what I am trying:
Param ( [string]$sVM, [string]$iCPU, [string]$iRAM, [string]$sIP, [string]$sSubnet, [string]$sGateway, [string]$sDNS, [string]$sDescription, [string]$sSite, [string]$sHost, [string]$sDStore, [string]$sTemplate, [string]$sSpec, [string]$sFolder, [string]$sOU ) Write-Host $args.Count "args" ForEach ($sArg in $args) { Write-Host $sArg }
I am relatively new to PowerShell, having come from other languages, but was under the impression the parameters had to be at the top of the script, which is why I formatted this way. When I run the script with something like this (quotes around those fields with spaces):
Args.ps1 SPERA-SCCM01 4 16 10.10.120.115 255.255.255.0 10.10.115.254 10.10.1.120 "SCCM 2016 Server" ERA ER-vms33.mycompany.com Dev-ERA-App01 "ER 2016 Template 02" ScriptERADomain ScriptDeploy "OU=Patch Reboot,OU=ERA,OU=Servers,DC=mycompany,DC=com"
The issue I am having is, if I run the script just as written above, I get the following output which seems to indicate it found none of the parameters:
./Args.ps1 SPERA-SCCM01 4 16 10.10.120.115 255.255.255.0 10.10.115.254 10.10.1.120 "SCCM 2016 Server" ERA ER-vms33.mycompany.com Dev-ERA-App01 "ER 2016 Template 02" ScriptERADomain ScriptDeploy "OU=Patch Reboot,OU=ERA,OU=Servers,DC=mycompany,DC=com" 0 args
If I add something to the first line, even just a simple Write-Host “Beginning” or a cls, I get an error on Param (which I would expect, it not being the first line), but the output is what I would expect:
Param : The term 'Param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At desktop\Args.ps1:3 char:1 + Param ( + ~~~~~ + CategoryInfo : ObjectNotFound: (Param:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException15 args
SPERA-SCCM01
4
16
10.10.120.115
255.255.255.0
10.10.115.254
10.10.1.120
SCCM 2016 Server
ERA
ER-vms33.mycompany.com
Dev-ERA-App01
ER 2016 Template 02
ScriptERADomain
ScriptDeploy
OU=Patch Reboot,OU=ERA,OU=Servers,DC=mycompany,DC=com
I have to believe whatever I am doing wrong is simple stupid, but if someone could clue me in that would be much appreciated.