问题描述
我正在PowerShell脚本中执行ssh-keyscan命令以更新Windows系统上的SSH已知主机文件。
环境详细信息:
def fitness(self,vectors,X):
df_vectors = spark.createDataFrame(
pd.DataFrame(
data=vectors,columns=[f"var_{x}" for x in range(vectors.shape[1])]
)
)
df = VectorAssembler(
inputCols=[x for x in df_vectors.columns if "var_" in x],outputCol="vars_sel",).transform(df_vectors)
@udf(returnType=ArrayType(FloatType()))
def vector_to_array(v):
# convert column of vectors into column of arrays
a = v.values.tolist()
return a
df = (
df.withColumn("vars_array",vector_to_array("vars_sel"))
.drop("vars_sel")
.withColumn("featuresCol",lit(self.featuresCol))
.withColumn("labelCol",lit(self.labelCol))
.withColumn("metric",lit(self.metric))
.withColumn("fitness",lit(0.0))
)
@pandas_udf(df.schema,functionType=PandasUDFType.GROUPED_MAP)
def test_data(pdf):
df_to_return = pdf.copy()
df_to_return["fitness"] = 1.0
return df_to_return
returns = df.groupBy("vars_array").apply(test_data)
return
我的代码如下:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
我遇到的问题是,无论我尝试什么,ssh-keyscan的控制台输出都不会捕获到脚本文件中。成绩单文件中捕获的所有内容如下:
PowerShell:5.1.18362.752
Operating system: Windows 10 x64 1903(OSBuild 18362.1082)
Language: English
我已尝试以下方法解决此问题:
1)Start-Transcript -Path C:\Windows\temp\abc.log
$sshKnownHostFile = "$Env:USERPROFILE\.ssh\known_hosts"
$cmd = "ssh-keyscan -p 7000 blah.net | Out-File -Encoding ASCII $sshKnownHostFile -Append"
Invoke-Expression $cmd -Verbose
Stop-Transcript
根据{{3}}
2)**********************
Windows PowerShell transcript start
Start time: 20201008201659
Username: DESKTOP-NIFKPBT\kiranh
RunAs User: DESKTOP-NIFKPBT\kiranh
Configuration Name:
Machine: DESKTOP-NIFKPBT (Microsoft Windows NT 10.0.18362.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 1280
PSVersion: 5.1.18362.752
PSEdition: Desktop
PSCompatibleVersions: 1.0,2.0,3.0,4.0,5.0,5.1.18362.752
BuildVersion: 10.0.18362.752
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started,output file is C:\Windows\temp\abc.log
**********************
Windows PowerShell transcript end
End time: 20201008201707
**********************
这是在https://github.com/PowerShell/PowerShell/issues/10994#issuecomment-550528523
3)Invoke-Expression $cmd -Verbose | Out-Default
根据{{3}}上的回复
上面的答案是针对Git的。由于ssh-keyscan.exe来自Git实用程序家族,因此我尝试使用该工具查看我的问题是否可以解决。
4)使用Invoke-Expression $cmd -Verbose | Out-Host
cmdlet调用ssh-keyscan.exe
5)直接在PowerShell脚本中调用ssh-keyscan.exe,就像在命令提示符下执行外部程序一样。
以上所有方法均无济于事。在上述所有情况下,记录日志中捕获的输出都是相同的。
我该如何工作?
解决方法
正如我评论的,ssh-keyscan
的输出显示在日志中就很好。这是我运行的命令和清理后的输出。
Start-Transcript .\transcript.txt
ssh-keyscan linuxhost
$PSVersionTable
如下所示,该脚本包含ssh-keyscan
和$PSVersionTable
的输出
Machine: DESKTOP-NEKK74J (Microsoft Windows NT 10.0.19041.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 8692
PSVersion: 5.1.19041.546
PSEdition: Desktop
PSCompatibleVersions: 1.0,2.0,3.0,4.0,5.0,5.1.19041.546
BuildVersion: 10.0.19041.546
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started,output file is .\transcript.txt
PS C:\TEMP> ssh-keyscan.exe linuxhost
# linuxhost:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
linuxhost ssh-rsa AAAAB3NzaC1yc2EAAAADAQ
# linuxhost:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
linuxhost ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItb
# linuxhost:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
linuxhost ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOGy3M1
PS C:\TEMP> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.19041.546
PSEdition Desktop
PSCompatibleVersions {1.0,4.0...}
BuildVersion 10.0.19041.546
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
在运行您的命令时,我也看到了抄本中键入的所有命令。
Transcript started,output file is .\transcript.txt
PS C:\TEMP> $sshhostfile = "$env:USERPROFILE\.ssh\known_hosts"
PS C:\TEMP> $cmd = "ssh-keyscan linuxhost | Out-File -Encoding ASCII $sshhostfile -Append"
PS C:\TEMP> Invoke-Expression $cmd -Verbose
PS C:\TEMP> Stop-Transcript
我不知道为什么不会列出您的命令。为了确保我会再次运行它们。如果希望看到要重定向到文件的输出,则仅可以捕获控制台中显示的内容。如果您希望输出到日志文件和笔录中,那么对命令的这些更改可能会有所帮助
$cmd = {ssh-keyscan -p 7000 blah.net}
& $cmd | Tee-Object -FilePath $sshhostfile -Append
现在成绩单和日志具有此输出。
Transcript started,output file is .\transcript.txt
PS C:\TEMP> $cmd = {ssh-keyscan linuxhost}
PS C:\TEMP> & $cmd | Tee-Object -FilePath $sshhostfile
linuxhost ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCgoqTpOufcs
linuxhost ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyN
linuxhost ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOGy3M1StlLMI
PS C:\TEMP> Stop-Transcript