【问题标题】:PowerShell Create Folder on Remote ServerPowerShell 在远程服务器上创建文件夹
【发布时间】:2022-05-18 18:40:48
【问题描述】:

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

$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

}

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    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"
             }
    }
    

    【讨论】:

      【解决方案2】:

      UNC 路径也适用于 New-Item

      $ComputerName = "fooComputer"
      $DriveLetter = "D"
      $Path = "fooPath"
      New-Item -Path \\$ComputerName\$DriveLetter$\$Path -type directory -Force 
      

      【讨论】:

      • 对我不起作用。我必须在 UNC 路径之前添加 FileSystem::。例如:FileSystem::\\servername\path\newfolder
      【解决方案3】:

      对于那些 -ScriptBlock 不起作用的,你可以使用这个:

      $c = Get-Credential -Credential 
      $s = $ExecutionContext.InvokeCommand.NewScriptBlock("mkdir c:\NewDir")
      Invoke-Command -ComputerName PC01 -ScriptBlock $s -Credential $c
      

      【讨论】:

      • 旧线程,但对于以这种方式运行的任何其他人:当您说“ScriptBlock 不起作用”时,我认为您可能遇到了局部变量未传递给远程会话的问题。看这里:stackoverflow.com/questions/36328690/…
      【解决方案4】:

      以下代码将使用$server 中指定的服务器名称在远程服务器上创建一个新文件夹。下面的代码假定凭据存储在MySecureCredentials 中并预先设置。只需拨打createNewRemoteFolder "<Destination-Path>" 即可创建新文件夹。

      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
      }
      

      【讨论】:

        【解决方案5】:
        $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
        }
        

        【讨论】:

        • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
        【解决方案6】:

        如果您需要从客户端提供文件夹名称,@Freddie 已经给出了巧妙的答案。但我发现更容易做到以下几点:

        $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")
        

        【讨论】:

          猜你喜欢
          • 2021-08-23
          • 2011-09-30
          • 2013-06-14
          • 1970-01-01
          • 1970-01-01
          • 2011-09-16
          • 2022-07-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多