14

The following script does not add a folder to my remote server. Instead it places the folder on My machine! Why does it do it? What is the proper syntax to make it add it?

$setupFolder = "c:\SetupSoftwareAndFiles"

$stageSrvrs | ForEach-Object {
  Write-Host "Opening Session on $_"
  Enter-PSSession $_

  Write-Host "Creating SetupSoftwareAndFiles Folder"

  New-Item -Path $setupFolder -type directory -Force 

  Write-Host "Exiting Session"

  Exit-PSSession

}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
ChiliYago
  • 11,341
  • 23
  • 79
  • 126

6 Answers6

17

Enter-PSSession can be used only in interactive remoting scenario. You cannot use it as a part of a script block. Instead, use Invoke-Command:

$stageSvrs | %{
         Invoke-Command -ComputerName $_ -ScriptBlock { 
             $setupFolder = "c:\SetupSoftwareAndFiles"
             Write-Host "Creating SetupSoftwareAndFiles Folder"
             New-Item -Path $setupFolder -type directory -Force 
             Write-Host "Folder creation complete"
         }
}
ravikanth
  • 24,922
  • 4
  • 60
  • 60
15

UNC path works as well with New-Item

$ComputerName = "fooComputer"
$DriveLetter = "D"
$Path = "fooPath"
New-Item -Path \\$ComputerName\$DriveLetter$\$Path -type directory -Force 
Barry MSIH
  • 3,525
  • 5
  • 32
  • 53
  • Doesn't work for me. I had to add `FileSystem::` before the UNC path. Ex: `FileSystem::\\servername\path\newfolder` – Alex Kwitny May 18 '18 at 15:50
2

For those who -ScriptBlock doesn't work, you can use this:

$c = Get-Credential -Credential 
$s = $ExecutionContext.InvokeCommand.NewScriptBlock("mkdir c:\NewDir")
Invoke-Command -ComputerName PC01 -ScriptBlock $s -Credential $c
Mark Varnas
  • 721
  • 9
  • 10
  • Old thread but for anyone else running this way: When you say "ScriptBlock doesn't work", I think you may be running into the issue where local variables aren't passed to the remote session. Take a look here: https://stackoverflow.com/questions/36328690/how-do-i-pass-variables-with-the-invoke-command-cmdlet – duct_tape_coder Apr 26 '19 at 19:30
0

The following code will create a new folder on a remote server using server name specified in $server. The below code assumes credentials are stored in MySecureCredentials and setup beforehand. Simply call createNewRemoteFolder "<Destination-Path>" to create the new folder.

function createNewRemoteFolder($newFolderPath) {

    $scriptStr = "New-Item -Path $newFolderPath -type directory -Force"
    $scriptBlock = [scriptblock]::Create($scriptStr)

    runScriptBlock $scriptBlock
}


function runScriptBlock($scriptBlock) {

    Invoke-Command -ComputerName $server -Credential $MySecureCreds -ScriptBlock $scriptBlock
}
Freddie
  • 908
  • 1
  • 12
  • 24
0
$Servers=Get-SPServer |?{ $_.Role -notlike "Invalid"}

foreach($Server in $Servers)
{
    $Machine=$Server.Address
    $Path= "SP Logs"
    $NewFolder="Trace2"
    $DL= "E"
    #Write-Host "Server name is = " $Server
    New-Item -Path \\$Machine\E$\$Path\$NewFolder -Force -ItemType Directory
}
Rose
  • 2,792
  • 4
  • 28
  • 42
Umr
  • 1
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Mickael B. May 01 '20 at 16:03
0

If you need to provide a folder name from the client, @Freddie has given clever answer. But I find more easy to do following:

$cred = Get-Credential
$session = New-PSSession -ComputerName $server -Credential $credInvoke- 
Command -Session $session -ScriptBlock { param ($folder) New-Item -Path $folder -ItemType Directory } -ArgumentList @("C:\temp\my_new_folder")
Roman Badiornyi
  • 1,509
  • 14
  • 28