简体   繁体   中英

Powershell Run as Script Doesn't work?

Why can't i run my Powershell Script ?

Code:

$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
   }
}

When i try to run it in Debug mode ( F5 ) in Edit mode, everything works.

When i want to save it and then run the file like " Run with Powershell "

I just get alot of errors in the Powershell Console:

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>

How do i fix this ?

The errors you are getting are telling you exactly what is wrong. Right from the beginning this is your problem

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

Line 2 there appears that you should be using $CurrentDate, not $CurrentTime. From there everything else is failing because $LogName never populates the variable.

It probably worked in the ISE because you have the variable you were missing already in memory, when you closed the session you lost the variable and since you never repopulate it the script fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM