Solved

powershell question about argument

Posted on 2013-05-15
25
340 Views
Last Modified: 2013-05-16
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
Comment
Question by:bibi92
  • 12
  • 7
  • 3
  • +1
25 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39167558
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
 
LVL 22

Expert Comment

by:Haresh Nikumbh
ID: 39167564
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39167606
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 22

Expert Comment

by:Haresh Nikumbh
ID: 39167631
Thanks I  will keep in mind next time.
0
 

Author Comment

by:bibi92
ID: 39167760

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
 
LVL 40

Expert Comment

by:Subsun
ID: 39167862
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
 

Author Comment

by:bibi92
ID: 39167927
Yes, it's already done. I have forgotten to copy this line on the post. Thanks
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39167953
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
 

Author Comment

by:bibi92
ID: 39169839
$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
 

Author Comment

by:bibi92
ID: 39169844
I have to execute .\script.ps1 TEST,DB
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39170527
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
 

Author Comment

by:bibi92
ID: 39170546
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
 

Author Comment

by:bibi92
ID: 39170587
$args = $arg1 -join ','

If I add echo $args, the result is TEST
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39170600
Sorry, there is a space where it isn't allowed. Use   [String[]]   instead of   [String []]  .
0
 

Author Comment

by:bibi92
ID: 39171018
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 39171118
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
 

Author Comment

by:bibi92
ID: 39171179
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 39171263
That should work, if $COLSC contains values.
0
 

Author Comment

by:bibi92
ID: 39171408
The $COLSC contains values but it does not work
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39171469
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
 

Author Comment

by:bibi92
ID: 39171526
if ($arg -match <<<<  "*DB") {
 + CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
 + FullyQualifiedErrorId : BadOperatorArgument
0
 

Author Comment

by:bibi92
ID: 39171528
if ($arg1 -match <<<<  "*DB") {
 + CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
 + FullyQualifiedErrorId : BadOperatorArgument
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39171802
Doh! I mixed up -like and -match - either use
   if ($arg1 -like "*DB")
or
  if ($arg1 -match ".*DB")
0
 

Author Closing Comment

by:bibi92
ID: 39173232
thanks
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question