Powershell Questions Section-1: 1. Search for any txt files in C: drive. 2. Get
ID: 3743290 • Letter: P
Question
Powershell Questions
Section-1:
1. Search for any txt files in C: drive.
2. Get txt file of full path which has max size.
3. Copy the txt file to TEMP location.
4. Search for a specific key in the text of it and find the number of hits
Section-2
1. Get EventLogs from LogName Application with last two days of datetime range.
2. Write the logs to Json format object.
3. Find the objects which has "Fail" in Message property.
section-3
1. Create sample functions which can have parameter setnames.
section-4
1. Use PSSessions for below computers (your local)
$cn = $env:COMPUTERNAME,"localhost","127.0.0.1"
2. Perform above section 1 and section 2 tasks.
Explanation / Answer
section-1:
solution for question2:
get-childitem ":windowsSystem32" -recurse | where {$extension -eq ".txt" " | { Write -Host $_.FullName}
Add |select FullName to the end of your line above. if you ned to actually do something with that afterwards, you might have to pipe into a foreach loop , like given above.
Solution for question 3:
Copy-Item -Recurse -Filter *.txt -path c: emp -destination c:sdf
The PowerShell get-help command is very useful and it does except wildcards.
ie
get-help *copy* gives you a list of commands that might be useful to you
get-help Copy-Item -full gives you all the parameters pluus examples of usage.
Solution for question 4:
$FileContent =get-Content "YourFile.txt"
$Matches = Select-String -InputObject $FileContent -Pattern "/export" -AllMatches
$Matches.Matches.Count
this code gives the count of specific key in the file.
Section2 :
Question 3:
this code is used to test if an object has a specific property
[bool] ($myObject.PSobjet.Properties.name -match "myPropertyNameToTest")
find object;s property value using property name:
write-Host ($obj | select -ExpandProperty "SomeProp")
Or for that matter:
$obj."SomeProp"
Section 3 question 1:
POWERSHELL HELP DESCRIBE THE GENERAL FORM OF FUNCTIONS AS FOLLOWS:
function [<scope:>]<name> [([type]$parameter1[,[type]$parameter2])]{
param([type]$parameter1[,[type]$parameter2]
dynamicparam{<statement list>}
begin{<statement list>}
process{<statement list>}
end {<statement list>}
}
The example is:
function Get-Something
{
[CmdletBinidng(DefaultParameterSetName='Name')]
Param
(
[Parameter(Mandatory=$true, Position=0, ParameterSetName='Name')]
[string] $Name,
[Parameter(Mandatory =$true, Position=0, ParameterSetName='Id')]
[int] $Id
)
}
The function will either take a name, or an id, but not both.You can use them positionally, or bt name. Since they are a diffferent tpe, PowerShell will figure it out. So all of these would work:
Get-Something "some name"
Get-Something 23
Get-Something -Name "some name"
Get-Something -Id 23
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.