如果包含.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}
}
答案 0 :(得分:0)
我发现了问题。我在整个代码中都将文件路径更改为相对路径,而不是绝对路径,它的工作原理很吸引人
#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