Board logo

标题: [问题求助] 网上下载的PowerShell,改了改path,发现file2 识别不了,请高手指点!谢谢 [打印本页]

作者: PS_2015    时间: 2015-1-16 13:39     标题: 网上下载的PowerShell,改了改path,发现file2 识别不了,请高手指点!谢谢

  1. $file1 = GI C:\TEMP\schlecht\Settings.xml
  2. $file2 = GI C:\TEMP\gut\Settings.xml    #路径是对的,确认了n遍
  3. function Compare-Files{
  4. param(
  5. $file1,$file2,[switch]$IncludeEqual)
  6. $content1 = Get-Content $file1
  7. $content2 = Get-Content $file2
  8. $comparedLines = Compare-Object $content1 $content2 -IncludeEqual:$IncludeEqual |
  9. group { $_.InputObject.ReadCount } # | sort Name
  10. $comparedLines | foreach {
  11. $curr=$_
  12. switch ($_.Group[0].SideIndicator){
  13. “==” { $right=$left = $curr.Group[0].InputObject;break}
  14. “=>” { $right,$left = $curr.Group[0].InputObject,$curr.Group[1].InputObject;break }
  15. “<=" { $right,$left = $curr.Group[1].InputObject,$curr.Group[0].InputObject;break }
  16. }
  17. [PSCustomObject] @{
  18. Line = $_.Name
  19. Left = $left
  20. Right = $right
  21. }
  22. }
  23. }
复制代码
下图是报警

Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At C:\TEMP\PS\test_1.ps1:16 char:26
+  $content2 = Get-Content $file2
+                          ~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

Compare-Object : Cannot bind argument to parameter 'DifferenceObject' because it is null.
At C:\TEMP\PS\test_1.ps1:17 char:44
+  $comparedLines = Compare-Object $content1 $content2 -IncludeEqual:$IncludeEqual ...
+                                            ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Compare-Object], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CompareObjectCommand

Cannot index into a null array.
At C:\TEMP\PS\test_1.ps1:20 char:2
+  $curr=$_
+  ~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
作者: yiwuyun    时间: 2015-3-27 10:53

  1. # 2>nul&@cls&@echo off&powershell -command "&{set-executionPolicy remoteSigned}"&copy "%~f0" "%~f0.ps1">nul&powershell -file "%~f0.ps1"&del "%~f0.ps1"&pause&exit /b 0
  2. $file1 = GI C:\TEMP\schlecht\Settings.xml;
  3. $file2 = GI C:\TEMP\gut\Settings.xml;
  4. function Compare-Files{
  5.   param(
  6.    $file1,
  7.    $file2,
  8.    [switch]$IncludeEqual
  9.   )
  10.   $content1 = Get-Content $file1;
  11.   $content2 = Get-Content $file2;
  12.   $comparedLines = Compare-Object $content1 $content2 -IncludeEqual:$IncludeEqual | group { $_.InputObject.ReadCount } | sort Name;
  13.   $comparedLines | foreach {
  14.     $curr=$_
  15.     switch ($_.Group[0].SideIndicator){
  16.       "==" { $right=$left = $curr.Group[0].InputObject;break}
  17.       "=>" { $right,$left = $curr.Group[0].InputObject,$curr.Group[1].InputObject;break }
  18.       "<=" { $right,$left = $curr.Group[1].InputObject,$curr.Group[0].InputObject;break }
  19.     }
  20.     [PSCustomObject] @{
  21.       Line = $_.Name;
  22.       Left = $left;
  23.       Right = $right;
  24.     }
  25.   }
  26. }
  27. Compare-Files -file1 $file1 -file2 $file2 -IncludeEqual;
复制代码
bat 和powershell均通过。程序是对的,是不是你的策略设置有问题。




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2