Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

powershell question about argument

Hello,
I a script I check a condition on argument which contains comma but I does not work :

IF ($arg1 -eq "TEST,DB")

How can I modify it?

Thanks
0
bibi92
Asked:
bibi92
  • 12
  • 7
  • 3
  • +1
1 Solution
 
SubsunCommented:
Did you try  with single quotes..
IF ($arg1 -eq 'TEST,DB') 

Open in new window

Also if you can post the complete script then it will be more convenient for us to give you suggestions..
0
 
Haresh NikumbhSr. Tech leadCommented:
0
 
QlemoC++ DeveloperCommented:
What the above link tries to tell is that if you provide the argument as an unquoted string containing commas, an array is built. Don't call your script like this:
   .\script.ps1 -arg1 1,2
because that will make $arg1 to consist of two string or numeric elements. Instead:
   .\script.ps1 -arg1 '1,2'
BTW, using single versus double quotes is not related to this issue.
0
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

 
Haresh NikumbhSr. Tech leadCommented:
Thanks I  will keep in mind next time.
0
 
bibi92Author Commented:

Open in new window

[string]$HOSTNAME = (Get-WmiObject Win32_Computersystem).Name
$COLSC=get-wmiobject -class win32_service -computername $hostname| where {$_.name -like '*SQL*'}| select-object Name,state,status,Started,Startname,displayname



if ($arg1 -match 'TEST') {

      
      foreach ($arg in $arg1) {
      $ofs =","
          $param = ([string] $arg).split("=")


      if ($arg1 -match 'TEST,DB') {

      foreach ($service in $COLSC) {

      $line_sc =  " { `"{#DBH}`":`"" + $HOSTNAME + "`",`"{#SC}`":`"" + $service.Name + "`"},"

      write-host $line_sc

      }
      }
      }
      }
0
 
SubsunCommented:
If you want to pass arguments to the script then you need to have parameters declared at the top of your script, in a param block:

param(
  [string]$Arg1
)

Open in new window

Is that what you looking for? if not please explain what you tying to achieve..
0
 
bibi92Author Commented:
Yes, it's already done. I have forgotten to copy this line on the post. Thanks
0
 
SubsunCommented:
if not please explain what you tying to achieve..
My question still stands.. Few lines in the script like $ofs =","$param = ([string] $arg).split("=") doesn't make any sense to me. So I think a bit of explanation on what you trying to achieve, will help us..

BTB: Hope you are running .\script.ps1 -arg1 'TEST,DB'
0
 
bibi92Author Commented:
$ofs ="," & $param = ([string] $arg).split("=") will be for testing http://stackoverflow.com/questions/7775487/why-does-powershell-replace-commas-with-whitespaces 

I can't execute .\script.ps1 -arg1 'TEST,DB' because the command is executed on zabbix userparameter
0
 
bibi92Author Commented:
I have to execute .\script.ps1 TEST,DB
0
 
QlemoC++ DeveloperCommented:
bibi,

Then you will have to "rejoin" the array to a single parameter, if you can't even use
  .\script.ps1 'TEST,DB'
That can be done like this:
param ([String []] $arg1)
$arg1 = $arg1 -join ','

Open in new window

Declaring $arg1 as an array of strings is important.
0
 
bibi92Author Commented:
Thanks but there is an error :
Unable to find type [String []]: make sure that the assembly containing this type is loaded.

+ param ([String []] <<<<  $arg1)
    + CategoryInfo          : InvalidOperation: (String []:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TypeNotFound
0
 
bibi92Author Commented:
$args = $arg1 -join ','

If I add echo $args, the result is TEST
0
 
QlemoC++ DeveloperCommented:
Sorry, there is a space where it isn't allowed. Use   [String[]]   instead of   [String []]  .
0
 
bibi92Author Commented:
Ok thanks.
With zabbix, it isn't OK, the script is executed like
UserParameter=sql_svc_control[TEST,DB],powershell -nologo -command "& d:\scripts\script.ps1 $1"

If I execute the script on powershell, it's Ok
powershell -nologo -command "& d:\scripts\script.ps1 TEST,DB"

Why?
0
 
QlemoC++ DeveloperCommented:
Put a
 write-host $arg1
 read-host

Open in new window

at the very beginning of the script to see what gets inserted for $1.
0
 
bibi92Author Commented:
The result is
TEST DB
So I have modified the script like
if ($arg1 -match 'DB') {

      foreach ($service in $COLSC) {

      $line_sc =  " { `"{#DBH}`":`"" + $HOSTNAME + "`",`"{#SC}`":`"" + $service.Name + "`"},"

      write-host $line_sc

      }
      }
But it does not work
0
 
QlemoC++ DeveloperCommented:
That should work, if $COLSC contains values.
0
 
bibi92Author Commented:
The $COLSC contains values but it does not work
0
 
QlemoC++ DeveloperCommented:
You might want to also echo the result of ($arg1 -match 'DB') with write-host, to make sure the match works. Depending on how values are provided by zabbix, the match might require a wildcard:
   if ($arg1 -match "*DB")
or
   if ($arg1 -match "* DB")
0
 
bibi92Author Commented:
if ($arg -match <<<<  "*DB") {
 + CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
 + FullyQualifiedErrorId : BadOperatorArgument
0
 
bibi92Author Commented:
if ($arg1 -match <<<<  "*DB") {
 + CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
 + FullyQualifiedErrorId : BadOperatorArgument
0
 
QlemoC++ DeveloperCommented:
Doh! I mixed up -like and -match - either use
   if ($arg1 -like "*DB")
or
  if ($arg1 -match ".*DB")
0
 
bibi92Author Commented:
thanks
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 12
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now