Store a string array in a variable and pass it to a command

Welcome Forums General PowerShell Q&A Store a string array in a variable and pass it to a command

Viewing 5 reply threads
  • Author
    Posts
    • #184784
      Participant
      Topics: 4
      Replies: 8
      Points: 60
      Rank: Member

      The following code works very well

       $Include = "*.exe, *.rpt, *.chm"
      
      Copy-Item -Include $Include -Path $NetPath -Recurse -Force -Destination $TmpPath 

      Now I would like to pass the entire copy command to another command. I do this as follows:

       $Include = "*.exe, *.rpt, *.chm"
      
      $Command = "Copy-Item -Include $Include -Path $NetPath -Recurse -Force -Destination $TmpPath”
      
      Start-Process powershell.exe -Verb runas -ArgumentList "$Command" 

      Apparently, the string array in variable $Include is no longer recognized. What do I have to change for the code to work?

    • #184802
      Participant
      Topics: 9
      Replies: 1277
      Points: 1,051
      Helping Hand
      Rank: Community Hero

      Two things:

      1. The $Include variable is not a string array, it's a just a string. See below:
        PS C:\Users\rasim\Desktop> $Include = "*.exe, *.rpt, *.chm"
        
        $Include.GetType()
        
        IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                              
        -------- -------- ----                                     --------                                                                                                                                                                                              
        True     True     String                                   System.Object                                                                                                                                                                                         
        
        
        
        PS C:\Users\rasim\Desktop> $Include = "*.exe,", "*.rpt", "*.chm"
        
        $Include.GetType()
        
        IsPublic IsSerial Name                                     BaseType                                                                                                                                                                                              
        -------- -------- ----                                     --------                                                                                                                                                                                              
        True     True     Object[]                                 System.Array                                                                                                                                                                                          
        
      2. Not sure why you are starting a Powershell in Powershell, but to answer your question you are not passing the variable to the process, so it doesn't know anything about the $Include variable or even $NetPath or $TmpPath. If you are trying to execute the command on another system, you should be using Invoke-Command and then you can use the $using. See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-6
    • #184805
      Participant
      Topics: 4
      Replies: 8
      Points: 60
      Rank: Member

      Not sure why you are starting a Powershell in Powershell

      I want to execute the copy command with elevated right. If that goes the other way, I would be grateful for any help.

      If you are trying to execute the command on another system

      The command should not be executed on another system, but with elevated privileges.

    • #184865
      Participant
      Topics: 4
      Replies: 8
      Points: 60
      Rank: Member

      I found the following:

      If I give $Include direct in the copy command, $Include must be an System.Array

      $Include = "*.exe", "*.rpt", "*.chm"
      Copy-Item -Include $Include -Path $Source -Recurse -Force -Destination $Target

      When I pass the copy command in another command, $Include must be an system.object

      $Include = "*.exe, *.rpt, *.chm"
      $Command = "Copy-Item -Include $Include -Path $Source -Recurse -Force -Destination '$Target'"
      Start-Process powershell.exe -Verb runas -ArgumentList "$Command"

      Both variants work now

    • #184967
      Participant
      Topics: 2
      Replies: 510
      Points: 1,301
      Helping Hand
      Rank: Community Hero

      You can use -Credential to execute the copy program with a specific credential, which generally suffices for most use cases. 🙂

       

    • #185003
      Participant
      Topics: 4
      Replies: 8
      Points: 60
      Rank: Member

      I think Iwould have to know the credentials of the user. It's not like this. I think I can't get on with -Credential.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.