PowerShell在远程服务器上创建文件夹

Chi*_*ago 11 powershell powershell-2.0

以下脚本不会将文件夹添加到我的远程服务器.相反,它将文件夹放在我的机器上!它为什么这样做?添加它的正确语法是什么?

$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

}
Run Code Online (Sandbox Code Playgroud)

rav*_*nth 16

Enter-PSSession只能在交互式远程处理场景中使用.您不能将它用作脚本块的一部分.相反,使用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"
         }
}
Run Code Online (Sandbox Code Playgroud)


Bar*_*SIH 13

UNC路径也适用于New-Item

$ComputerName = "fooComputer"
$DriveLetter = "D"
$Path = "fooPath"
New-Item -Path \\$ComputerName\$DriveLetter$\$Path -type directory -Force 
Run Code Online (Sandbox Code Playgroud)