Powershell - нужен скрипт для запуска независимо от PWD - PullRequest
1 голос
/ 13 марта 2020

Вот ошибка, которую я получаю, если папка, содержащая файл .ps1, не находится в c: \ Если PWD не совпадает с папкой, в которой находится папка, сценарий завершается ошибкой. Я попытался изменить путь от абсолютного к относительному и обратно, и это все еще не удается. Помогите !!

System.OutOfMemoryException : The term 'System.OutOfMemoryException' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of 
the name, or if a path was included, verify that the path is correct and try again.
At C:\IT\Requirements1\prompts.ps1:35 char:15
+         catch{System.OutOfMemoryException}
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (System.OutOfMemoryException:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Вот мой код:

#Jamal Shabazz Student ID : 001030858

#Decleration of variables.
$fp = "c:\Requirements1"
$cp = "c:\Requirements1\Dailylog.txt"
$cp2 = "c:\Requirements1\C916contents.txt"
$regexp = '.log$'
$selection = 0


#Main tasks to be executed.

While ($selection -ne 5) #returns the user to the task selection menu until option 5 is selected.
{
    try{
        Switch ($selection) #commands to execute based on user selection.
        {
            1{get-date | out-file -FilePath $cp -append
            Get-ChildItem -path $fp | Where-Object {$_.Name -match $regexp}|Out-File -FilePath $cp -Append; continue}                                      #Lists the .log files in the requirements folder and saves to new file.     
            2{Get-ChildItem -Path $fp| Sort-Object | Format-Table |Out-File -filepath $cp2 ; continue}                                                     #Lists files in the requirements folder and saves to new file   
            3{Get-Counter -Counter "\Processor(_Total)\% Processor Time","\Memory\% Committed Bytes In Use"  -SampleInterval 5 -MaxSamples 4 ; continue}   #displays % of CPU usage and Memory usage.
            4{Get-Process | sort-object -Descending -property cpu | Out-GridView ; continue}                                                               #Lists the running processes in a new window in grid format.
            5{break}                                                                                                                 #Exits the application.
        }
#Menu presented to the user.
$selection = Read-Host "Choose the following  
1.List .log files.
2.List files in Requirements1 folder 
3.View Current CPU & Memory Usage
4.List Running Processes
5.Exit
"

        }
        catch{System.OutOfMemoryException}

} 

1 Ответ

0 голосов
/ 13 марта 2020

Я нашел проблему. Я изменил путь к файлу на относительный, а не абсолютный во всем коде, и он работал как шарм

#Jamal Shabazz Student ID : 001030858

#Decleration of variables.
$psscriptroot
try{
    $fp = ".\"
    $cp = ".\Dailylog.txt"
    $cp2 = ".\C916contents.txt"
    $regexp = '.log$'
    $selection = 0
    }
    catch{System.OutOfMemoryException}


#Main tasks to be executed.

While ($selection -ne 5) #returns the user to the task selection menu until option 5 is selected.
{

        Switch ($selection) #commands to execute based on user selection.
        {
            1{get-date | out-file -FilePath $cp -append
            Get-ChildItem -path $fp | Where-Object {$_.Name -match $regexp}|Out-File -FilePath $cp -Append; continue}                                      #Lists the .log files in the requirements folder and saves to new file.     
            2{Get-ChildItem -Path $fp| Sort-Object | Format-Table |Out-File -filepath $cp2 ; continue}                                                     #Lists files in the requirements folder and saves to new file   
            3{Get-Counter -Counter "\Processor(_Total)\% Processor Time","\Memory\% Committed Bytes In Use"  -SampleInterval 5 -MaxSamples 4 ; continue}   #displays % of CPU usage and Memory usage.
            4{Get-Process | sort-object -Descending -property cpu | Out-GridView ; continue}                                                               #Lists the running processes in a new window in grid format.
            5{break}                                                                                                                 #Exits the application.
        }
#Menu presented to the user.
$selection = Read-Host "Choose the following  
1.List .log files.
2.List files in Requirements1 folder 
3.View Current CPU & Memory Usage
4.List Running Processes
5.Exit
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...