Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 为什么在使用替换方法时会出现此错误_Powershell - Fatal编程技术网

Powershell 为什么在使用替换方法时会出现此错误

Powershell 为什么在使用替换方法时会出现此错误,powershell,Powershell,我试图列出路径的第一级和第二级文件夹。脚本工作正常,但我有一个错误“不能对空值表达式调用方法”。知道为什么吗 $folderPath = '\\FILSERVER\DATA$' $PathScript = "C:\Users\adm\Desktop\Script_V.2" $sites = "Madrid" foreach ($site in $Sites){ #Get_Level_1_Folders $PathShare = "\\FILSE

我试图列出路径的第一级和第二级文件夹。脚本工作正常,但我有一个错误“不能对空值表达式调用方法”。知道为什么吗

$folderPath = '\\FILSERVER\DATA$'
$PathScript = "C:\Users\adm\Desktop\Script_V.2"
$sites = "Madrid"
foreach ($site in $Sites){

#Get_Level_1_Folders
$PathShare = "\\FILSERVER\DATA$\Data_$site"
Get-ChildItem -Path $PathShare -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName | out-file "${PathScript}\level_1_${site}.txt"
(get-content "${PathScript}\level_1_${site}.txt") -notmatch "--------" | out-file "${PathScript}\level_1_${site}.txt"
(get-content "${PathScript}\level_1_${site}.txt").replace("\\FILSERVER\DATA$\Data_$site\","" ) | out-file "${PathScript}\level_1_${site}.txt"
(get-content "${PathScript}\level_1_${site}.txt") -notmatch "FullName" | out-file "${PathScript}\level_1_${site}.txt"
(get-content "${PathScript}\level_1_${site}.txt") | Foreach {$_.TrimEnd()} | Set-Content "${PathScript}\level_1_${site}.txt"
(get-content "${PathScript}\level_1_${site}.txt") | ? {$_.trim() -ne "" } | set-content "${PathScript}\level_1_${site}.txt"

#Get_Level_2_Folders
$Level_Folders = get-content "${PathScript}\level_1_${site}.txt"
foreach($lv1 in $Leve1_Folders){
Get-ChildItem -Path $PathShare\$lv1 -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName | out-file "${PathScript}\level_2_${site}_${lv1}.txt"
(get-content "${PathScript}\level_2_${site}_${lv1}.txt") -notmatch "--------" | out-file "${PathScript}\level_2_${site}_${lv1}.txt"
(get-content "${PathScript}\level_2_${site}_${lv1}.txt").replace("\\FILSERVER\DATA$\Data_$site\","") |  out-file "${PathScript}\level_2_${site}_${lv1}.txt"
(get-content "${PathScript}\level_2_${site}_${lv1}.txt") -notmatch "FullName" | out-file "${PathScript}\level_2_${site}_${lv1}.txt"
(get-content "${PathScript}\level_2_${site}_${lv1}.txt") | Foreach {$_.TrimEnd()} | Set-Content "${PathScript}\level_2_${site}_${lv1}.txt"
(get-content "${PathScript}\level_2_${site}_${lv1}.txt") | ? {$_.trim() -ne "" } | set-content "${PathScript}\level_2_${site}_${lv1}.txt"
}

如注释中所述,原因可能是此可扩展字符串:

"${PathScript}\level_2_${site}_${lv1}.txt"
。。。已解析为空文件的路径

Get Content
将打开文件-这就是为什么您没有得到任何“file not found”错误-然后立即返回而不输出任何内容,因为空文件中没有可使用的有意义的“行”

因此,
(Get Content…
表达式的结果是
$null
,您收到了有问题的错误

您可以使用
-replace
运算符,该运算符将接受任意数量的字符串(包括无字符串)作为输入-只需确保转义参数:

(Get-Content "${PathScript}\level_2_${site}_${lv1}.txt") -replace [regex]::Escape("\\FILSERVER\DATA$\Data_$site\") |Out-File ...
或者让管道负责枚举输出,而不是依赖隐式属性枚举:

Get-Content "${PathScript}\level_2_${site}_${lv1}.txt" |ForEach-Object {$_.Replace("\\FILSERVER\DATA$\Data_$site\","")} |Out-File ...

添加完整错误,这样我们就可以看到它说的是$nullYou不能对空值表达式调用方法。在第22行char:1+(获取内容“${PathScript}\level\u 2{site}{u${lv1}.txt”)。替换(“\\s…++CategoryInfo:InvalidOperation:(:)[],RuntimeException+FullyQualifiedErrorId:invokeMethodNonull”,不能在空值表达式上调用方法。在第22行char:1+(获取内容“${PathScript}\level\u 2{site}{lv1}.txt)替换(\\s…++CategoryInfo:InvalidOperation:(:)[],RuntimeException+FullyQualifiedErrorId:InvokeMethodUnull引起对齐的问题是:(获取内容“${PathScript}\level\u2\u${site}}{lv1}.txt”)。替换(\\FILSERVER\DATA$\DATA\u$site\,“”)out文件“${PathScript}\level u2\u${site}lv1}.txt”)“@AbdelkrimBalaboula这意味着字符串
“${PathScript}\level\u 2\u${site}{u${lv1}.txt”
解析为一个空文件的文件名。实际上,这些文件是空的,这就是为什么..谢谢