PowerShell 在远程服务器上创建文件夹 [英] PowerShell Create Folder on Remote Server
本文介绍了PowerShell 在远程服务器上创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下脚本不会向我的远程服务器添加文件夹.相反,它将文件夹放在我的机器上!它为什么这样做?让它添加它的正确语法是什么?
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
}
推荐答案
Enter-PSSession 只能在交互式远程处理方案中使用.您不能将其用作脚本块的一部分.相反,使用调用命令:
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"
}
}
这篇关于PowerShell 在远程服务器上创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文