Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment work, you will analyze, create and test Windows PowerShell scripting files to manage local and remote hosts with focus on system settings.

In this assignment work, you will analyze, create and test Windows PowerShell scripting files to manage local and remote hosts with focus on system settings.

Tasks:

  1. Run Windows 2012R2/2016 servers or 8.1/10 (in domain) host, then open Windows PowerShell command window and ISE as administrator. Analyze the following script, and work out tasks a), b) and c).

function Get-File ( [string[]] $file = "Default" )

{

foreach ($i in $file) {

if (test-path $i) {Get-ChildItem $i }

else {"$i file doesn't exist in the current directory."}

}

}

a). Explain how to make this function as global-scoped.

b). Make this function as global scoped in a new Windows PowerShell console window. Test if this function is available to be called.

Note down the output of get-file *.com command.

c). Update the function parameter so that it supports pipeline.

List your updated script and note down the output of “*.com” | get-file command in a new PowerShell console Window.

2. Analyze the following script. Create the file named as diskspace.ps1 by Windows PowerShell ISE.

#Begin

Function Get-Testresult {

[CmdletBinding()]

Param(

[Parameter(ValueFromPipeline)]

$item )

Write-Host "You passed the free space size $item GB into the free space analysis function."

if ($item -le 100) {write-host "Free Space is too limited! Please act upon instantly." –foreground Red}

elseif ($item -ge 5000) {write-host "The disk space can be shared with more others." –foreground green}

else {"Normal free space."}

}

$disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" |Select-Object Size, FreeSpace

$tdisk=$disk.Size/1gb

"Total disk space is $tdisk GB"

$fdisk=$disk.FreeSpace/1gb

$fdisk | Get-Testresult

#End

Run the script file, screenshot the output and paste here.

Answer the following questions.

  • How can you use get-help to verify if function parameter item supports pipeline or not?
  • Which WMI class is used in the script?
  • Can you replace $fdisk | Get-Testresult by Get-Testresult -item $fdisk in the script?

3. Analyze the following script file (create and save it as logfile1.ps1).

# PowerShell script file to find and sort Windows log files

#Begin

Clear-Host

$Directory = "C:\Windows\"

$Phrase = "Error"

$Files = Get-Childitem $Directory -recurse -Include *.log -ErrorAction SilentlyContinue

$Files | Select-String $Phrase -ErrorAction SilentlyContinue | Group-Object filename | Sort-Object count –descending

# End

Test it and answer questions. (Note: it may take over 50 seconds to get the output.)

What is the alias for Clear-host? _________________________

What is the alias for Get-childitem? _________________________

What is –recursesearch? __________________________

What does “-ErrorAction SilentlyContinue” do in the script? _____________________

Explain select-string cmdlet:

4. Consult with the above logfile1.ps1 script file, create a new script named logfile2.ps1. Basic requirements:

  • Prompts administrator to key in a key word that may appear in log files under C:\Windows\logs
  • List the log file (which contains the key word) name(s) and counts.

Your script:

--------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------.

Test your script file, screenshot and paste the testing result here.

5. Revise the above script file as a function named as get-logfile, pipeline administrator’s typed key word to the function which lists the related log file names and counts. Save this new script file as logfile3.ps1

Copy and paste your new script with function.

--------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------.

Test it by typing in keywords such as error, fail, CPU and network as in the following commands. Screenshot and paste the output of each command here.

“error”|get-logfile

“fail” |get-logfile

“cpu”|get-logfile

“network”|get-logfile

6. Test the following script which lists specific type of files in a preferred computer.

Save the script file as remotelist1.ps1.

Before testing the script, please enable PowerShell Remoting on the target computer (either localhost or any other host with PSRemoting enabled).

Note: this task needs to test the c:\pswork directory which you created in previous labs. If this directory is not available, create it and copy some sample .ps1/.txt/.csv/.pdf files here.

#Begin

$ComputerName = read-host ("Please enter ComputerName for Remoting connectivity test and .ps1 file list")

Write-host "Reminder: Remoting test is on Computer-- $Computername."

$status = Test-Connection -ComputerName $Computername -count 1

if ($status.statuscode -eq 0) {

Write-host “Connection test result is $True. $ComputerName’s list of PS files:"

Invoke-Command -ComputerName $ComputerName –scriptblock {cd c:\pswork ; dir *.ps1 –recurse –errorAction SilentlyContinue |select fullname, length, creationtime, lastwritetime }

}

else {Write-host “Connection test result is $False.” -ForegroundColor DarkRed }

#End

Notes: In “Test-Connection -ComputerName $Computername -count 1”, count specifies the number of echo requests to send. The default value is 4.

