gpt4 book ai didi

function - 不能在函数中调用管道属性。电源外壳

转载 作者:行者123 更新时间:2023-12-01 23:42:27 25 4
gpt4 key购买 nike

所以我正在尝试创建一个“下载”函数,该函数使用管道对象属性来确定下载方法(sftp 或 http)。然后为 putty/winscp 创建一个 sftp 脚本或 curl http url。我定义对象如下:

#WinSCP
$winscp = new-object psobject
$winscp | add-member noteproperty name "WinSCP"
$winscp | add-member noteproperty dltype "http"
$winscp | add-member noteproperty file "winscp.exe"
$winscp | add-member noteproperty url "https://cdn.winscp.net/files/WinSCP-5.17.8-Setup.exe"
$winscp | add-member noteproperty path "$env:ProgramFiles(x86)\WinSCP"
$winscp | add-member noteproperty install 'msiexec /i "$DataPath\$winscp.file" /quiet /norestart'

#Database
$db = new-object psobject
$db | add-member noteproperty name "Client Database"
$db | add-member noteproperty dltype "sftp"
$db | add-member noteproperty file "database_"
$db | add-member noteproperty ver "check"
$db | add-member noteproperty ext ".csv"
$db | add-member noteproperty dir "db"

#DatabaseVersion
$db_ver = new-object psobject
$db_ver | add-member noteproperty name "Database Version File"
$db_ver | add-member noteproperty dltype "sftp"
$db_ver | add-member noteproperty file "current_version.txt"
$db_ver | add-member noteproperty dir "db"

目前,函数中的 $Input 变量有问题。它只能使用一次并且不会转换为 if 语句。由于它包含一个具有多个属性的对象,因此我认为首先需要在函数内将其转换为一个新对象。我是 powershell 的新手,还没有找到这样做的方法。这是我制作并尝试使用的功能:

function Download () {

#HTTP Download Method
if ($input.dltype -eq "http") {
curl $input.url -O $DataPath\$input.file
#HTTP Success or Error
$curlResult = $LastExitCode
if ($curlResult -eq 0)
{
Write-Host "Successfully downloaded $input.name"
}
else
{
Write-Host "Error downloading $input.name"
}
pause
}

#SFTP Download Method
if ($input.dltype -eq "sftp") {
sftpPassCheck
#Detect if version required
if ($input.ver = "check") {
#Download the objects version file
"$+$Input+_ver" | Download
#Update the object's ver property
$input.ver = [IO.File]::ReadAllText("$DataPath\current_version.txt")
#Build the new filename
$input.file = "$input.file"+"$input.ver"+"$input.ext"
#Delete the version file
Remove-Item "$DataPath\current_version.txt"
}
& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
/log="$DataPath\SFTP.log" /ini=nul `
/command `
"open sftp://ftpconnector:$script:sftp_pass@$input.ip/ -hostkey=`"`"ssh-ed25519 255 SETvoRlAT0/eJJpRhRRpBO5vLfrhm5L1mRrMkOiPS70=`"`" -rawsettings ProxyPort=0" `
"cd /$input.dir" `
"lcd $DataPath" `
"get $input.file" `
"exit"
#SFTP Success or Error
$winscpResult = $LastExitCode
if ($winscpResult -eq 0)
{
Write-Host "Successfully downloaded $input.name"
}
else
{
Write-Host "Error downloading $input.name"
}
}
}

我可能遗漏了一些简单的东西,但我现在一无所知。哦用法应该是:

WinSCP | download

最佳答案

将来自管道的输入绑定(bind)到函数的参数的正确方法是声明一个高级函数 - 参见about_Functions_Advanced_Parameters以及此答案底部的实现。

但是,在简单的情况下,一个过滤器就可以了,它是一种函数的简化形式,它将管道输入隐式绑定(bind)到 automatic $_ variable并为每个输入对象调用:

filter Download {
if ($_.dltype -eq "http") {
# ...
}
}

$input is another automatic variable ,在简单(非高级)函数中,它是所有管道输入的枚举器 被接收,因此必须循环

也就是说,下面的simple function 等价于上面的filter:

function Download {
# Explicit looping over $input is required.
foreach ($obj in $input) {
if ($obj.dltype -eq "http") {
# ...
}
}
}

如果您确实想将它变成一个高级函数(请注意,我已经更改了名称以符合 PowerShell 的动词-名词命名约定):

function Invoke-Download {

param(
# Declare a parameter explicitly and mark it as
# as pipeline-binding.
[Parameter(ValueFromPipeline, Mandatory)]
$InputObject # Not type-constraining the parameter implies [object]
)

# The `process` block is called for each pipeline input object
# with $InputObject referencing the object at hand.
process {

if ($InputObject.dltype -eq "http") {
# ...
}

}

}

关于function - 不能在函数中调用管道属性。电源外壳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64826657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com