使用变量作为路径时如何将参数传递给程序
How to pass arguments to program when using variable as path
我正在尝试使用路径变量在开始作业脚本块中启动程序。这是行:
$using:plinkdir\plink.exe -telnet $using:ip -P $using:port | TimeStamp >> "$using:LogDir\WeightLog_$(get-date -f MM-dd-yyyy).txt"
所有变量都有效,当我使用 c:\plink 代替 $plink 变量时整行有效。它在 -telnet 上出错,因此没有获取 plink 的参数。
这是 $var 的工作:
$LogDir = "c:\users\user" # Log file output directory
$PlinkDir = "C:" # plink.exe directory
$SerialIP = "1.1.1.1" # serial device IP address
$SerialPort = 10000 # port to log
function CaptureWeight {
Start-Job -Name WeightLog -ScriptBlock {
# Bring Variables into job from callers scope
#$LogDir = $using:LogDir
#$PlinkDir = $using:PlinkDir
#$SerialIP = $using:SerialIP
#$SerialPort = $using:SerialPort
# Set TimeStamp format
filter timestamp {"$(Get-Date -Format MM/dd/yyyy_HH:mm:ss) $_"}
# Start plink, pipe output to TimeStamp, redirect to log file
$using:PlinkDir\plink.exe -telnet $using:SerialIP -P $using:SerialPort | TimeStamp >> "$using:LogDir\WeightLog_$(get-date -f MM-dd-yyyy).txt"
}
}
谢谢!
这个答案是基于一些假设和对你的问题和评论中解释的内容可能有用的预感。
首先解释一下“PowerShell被意外杀死时没有丢失任何数据。”,这是因为>>
(别名Out-File -Append
) 是:
- 打开文件流
- 将输出附加到文件
- 关闭流
因此,当您终止工作时,基本上仍然存在。我确实建议您使用 Set-Content
但这是在我理解您在做什么之前,在这种情况下,这不是一个选择。
此处建议的替代方案是使用 StreamWriter
, which is nice because we can keep the file stream open and append to the file as needed without the need to close the stream each time (this will also take care of the "blank line between every line in the output to the log file"). To get around the killing the Job but still saving the results to the file we can use a try / finally
statement。
$LogDir = "c:\users\user" # Log file output directory
$PlinkDir = "C:" # plink.exe directory
$SerialIP = "1.1.1.1" # serial device IP address
$SerialPort = 10000 # port to log
function CaptureWeight {
Start-Job -Name WeightLog -ScriptBlock {
filter timestamp {
$sw.WriteLine("$(Get-Date -Format MM/dd/yyyy_HH:mm:ss) $_")
}
try {
$sw = [System.IO.StreamWriter]::new("$using:LogDir\WeightLog_$(Get-Date -f MM-dd-yyyy).txt")
& "$using:PlinkDir\plink.exe" -telnet $using:SerialIP -P $using:SerialPort | TimeStamp
}
finally {
$sw.ForEach('Flush')
$sw.ForEach('Dispose')
}
}
}
$job = CaptureWeight # For testing, save the job
Start-Sleep -Seconds 60 # wait 1 minute
$job | Stop-Job # kill the job
Get-Content "$LogDir\WeightLog_$(Get-Date -f MM-dd-yyyy).txt" # Did it work?
我正在尝试使用路径变量在开始作业脚本块中启动程序。这是行:
$using:plinkdir\plink.exe -telnet $using:ip -P $using:port | TimeStamp >> "$using:LogDir\WeightLog_$(get-date -f MM-dd-yyyy).txt"
所有变量都有效,当我使用 c:\plink 代替 $plink 变量时整行有效。它在 -telnet 上出错,因此没有获取 plink 的参数。
这是 $var 的工作:
$LogDir = "c:\users\user" # Log file output directory
$PlinkDir = "C:" # plink.exe directory
$SerialIP = "1.1.1.1" # serial device IP address
$SerialPort = 10000 # port to log
function CaptureWeight {
Start-Job -Name WeightLog -ScriptBlock {
# Bring Variables into job from callers scope
#$LogDir = $using:LogDir
#$PlinkDir = $using:PlinkDir
#$SerialIP = $using:SerialIP
#$SerialPort = $using:SerialPort
# Set TimeStamp format
filter timestamp {"$(Get-Date -Format MM/dd/yyyy_HH:mm:ss) $_"}
# Start plink, pipe output to TimeStamp, redirect to log file
$using:PlinkDir\plink.exe -telnet $using:SerialIP -P $using:SerialPort | TimeStamp >> "$using:LogDir\WeightLog_$(get-date -f MM-dd-yyyy).txt"
}
}
谢谢!
这个答案是基于一些假设和对你的问题和评论中解释的内容可能有用的预感。
首先解释一下“PowerShell被意外杀死时没有丢失任何数据。”,这是因为>>
(别名Out-File -Append
) 是:
- 打开文件流
- 将输出附加到文件
- 关闭流
因此,当您终止工作时,基本上仍然存在。我确实建议您使用 Set-Content
但这是在我理解您在做什么之前,在这种情况下,这不是一个选择。
此处建议的替代方案是使用 StreamWriter
, which is nice because we can keep the file stream open and append to the file as needed without the need to close the stream each time (this will also take care of the "blank line between every line in the output to the log file"). To get around the killing the Job but still saving the results to the file we can use a try / finally
statement。
$LogDir = "c:\users\user" # Log file output directory
$PlinkDir = "C:" # plink.exe directory
$SerialIP = "1.1.1.1" # serial device IP address
$SerialPort = 10000 # port to log
function CaptureWeight {
Start-Job -Name WeightLog -ScriptBlock {
filter timestamp {
$sw.WriteLine("$(Get-Date -Format MM/dd/yyyy_HH:mm:ss) $_")
}
try {
$sw = [System.IO.StreamWriter]::new("$using:LogDir\WeightLog_$(Get-Date -f MM-dd-yyyy).txt")
& "$using:PlinkDir\plink.exe" -telnet $using:SerialIP -P $using:SerialPort | TimeStamp
}
finally {
$sw.ForEach('Flush')
$sw.ForEach('Dispose')
}
}
}
$job = CaptureWeight # For testing, save the job
Start-Sleep -Seconds 60 # wait 1 minute
$job | Stop-Job # kill the job
Get-Content "$LogDir\WeightLog_$(Get-Date -f MM-dd-yyyy).txt" # Did it work?