我有一个脚本,下面,我有一个脚本文件夹。它引用了一组命令提示符应用程序$cmd1.exe, etc.
。在 VS Code 中使用 Powershell 或 Integrated Powershell 终端,我按照以下步骤使用它:
- 从 Scripts 目录点源脚本
> .\new-customconfig.ps1
, - 切换到工作目录
> cd [New Directory]
, - 使用脚本中的函数并从工作目录传递一个文件名
> New-CustomConfig .\FileName
。
它在我第一次运行脚本时从工作目录传递文件,但从任何后续运行中,它都会在 Scripts 目录中查找文件。
有什么我做错了吗?
Function New-CustomConfig {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]
$FileName
)
Begin {
#Set tools directory
$ToolD = '[Directory]\Tools'
#Clean up filename
$FileName = $FileName -replace '^[\W]*', ''
#Setup Executables
$cmdtools = "\cmd1.exe", "\cmd2.exe", "\cmd3.exe"
#Setup Arguments
$cmdargs = "cmd1args", "cmd2args", "cmd3args"
#Setup Write-Host Comments
$cmdecho = "echo1", "echo2", "echo3"
#Setup command object info
$cmdinfo = New-Object System.Diagnostics.ProcessStartInfo
$cmdinfo.RedirectStandardError = $true
$cmdinfo.RedirectStandardOutput = $true
$cmdinfo.UseShellExecute = $false
#Create command object
$cmd = New-Object System.Diagnostics.Process
"Generating Config for $FileName"
}
Process {
for ($i = 0; $i -le $cmdtools.Count; $i++) {
$cmdinfo.FileName = $ToolD + $cmdtools[$i]
$cmdinfo.Arguments = '' + $cmdargs[$i] + ''
Write-Host $i
Write-Host $cmdinfo.FileName
Write-Host $cmdinfo.Arguments
Write-Host $(Get-Location)
$cmdecho[$i]
$cmd.StartInfo = $cmdinfo
$cmd.Start() | Out-Null
$cmd.WaitForExit()
$stdout = $cmd.StandardOutput.ReadToEnd()
$stderr = $cmd.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
Write-Host "exit code: " + $p.ExitCode
}
}
End {
"Press any key to continue..."
$null = $host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
}
}