Powershell作为脚本运行不起作用?

时间:2014-09-16 13:28:43

标签: powershell replace find backup back

为什么我不能运行我的Powershell脚本?

代码:

$CurrentDate = Get-Date
$Logname = $CurrentTime.ToString("yyyyMMdd") + "_ReplaceChars.txt"
$LogFile = "C:\Users\Administrator\Desktop\Eurocard\ReplaceJob\$Logname"

#Get's the path name for all files in specified folder and puts it into variable $_
(Get-ChildItem "C:\Users\Administrator\Desktop\Eurocard\SEB\*.*" -recurse).FullName |
Foreach-Object {
    $Content = (Get-Content $_ -Raw) 
    $Regex = '[*"#¤&()=?´`|@£${\[\]}^~¨*<>\\_;.!¾]'
    $Filename = Split-Path $_ -leaf

    #Checks if there are any Characters that match $Regex in the string from $Content.
    If ($Content | Select-String $Regex -quiet)
    {
    "*************************`r`n$CurrentDate Content Before we Replace in file: $_ `r`n$Content" | Out-File $LogFile -Append -Force -Encoding UTF8

        If (Test-Path "C:\Users\Administrator\Desktop\Eurocard\Backup\$Filename"){
        #If original file already exists make a new file with a modified name so we don't overwrite the original.
        "$CurrentDate Content After we Replace in file: $_ `r`n" | Out-File $LogFile -Append -Force -Encoding UTF8
            copy-item $_ -destination "C:\Users\Administrator\Desktop\Eurocard\Backup\Modified_$Filename" -Force -Verbose
            $Content -Replace $Regex,'Æ' | Out-File $LogFile -Append -Force -Encoding UTF8
            Out-File $_
            "`r`n*************************" | Out-File $LogFile -Append -Force -Encoding UTF8
        }
        Else{
        #If there is no original file in the backup folder make one.
        "$CurrentDate Content After we Replace in file: $_ ( Original ) `r`n" | Out-File $LogFile -Append -Force -Encoding UTF8
            copy-item $_ -destination "C:\Users\Administrator\Desktop\Eurocard\Backup" -Force -Verbose
            $Content -Replace $Regex,'Æ' | Out-File $LogFile -Append -Force -Encoding UTF8
            Out-File $_ 
            "`r`n*************************" | Out-File $LogFile -Append -Force -Encoding UTF8  
        }

    }
    Else{
    "$CurrentDate Nothing to Replace" | Out-File $LogFile -Append -Force -Encoding UTF8
   }
}

当我尝试在编辑模式下以调试模式(F5)运行它时,一切正常。

当我想保存它然后像&#34;那样运行文件时使用Powershell&#34;

运行

我在Powershell控制台中遇到了很多错误:

You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Desktop\Eurocard\Shell\SEB_Replace_Chars.ps1:3 char:1
+ $Logname = $CurrentTime.ToString("yyyyMMdd") + "_ReplaceChars.txt"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Out-File : Could not find a part of the path 'C:\Users\Administrator\Desktop\Eu
rocard\ReplaceJob\'.
At C:\Users\Administrator\Desktop\Eurocard\Shell\SEB_Replace_Chars.ps1:37 char:
41
+     "$CurrentDate Nothing to Replace" | Out-File $LogFile -Append -Force -Enc
odi ...
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundExce
   ption
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
   tFileCommand

Out-File : Could not find a part of the path 'C:\Users\Administrator\Desktop\Eu
rocard\ReplaceJob\'.
At C:\Users\Administrator\Desktop\Eurocard\Shell\SEB_Replace_Chars.ps1:37 char:
41
+     "$CurrentDate Nothing to Replace" | Out-File $LogFile -Append -Force -Enc
odi ...
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundExce
   ption
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
   tFileCommand

Out-File : Could not find a part of the path 'C:\Users\Administrator\Desktop\Eu
rocard\ReplaceJob\'.
At C:\Users\Administrator\Desktop\Eurocard\Shell\SEB_Replace_Chars.ps1:37 char:
41
+     "$CurrentDate Nothing to Replace" | Out-File $LogFile -Append -Force -Enc
odi ...
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundExce
   ption
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
   tFileCommand

Out-File : Could not find a part of the path 'C:\Users\Administrator\Desktop\Eu
rocard\ReplaceJob\'.
At C:\Users\Administrator\Desktop\Eurocard\Shell\SEB_Replace_Chars.ps1:37 char:
41
+     "$CurrentDate Nothing to Replace" | Out-File $LogFile -Append -Force -Enc
odi ...
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundExce
   ption
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
   tFileCommand

Out-File : Could not find a part of the path 'C:\Users\Administrator\Desktop\Eu
rocard\ReplaceJob\'.
At C:\Users\Administrator\Desktop\Eurocard\Shell\SEB_Replace_Chars.ps1:37 char:
41
+     "$CurrentDate Nothing to Replace" | Out-File $LogFile -Append -Force -Enc
odi ...
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundExce
   ption
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
   tFileCommand

Out-File : Could not find a part of the path 'C:\Users\Administrator\Desktop\Eu
rocard\ReplaceJob\'.
At C:\Users\Administrator\Desktop\Eurocard\Shell\SEB_Replace_Chars.ps1:37 char:
41
+     "$CurrentDate Nothing to Replace" | Out-File $LogFile -Append -Force -Enc
odi ...
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundExce
   ption
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
   tFileCommand

Out-File : Could not find a part of the path 'C:\Users\Administrator\Desktop\Eu
rocard\ReplaceJob\'.
At C:\Users\Administrator\Desktop\Eurocard\Shell\SEB_Replace_Chars.ps1:37 char:
41
+     "$CurrentDate Nothing to Replace" | Out-File $LogFile -Append -Force -Enc
odi ...
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundExce
   ption
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
   tFileCommand

Out-File : Could not find a part of the path 'C:\Users\Administrator\Desktop\Eu
rocard\ReplaceJob\'.
At C:\Users\Administrator\Desktop\Eurocard\Shell\SEB_Replace_Chars.ps1:37 char:
41
+     "$CurrentDate Nothing to Replace" | Out-File $LogFile -Append -Force -Enc
odi ...
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundExce
   ption
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
   tFileCommand

PS C:\Users\Administrator\Desktop\Eurocard\Shell>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

你得到的错误告诉你究竟出了什么问题。从一开始就是你的问题

$CurrentDate = Get-Date
$Logname = $CurrentTime.ToString("yyyyMMdd") + "_ReplaceChars.txt"
$LogFile = "C:\Users\Administrator\Desktop\Eurocard\ReplaceJob\$Logname"

第2行似乎你应该使用$ CurrentDate,而不是$ CurrentTime。从那里一切都失败了,因为$ LogName永远不会填充变量。

它可能在ISE中起作用,因为你已经在内存中丢失了变量,当你关闭会话时丢失了变量,并且因为你从不重新填充它,脚本就会失败。