我有一个有效的 powershell 脚本,它调用 psftp.exe 并让我将文件从本地计算机成功传输到云中的服务器。一切正常,除了一个错误(据我所知,没有抢占任何东西)正在我的 powershell ISE 屏幕上闪烁。
PS D:\PROJECTS\Timekeeping\build_src> D:\PROJECTS\Timekeeping\build_src\mypowershell.ps1
.\psftp.exe : Looking up host "remoteserver"
At D:\PROJECTS\Timekeeping\build_src\mypowershell.ps1:30 char:20
+ ... expression (.\psftp.exe 'remoteserver' -l 'ubuntu' -i key.ppk ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Looking up host "remoteserver":String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Connecting to remoteserver port 22
Server version: SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2
Using SSH protocol version 2
We claim version: SSH-2.0-PuTTY_Release_0.62
Doing Diffie-Hellman group exchange
Doing Diffie-Hellman key exchange with hash SHA-256
Host key fingerprint is:
ssh-rsa 2048 87:4d:50:8d:09:25:40:78:c2:7b:5c:ef:12:8e:57:7c
Initialised AES-256 SDCTR client->server encryption
Initialised HMAC-SHA1 client->server MAC algorithm
Initialised AES-256 SDCTR server->client encryption
Initialised HMAC-SHA1 server->client MAC algorithm
Reading private key file "key.ppk"
Using username "ubuntu".
Offered public key
Offer of public key accepted
Authenticating with public key "imported-openssh-key"
Sent public key signature
Access granted
Opened channel for session
Started a shell/command
Sent EOF message
Server sent command exit status 0
Disconnected: All channels closed
Invoke-Expression : Cannot bind argument to parameter 'Command' because it is null.
At D:\PROJECTS\Timekeeping\build_src\mypowershell.ps1:30 char:19
+ ... -expression (.\psftp.exe 'remoteserver' -l 'ubuntu' -i key.ppk ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand
请注意,出于安全目的,我将服务器 IP 替换为 remoteserver,并将远程 PC 的 IP 替换为 remotePC。我正在使用的 powershell 包含以下代码:
#Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
$props = Get-Content D:\PROJECTS\Timekeeping\build_src\branch.txt
$BranchName = $props | select-string 'BranchName' | %{$_.line.split('=')}
$Branch = $BranchName[1]
$datetime2 = get-date -format yyyyMMdd
$datetime = get-date
$Logfile = "D:\PROJECTS\Timekeeping\build_src\logs\$Branch-$datetime2-chronos.log"
Function LogWrite
{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
}
if(!(Test-Connection -ComputerName remotePC -BufferSize 16 -Count 1 -ea 0 -quiet)){ LogWrite "$datetime Biometric Device is turned off!" }else{ LogWrite "$datetime Biometric Device is turned on!"}
$startdate = get-date -format yyyy-MM-dd
$enddate = get-date -format yyyy-MM-dd
for($i=1;$i -le 1000;$i++){
$number +="$i,"
}
$numlength = $number.Length-1
$x = invoke-webrequest -Uri 'http://remotePC/form/Download?uid=$number"&"sdate=$startdate"&"edate=$enddate' -OutFile D:\PROJECTS\Timekeeping\build_src\logs\$Branch-$datetime2.dat -PassThru
if($x.StatusCode -eq 200){
LogWrite "$datetime Timesheet successfully retrieved. Response code: $($x.StatusCode)"
}else{
LogWrite "$datetime Error retrieving timesheet. Response code: $($x.StatusCode)"
}
#invoke-expression .\psftp.exe -i key.ppk ubuntu@remoteserver -be b sftpcommands.txt > D:\PROJECTS\Timekeeping\build_src\logs\sftp-log.log 2>$null
#Write-Host "done"
invoke-expression (.\psftp.exe 'remoteserver' -l 'ubuntu' -i key.ppk -v -b 'sftpcommands.txt' > 'D:\PROJECTS\Timekeeping\build_src\logs\sftp-log.log') 2> $null