powershell变量目录返回null
我正在尝试获取一个文件列表,这些文件不是我想要的版本,我创建了一个包含3个变量的函数:powershell变量目录返回null,powershell,Powershell,我正在尝试获取一个文件列表,这些文件不是我想要的版本,我创建了一个包含3个变量的函数: 版本号 文件夹的名称 文件夹的路径 如果文件版本与$version不匹配,我会将行写出来,以便知道文件名和实际版本号 Function Check-Version ($version, $folderName, $folderPath) { Write-Host $version, $folderName, $folderPath $list = get-childitem $folder
- 版本号
- 文件夹的名称
- 文件夹的路径
$version
不匹配,我会将行写出来,以便知道文件名和实际版本号
Function Check-Version ($version, $folderName, $folderPath)
{
Write-Host $version, $folderName, $folderPath
$list = get-childitem $folderPath\* -include *.dll,*.exe
foreach ($one in $list)
{
If ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($one).FileVersion -ne $version)
{
$line = "{0}`t{1}" -f [System.Diagnostics.FileVersionInfo]::GetVersionInfo($one).FileVersion, $one.Name
Write-Host $line
}
}
}
Check-Version ("1.0", "bin", "C:\bin")
我的问题是,当我使用get childitem
时,path变量为NULL,但如果我使用write host
则是正确的
顶部的Write Host
行返回正确的值
如果我尝试cd$folderPath
我会得到错误:
cd:无法处理参数,因为参数“path”的值为null。将参数“path”的值更改为非空值
我不明白为什么当我尝试转到该目录时,
$folderPath
为空。您的问题是将3个参数作为数组传递到第一个参数,而不是传递三个单独的参数。更改检查版本(“1.0”,“bin”,“C:\bin”)
->检查版本“1.0”“bin”“C:\bin”
通过将写入主机
拆分为3行,您可以看到差异:
Function Check-Version ($version, $folderName, $folderPath) {
Write-Host "Version: $version"
Write-Host "FolderName: $folderName"
Write-Host "FolderPath: $folderPath"
$list = get-childitem $folderPath\* -include *.dll,*.exe
Set-Location $folderPath
foreach ($one in $list) {
If ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($one).FileVersion -ne $version) {
$line = "{0}`t{1}" -f [System.Diagnostics.FileVersionInfo]::GetVersionInfo($one).FileVersion, $one.Name
Write-Host $line
}
}
}
Check-Version "1.0" "bin" "C:\bin"
谢谢我应该看到的。