输入读取主机的详细信息后保存powershell脚本

输入读取主机的详细信息后保存powershell脚本,powershell,save,office365,Powershell,Save,Office365,示例: $nameofpath = read-host "enter path" get-services | export-csv "$nameofpath" 我需要一个脚本,因此在上面的示例中输入路径(如c:\files\test.txt)后,它将保存一个脚本,其中包含: get-services | export-csv "c:\files\test.txt" 。。。所以我可以去那个文件,点击它,它就会运行 目前,我有一个这样的脚本草稿,但如果我知道如何做第一个示例,我希望能够对该脚本

示例

$nameofpath = read-host "enter path"
get-services | export-csv "$nameofpath"
我需要一个脚本,因此在上面的示例中输入路径(如
c:\files\test.txt
)后,它将保存一个脚本,其中包含:

get-services | export-csv "c:\files\test.txt"
。。。所以我可以去那个文件,点击它,它就会运行


目前,我有一个这样的脚本草稿,但如果我知道如何做第一个示例,我希望能够对该脚本执行相同的操作,您可能需要更改正在运行的脚本,或者查询其他文本文件。如果文本文件中有任何内容,请使用该文件;否则,提示输入值。以下示例说明如何使用
$PSCommandPath
(它包含正在运行的脚本的完整路径和文件名)变量更改正在运行的脚本:

$Foo = $null
#If $Foo is $null prompt for value then write that value to the script.
if($Foo -eq $null){
    $Foo = Read-Host "Foo"
    #Need to becarful not to match this line
    $NewScript = Get-Content $PSCommandPath | ForEach-Object {$_ -replace '^\$Foo = \$null$',"`$Foo = '$Foo'"}
    $NewScript | Out-File $PSCommandPath -Force
}
Write-Host "Foo $Foo"

希望这能有所帮助。

这看起来很业余,但效果与预期一样

$path = [Microsoft.VisualBasic.Interaction]::InputBox("Enter path") 
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null  
 get-service | export-csv -path $path\services.csv # original command 
$Command="get-service | export-csv -path $path\services.csv" # same copy here to save 
  $command |Out-File "D:\exportservice.ps1" # where you want the file to be saved to run later