Variable with space

Welcome Forums General PowerShell Q&A Variable with space

  • This topic has 6 replies, 5 voices, and was last updated 2 months ago by
    Participant
    .
Viewing 6 reply threads
  • Author
    Posts
    • #214461
      Participant
      Topics: 4
      Replies: 14
      Points: 85
      Rank: Member

      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 2 months ago by sreed7743.
    • #214470
      Participant
      Topics: 12
      Replies: 524
      Points: 1,219
      Helping Hand
      Rank: Community Hero

      enclose the var in quotes

    • #214473
      Participant
      Topics: 4
      Replies: 14
      Points: 85
      Rank: Member

      would it be

      “$var”

    • #214491
      Participant
      Topics: 12
      Replies: 1623
      Points: 2,565
      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:

      PS C:\WINDOWS\system32> $test = "a zombie"
      
      PS C:\WINDOWS\system32> "Rob was chased by $test"
      Rob was chased by a zombie
      
      PS C:\WINDOWS\system32> 'Rob was chased by $test'
      Rob was chased by $test
      
    • #214497
      Participant
      Topics: 4
      Replies: 82
      Points: 251
      Helping Hand
      Rank: Contributor

      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.

      ${hello world} = "Some text"

      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: 4
      Replies: 14
      Points: 85
      Rank: Member

      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: 4
      Replies: 2248
      Points: 5,490
      Helping Hand
      Rank: Community MVP

      This should be all you need:

      Add-PSSnapin VeeamPSSnapin
      $jobs = Get-VBRJob
      
      forEach ( $job in $jobs) {
          Get-VBRJobObject -Name $job.Name
      }
Viewing 6 reply threads
  • You must be logged in to reply to this topic.