Author Posts

July 10, 2014 at 1:29 pm

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

July 10, 2014 at 1:37 pm

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.

July 10, 2014 at 1:47 pm

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

July 10, 2014 at 1:53 pm

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

July 10, 2014 at 1:59 pm

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 }

July 10, 2014 at 2:09 pm

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!

July 11, 2014 at 5:06 am

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