Creating a folder remotely using variables

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Michael Cummings Michael Cummings 2 years, 4 months ago.

  • Author
    Posts
  • #16997
    Profile photo of Michael Cummings
    Michael Cummings
    Participant

    First off I'm a real noobe to PS less than 2 weeks of playing with it and I'm having some success but I'm stuck on the following .. trying to figure out why it not working

    #Imports Web Administration Module
    Import-Module WebAdministration

    #Setup server sessions
    $s = New-PSSession -ComputerName Server1

    #Variables
    $group = "GCE"
    $NewFolder = "WebAdmin"

    #Command to create folder using variables
    $command = {New-Item -Path c:\web\www\$group\$NewFolder -ItemType DIR}

    #Create folder on the remote server
    invoke-command -session $s -ScriptBlock $command

    Any Help would be greatly appreciated

    Thanks
    Mike

  • #16998
    Profile photo of Adam Bertram
    Adam Bertram
    Participant

    From the looks of it I assume you're just wanting to create a single folder on a remote server. At first glance, you're using remoting when you don't have to. I'd eliminate some complexity from this and just use the built in c$ admin share to do it. You also don't need the WebAdministration module to do this.

    $Group = 'GCE'
    $NewFolder = 'WebAdmin'
    New-Item "\\SERVER1\c$\web\www\$Group\$NewFolder" -ItemType Dir

    Your original issue was that you weren't putting double quotes around your folder.

  • #16999
    Profile photo of Michael Cummings
    Michael Cummings
    Participant

    Great... Thanks for the help It worked great and less complicated.

    How would I write it if I wanted to put the same folder on several servers? Can I use sessions in place of the server name?

    Thanks
    Mike

    • #17001
      Profile photo of Adam Bertram
      Adam Bertram
      Participant

      To put the same folder on several servers try this:

      $Servers='SERVER1','SERVER2','SERVER3'
      $Group = 'GCE'
      $NewFolder = 'WebAdmin'
      
      $Servers | foreach { New-Item "\\$($_)\c$\web\www\$Group\$NewFolder" -ItemType Dir }
      
  • #17000
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Adam is right you can just use the C$ admin share to create a folder on a remote server. An alternative approach would be to tell PowerShell that you want the variables in your controller script to be available in the remote session with the Using: keyword.

    $Using:Group instead of $Group
    $Using:NewFolder instead of $NewFolder

    Modified example:

    #Setup server sessions
    $s = New-PSSession -ComputerName Server1 
    
    #Variables
    $group = "GCE"
    $NewFolder = "WebAdmin"
    
    #Command to create folder using variables. 
    $command = { New-Item -Path c:\web\www\$Using:Group\$Using:NewFolder -ItemType Directory }
    
    #Create folder on the remote server
    invoke-command -session $s -ScriptBlock $command
    
  • #17003
    Profile photo of Don Jones
    Don Jones
    Keymaster

    LOL... as a fun side note...

    #Imports Web Administration Module
    Import-Module WebAdministration
    

    One nice thing about PowerShell is that command names can be pretty obvious. You don't necessarily need that comment :). Save ya some typing!

  • #17010
    Profile photo of Michael Cummings
    Michael Cummings
    Participant

    Thanks every one for all the great help. As for as typing goes and the "Documentation" where I'm at likes to see lines like #Imports Web Administration Module. Don't ask me I just work here LOL

    Thanks again
    Mike

You must be logged in to reply to this topic.