Download presentation
Presentation is loading. Please wait.
Published byScarlett Hensley Modified over 9 years ago
1
CN1266 Network Scripting Kemtis Kunanuraksapong MSIS with Distinction MCTS, MCDST, MCP, A+
2
Agenda Chapter 18: Mission Control: All Systems Go Chapter 19: Taming the Windows Registry Quiz Exercise
3
Monitor Free Disk Space Listing 18-1 on Page 248 This script use Windows Script Host method ▫See Table 18-1 and 18-2 for more value Function WarningPopup ([string]$message) { ▫$oShell = New-Object -comobject Wscript.Shell ▫$ret = $oShell.popup($message,0,"Warning",48) } Time18-1
4
Monitor Free Disk Space (2) We use.NET class in order to use GetDrives method ▫$drives = [System.IO.DriveInfo]::GetDrives()
5
Monitor Free Disk Space – WMI Version See Listing 18-2 on Page 251 if ($drive.DriveType -eq 3) See Table 18-3 on Page 252
6
Managing Windows Services To see whether the services is running or not See code on Page 253
7
Controlling services To make sure that necessary services are running at all time See code on Page 255
8
Checking your event logs See code on Page 257 To get the event remotely, see code on Page 259
9
Registry What is Windows Registry? *WARNING* Always backup your registry before you run the script The registry tree : ▫HKEY_CLASSES_ROOT ▫HKEY_CURRENT_USER ▫HKEY_LOCAL_MACHINE ▫HKEY_USERS ▫HKEY_CURRENT_CONFIG
10
Registry (2) Registry values ▫See Table 19-2 on Page 263 To manage registry ▫Regedit.exe ▫Microsoft.Win32.RegistryKey
11
To see all the drives in PSH Try these commands: ▫Get-Drive ▫cd HKLM: ▫cd software\microsoft\windows\ ▫dir
12
To access registry via PSH OpenRemoteBaseKey() to connect to the desired registry hive OpenSubKey() to open a specific subkey GetValue() on the subkey to get the data for a given subkey value See code on Page 266 ▫Substitute line 2, LocalMachine, to another value as stated on Page 266 to access another registry hive
13
Reading Keys and Values Try these code: ▫Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVe rsion\Run ▫$regkey = Get-ItemProperty “HKLM:\Software\Microsoft\Windows NT\CurrentVersion” ▫$os = $regkey.ProductName ▫$servicePack = $regkey.CSDVersion ▫Write-Host ($os + “ “ + $servicePack)
14
Writing Keys and Values To add new registry folder ▫New-Item HKLM:\Software\MyPSH To add new entry/item ▫New-ItemProperty –path HKLM:\Software\MyPSH –name ConfigParams – PropertyType Dword –value 23 If the item is existed: ▫Set-ItemProperty –path HKLM:\Software\MyPSH –name ConfigParams – value 15
15
Writing by Microsoft.Win32.RegistryKey To add new registry folder ▫$computername = $env:computername ▫$regHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBas eKey(“LocalMachine”,$computerName) ▫$regHKLM.CreateSubKey("Software\MyPSH2") ▫$regKey = $regHKLM.OpenSubKey(“Software\MyPSH2”, $true) ▫$regKey.setValue(‘ConfigParams’,23,’dword’)
16
Renaming and deleting Keys and Values To rename registry folder/entry ▫Rename-Item –path HKLM:\Software\MyPSH – newname NewPSH ▫Rename-ItemProperty –path HKLM:\Software\NewPSH –name ConfigParams – NewName NewParam To remove registry folder/entry ▫Remove-Item HKLM:\Software\NewPSH ▫Remove-ItemProperty –path HKLM:\Software\NewPSH –name NewParam If the folder is not empty ▫Remove-Item HKLM:\Software\NewPSH -Recurse
17
Renaming and deleting by Microsoft.Win32.RegistryKey To delete ▫$compname = $env:computername ▫$regHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBas eKey (“LocalMachine”,$compname) ▫$regKey = $regHKLM.OpenSubKey (“Software\MyPSH”, $true) ▫$regKey.DeleteValue(“MyPSH”)
18
Deleting by Microsoft.Win32.RegistryKey $compname = $env:computername $regHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBa seKey (“LocalMachine”,$compname) $regKey = $regHKLM.OpenSubKey (“Software\MyPSH”, $true) $regKey.DeleteValue(“MyPSH”)
19
Deleting by Microsoft.Win32.RegistryKey (2) To delete subkey ▫$compname = $env:computername ▫$regHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBas eKey (“LocalMachine”,$compname) ▫$regKey = $regHKLM.OpenSubKey (“Software\MyPSH”, $true) ▫$regKey.DeleteSubKey(“Software\MyPSH”) To delete everything ▫$regHKLM.DeleteSubKeyTree (“Software\MyPSH”)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.