Test it by using localhost and/or any other host. Screenshot and paste the outputs here.

Answer the following to questions and give testing results.

How to update the command of the following to make the script block execution in background as PowerShell job? And how to retrieve the results of a PowerShell job?

___________________________________________________________________________ .

Invoke-Command -ComputerName $ComputerName –scriptblock {cd c:\pswork ; dir *.ps1 –recurse –errorAction SilentlyContinue }

7. Consult with the above script file, revise it so that the execution of the new script file (named as remotelist2.ps1) can:

  • Prompt user to key in target computer’s name and file type such as .ps1/.txt/.csv/.pdf/etc.
  • List the specified type of files (in the target computer’s C:\pswork Drive) with the following properties:

Fullname, length, creationtime, lastwritetime

  • Show the number of the above files.

Your new script file remotelist2.ps1:

--------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------.

Test it. Screenshot and paste the outputs here.

8. This task, you will work on “How to Batch Rename Files in Windows by PowerShell”. Copy several PS script files you already created to C:\Users\Admin\Desktop\tmp. (If you prefer a different directory, note down here ____________ and replace C:\Users\Admin\Desktop\tmp with it in the following script file.)

Analyze the script file. Create an save it as changenames.PS1

#Begin

[int] $i=1

Foreach ($file in get-childitem C:\Users\Admin\Desktop\tmp *.ps1)

{

write-host PS File#: $i

write-host old PS File: $file.name

$newname=$file.name –replace “.ps1“ , "$i.ps1”

cd C:\Users\Admin\Desktop\tmp

Rename-Item $file.name $newname

write-host new PS File: $newname

" "

$i++

}

Write-host “Total number of PS file name change is $($i-1), please check the new names in that folder.”

#End

What are the new file names after you execute the script file once? _____________________.

Consult with the above script, create a new script file (name it as resumenames.PS1) so that you resume the original names after you run it once. List your new script file.

--------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------.

Test your script file. Copy and paste outputs.

9. Create a new script file named as listservice.ps1.

Tasks of this script file:

  • Prompt administrator to key in the name of computer that will be managed.
  • Prompt administrator the credentials for authentication.
  • Display the target’s all stopped Services’ Name/Displayname/Satus/Canstop information.

Also save the above information in a text file named servicestopped.txt (in your local host’s C:\pswork).

Your scripts:

--------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------.

After executing this script file, read and verify the created servicestopped.txt text file manually. Screenshot and paste here. Test it by localhost or any remote host as target.

10. Analyze the following script file.

#Begin

function add-newuser

{

$gname=read-host “Please key in domain new user’s Given Name:”

$sname=read-host “Please key in domain new user’s Sur Name:”

$pass=read-host “Please key in domain new user’s password:”

New-ADUser -name “$gname $sname” -SamAccountName “$gname $sname” -AccountPassword (ConvertTo-SecureString “$pass” -AsPlainText -Force) -DisplayName “$gname $sname” -Enabled $True -GivenName $gname -Surname $sname

write-host “New user account is now created properly!” -ForegroundColor Green

}

function list-adusers

{

write-host “List of doamin users’ information” -ForegroundColor Green

get-ADUser -filter * |ft

}

function d-user

{

$name1=read-host “Key in the user name which you want to remove from your domain”

remove-aduser $name1

}

$option=”Null”

do

{

write-host “Please type L to list your domain users,

type A to add a new domain user account,

type R to remove exiting domain user account

and type any other key to exit:” -ForegroundColor Green

$option=read-host

$x=1

switch ($option)

{

‘L’ {list-adusers}

‘A’ {add-newuser}

‘R’ {d-user}

Default {$x=2}

}

}

until ($x -eq 2)

#End

Explain what job function(s) this script can provide. _________________________ .

In ADDS server, create and save the file as managedomainuser.ps1. Test it and screenshot the outputs.

Also verify the effectiveness by using AD Users and Computers (ADUC) or AD Administrative Center (ADAC), make screenshots and paste here.

If you have an existing certificate for code sign, please sign on this script file and save it as ADuser.ps1. Change PowerShell Execution Policy to AllSigned. Make the signed script file executable. (You may create a new certificate to sign on your script file if needed.)

List the signed script file here.

Step by Step Solution

3.42 Rating (158 Votes )

There are 3 Steps involved in it

Step: 1

Run Windows 2012R22016 servers or 8110 in domain host then open Windows PowerShell command window and ISE as administrator Analyze the following script and work out tasks a b and c function GetFile st... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Principles Of Information Security

Authors: Michael E. Whitman, Herbert J. Mattord

7th Edition

035750643X, 978-0357506431

More Books

Students also viewed these Programming questions