Powershell:为什么这个功能不起作用?
Powershell: Why doesn't this function work?
我正在尝试制作一个脚本,其中包含比较两个文件夹项目的功能。节目:
- 提示用户输入文件路径
- 检查文件名是否不同
- 检查文件大小是否不同
作为测试,我一直在将同一个文件夹与其自身进行比较(输出应该是错误的,错误的)。在将第 1 步 ($referencepath
) 设为函数 (FolderPrompt
) 时,我的程序无法正常工作,我的意思是我几乎每次 运行 时都会得到不同的答案。
有效:
$referencePath = Read-Host -Prompt "Enter new DTNA folder path to check"
NameDisc
SizeDisc
function NameDisc {
write-host "Name Discrepancy: " -NoNewline
if (Compare-Object -Property name (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
function SizeDisc {
write-host "Size Discrepancy: " -NoNewline
if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
但这不是:
FolderPrompt
NameDisc
SizeDisc
function FolderPrompt {
$referencePath = Read-Host -Prompt "Enter new DTNA folder path to check"
}
function NameDisc {
write-host "Name Discrepancy: " -NoNewline
if (Compare-Object -Property name (Get-ChildItem $referencePath) -DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
function SizeDisc {
write-host "Size Discrepancy: " -NoNewline
if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
我试过了:
在调用之前声明函数
每次输入$referencePath = 0
重置值
认为这是问题所在
将Return $referencePath
放在不同函数的末尾
我最好的猜测是我需要做类似 function <name> ($referencePath)
的事情来传递变量 (?)。
$referencepath
一旦你分配给它就成为函数的局部,所以它的值会丢失,因为你没有 return 它。你说你在 "various functions" 中尝试过 returning 它,但不清楚它看起来像什么。
您也不应依赖从其父作用域继承变量的函数。理想情况下,您会将任何需要的信息作为参数传递。
在 PowerShell 中调用函数时,参数不要使用括号和逗号,而要使用空格。
function FolderPrompt {
Read-Host -Prompt "Enter new DTNA folder path to check"
}
function NameDisc {
param($referencePath)
write-host "Name Discrepancy: " -NoNewline
if (Compare-Object -Property name (Get-ChildItem $referencePath) -DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
function SizeDisc {
param($referencePath)
write-host "Size Discrepancy: " -NoNewline
if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
$refPath = FolderPrompt
NameDisc -referencePath $refPath
SizeDisc -referencePath $refPath
这就是您修改后的代码的样子。
我正在尝试制作一个脚本,其中包含比较两个文件夹项目的功能。节目:
- 提示用户输入文件路径
- 检查文件名是否不同
- 检查文件大小是否不同
作为测试,我一直在将同一个文件夹与其自身进行比较(输出应该是错误的,错误的)。在将第 1 步 ($referencepath
) 设为函数 (FolderPrompt
) 时,我的程序无法正常工作,我的意思是我几乎每次 运行 时都会得到不同的答案。
有效:
$referencePath = Read-Host -Prompt "Enter new DTNA folder path to check"
NameDisc
SizeDisc
function NameDisc {
write-host "Name Discrepancy: " -NoNewline
if (Compare-Object -Property name (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
function SizeDisc {
write-host "Size Discrepancy: " -NoNewline
if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
但这不是:
FolderPrompt
NameDisc
SizeDisc
function FolderPrompt {
$referencePath = Read-Host -Prompt "Enter new DTNA folder path to check"
}
function NameDisc {
write-host "Name Discrepancy: " -NoNewline
if (Compare-Object -Property name (Get-ChildItem $referencePath) -DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
function SizeDisc {
write-host "Size Discrepancy: " -NoNewline
if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
我试过了:
在调用之前声明函数
每次输入
$referencePath = 0
重置值 认为这是问题所在将
Return $referencePath
放在不同函数的末尾
我最好的猜测是我需要做类似 function <name> ($referencePath)
的事情来传递变量 (?)。
$referencepath
一旦你分配给它就成为函数的局部,所以它的值会丢失,因为你没有 return 它。你说你在 "various functions" 中尝试过 returning 它,但不清楚它看起来像什么。
您也不应依赖从其父作用域继承变量的函数。理想情况下,您会将任何需要的信息作为参数传递。
在 PowerShell 中调用函数时,参数不要使用括号和逗号,而要使用空格。
function FolderPrompt {
Read-Host -Prompt "Enter new DTNA folder path to check"
}
function NameDisc {
param($referencePath)
write-host "Name Discrepancy: " -NoNewline
if (Compare-Object -Property name (Get-ChildItem $referencePath) -DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
function SizeDisc {
param($referencePath)
write-host "Size Discrepancy: " -NoNewline
if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
{return $true}
else
{return $false}
}
$refPath = FolderPrompt
NameDisc -referencePath $refPath
SizeDisc -referencePath $refPath
这就是您修改后的代码的样子。