2014-09-16 4 views
0

Почему я не могу запустить свой сценарий Powershell?Powershell Run as Script Не работает?

Код:

$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) в режиме редактирования, все работает.

Когда я хочу, чтобы сохранить его, а затем запустить файл как «Запуск с Powershell»

Я просто много ошибок в 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> 

Как это исправить?

ответ

2

Ошибки, которые вы получаете, говорят вам точно, что не так. С самого начала это ваша проблема

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

Line 2 появляется, что вы должны использовать $ CurrentDate, а не $ CURRENTTIME. Оттуда все остальное терпит неудачу, потому что $ LogName никогда не заполняет эту переменную.

Возможно, он работал в ISE, потому что у вас есть переменная, которой вы отсутствовали уже в памяти, когда вы закрыли сеанс, вы потеряли переменную, и поскольку вы никогда не перезаписываете его, сценарий терпит неудачу.

+0

О, боже, ха-ха, теперь это работает .. Спасибо! –

+0

Lol, добро пожаловать. –