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
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.
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
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