Variable with space

Welcome Forums General PowerShell Q&A Variable with space

Viewing 6 reply threads
  • Author
    Posts
    • #214461
      Participant
      Topics: 6
      Replies: 15
      Points: 116
      Rank: Participant

      If I have a variable input is passed with space it causes the follow on commandlet to fail.

       

      i.e

      get-vbrjob from veeam if the jobs have spaces when I pass it to vbrjobobject it fails because the jobs from the passed variable has spaces.

      i.e

      Test job 1
      Test backup job 2
      Test backup monthly

      when the $var is passed it fails because of the space.
       

      • This topic was modified 9 months, 4 weeks ago by sreed7743.
    • #214470
      Participant
      Topics: 12
      Replies: 547
      Points: 1,345
      Helping Hand
      Rank: Community Hero

      enclose the var in quotes

    • #214473
      Participant
      Topics: 6
      Replies: 15
      Points: 116
      Rank: Participant

      would it be

      “$var”

    • #214491
      Participant
      Topics: 17
      Replies: 1956
      Points: 4,010
      Helping Hand
      Rank: Community Hero

      Either single or double should work. The main difference is double qoutes will attempt to expand variables and single qoutes are literal:

    • #214497
      Participant
      Topics: 5
      Replies: 260
      Points: 1,039
      Helping Hand
      Rank: Community Hero

      I’m not sure I follow your question.  Are you saying the variable name has spaces or your variable has a value <string> with spaces in it?   For information on naming variables look here.  Bottom line to have spaces in a variable name you would place it in curly braces like this.

      String literals with spaces should be in quotes.  Single quotes are literal and double-quotes can interpolate.  You can read more about that here.

      If you post your actual code I might be able to help you further.

    • #214662
      Participant
      Topics: 6
      Replies: 15
      Points: 116
      Rank: Participant

      Add-PSSnapin VeeamPSSnapin
      “$jobs” = (Get-VBRJob).name

      ForEach-Object ($job in “‘$jobs'”)
      {
      Get-VBRJobObject -Name $job
      }

      here is the list of items that I’m passing from a cmdlet as you can see most of the list has spaces. What I’m really trying to accomplish is to pass this data to another cmdlet however it fails. I wonder if I have to split each item in the list into a separate item (not sure how to do that) because it still fails when I put the vatiable in quotes i.e “$jobs”.

      VMware Daily-Weekly-Monthly GFS-1 8D-RP_52W_RP-12M-RP
      Weekly Saturday Backup Group-1 Saturday GFS1 0D-RP_4W-RP_12M-RP
      Twice Week Days VMware Backup Group-1
      VMware Daily-Weekly-Monthly GFS-2 (4 D-RP 52 W-RP 12 M-RP)
      Weekly VMware Backup Group-1 No-GFS
      Weekly Sunday VMware Group-1 No-GFS
      VMware Twice Daily GFS-1 8D-RP_52W_RP-12M-RP
      VMware No Daily Weekly Monthly GFS-2 0D-RP_26W_RP-12M-RP
      Weekly Saturday VMware Group-1 No-GFS
      Weekly Saturday
      EOL Server Backup
      Week Days VMware Backup Group-4
      Week Day VMware Backup Group-6-NWLK
      Week Days VMware Backup Group-6
      Weekly VMware Saturday-Group-1
      Physical Twice Daily GFS-1 8D-RP_26W_RP-12M-RP
      VMware No Daily Weekly Monthly GFS-1 0D-RP_13W_RP-0M-RP
      Week Days VMware Backup Group-2
      Week Days VMware Backup Group-3
      Week Days VMware Backup Group-1
      Weekly Folder GFS-1 0D-RP_26-RP_0M-RP
      Weekly Sunday VMware Group-1-NWLK

    • #214665
      Participant
      Topics: 10
      Replies: 2480
      Points: 6,537
      Helping Hand
      Rank: Community MVP

      This should be all you need:

Viewing 6 reply threads
  • The topic ‘Variable with space’ is closed to new replies.