【问题标题】:How can i get a prompt to ask for the path, when using Get-ChildItem in PowerShell?在 PowerShell 中使用 Get-ChildItem 时,如何获得询问路径的提示?
【发布时间】:2011-06-23 21:16:08
【问题描述】:

我对 PowerShell 比较陌生。 我找到了一些指南并将一个小脚本混合在一起,但我似乎无法设置提示来询问源/目标。

脚本如下:

gci -path   | Get-Random -Count 4 | mi -Destination C:\Temp
while(1) { sleep -sec 20; .\Power.ps1 }

对于任何回应, 提前致谢!

【问题讨论】:

    标签: powershell path set prompt


    【解决方案1】:

    使用Read-Host:

    Get-ChildItem -Path (Read-Host -Prompt 'Get path')
    

    【讨论】:

      【解决方案2】:

      这是一个例子,FWIW:

       $source,$target = $null,$null
      
      while ($source -eq $null){
      $source = read-host "Enter source file name"
      if (-not(test-path $source)){
          Write-host "Invalid file path, re-enter."
          $source = $null
          }
      elseif ((get-item $source).psiscontainer){
          Write-host "Source must be a file, re-enter."
          $source = $null
          }
      }
      
      while ($target -eq $null){
      $target = read-host "Enter source directory name"
      if (-not(test-path $target)){
          Write-host "Invalid directory path, re-enter."
          $target = $null
          }
      elseif (-not (get-item $target).psiscontainer){
          Write-host "Target must be a directory, re-enter."
          $target = $null
          }
      }
      

      【讨论】:

      • 无论如何提供tab补全?
      猜你喜欢
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多