PowerShell v2 Day 4
Blah
Day 4
$Voice = New-Object -ComObject SAPI.SPVoice
$Voice.Speak("blah")
$voice.Rate = -10
$Voice.Speak("hello from powershell")
$voice.Rate = 10
$Voice.Speak("hello from powershell")
Get-Module -ListAvailable
import-module ActiveDirectory -verbose
cd AD:
cd DC=blah,DC=local
New-PSDrive -Name BLAH -PSProvider ActiveDirectory -Root "DC=Blah,DC=Local" -Credential (get-credential)
Get-ADUser -Filter { samaccountname -like "*admin*"}
Get-ADUser -Filter { memberof -recursiveMatch $(grp.DistinguishedName)
Get-ADObject -Filter { ServicePrincipalName -like "*"} -Properties ServicePrincipalName | select Name, ServicePrincipalName
Get-ADUser Kevin | set-ADUser -Replace @{ physicalDeliveryOfficeName = "Boise" }
$user = get-ADUser Kevin - Properties *
$user.office = "Blah"
Set-AdUser -Instance $User
Active Directory Explorer sysinternals
ACLs
gci $HOME -Recurse | ? { ($_ | get-acl).owner -eq "$env:UserDomain\$env:Username"}
$acl = gci C:\config.sys | Get-Acl
$acl | fl *
$acl.AddAccessRule # returns an error of what you need
$ace=New-Object system.security.accesscontrol.FileSystemAccessRule "Domain\User","FullControl","Allow"
$acl.AddAccessRule($ace)
Set-Acl -Path C:\config.sys -AclObject $acl
WMI Type Accelerators page 525
[wmiclass] "Win32_Process"
([wmiclass] "Win32_Process").Create("notepad.exe")
([wmiclass] "\\server01\root\cimv2:Win32_Process").Create("notepad.exe")
.Net way of doing this
[system.Diagnostics.Process]::Start("notepad.exe")
XML
Get-Content blah.xml
$xml=[xml] (get-content blah.xml)
$xml | gm
$xml.
get-command *xml* -CommandType cmdlet
select-xml -Xml $xml -XPath "//site"
select-xml -Xml $xml -XPath "//site" |% { $_.Node}
select-xml -Xml $xml -XPath "//site" | Select-Object -ExpandProperty Node
Type Adaptation
Module 3 page 235
Get-Process explorer | Get-Member -View Extended
Get-Process explorer | Get-Member -View Base
Get-Process explorer | Get-Member -View Adapted
Get-Process explorer | Get-Member -View Adapted,Extended
get-help Get-Member -Parameter view
(Get-Process explorer).psobject
$Process = Get-Process explorer
$Process.psbase
$Process.psobject
$Process.psextended
$Process | Add-Member -MemberType ScriptProperty -Name CurrentTime -Value { Get-Date }
$Process.CurrentTime
$Process.CurrentTime
$Process | Add-Member -MemberType ScriptMethod -Name GetCurrentTime -Value { Get-Date }
$Process.GetCurrentTime()
types.ps1xml
c:\Windows\System32\WindowsPowerShell\v1.0\types.ps1xml
<Type>
<Name>System.Management.Automation.PSDriveInfo</Name>
<Members>
<ScriptProperty>
<Name>Used</Name>
<GetScriptBlock>
## Ensure that this is a FileSystem drive
if($this.Provider.ImplementingType -eq
[Microsoft.PowerShell.Commands.FileSystemProvider])
{
$driveRoot = ([System.IO.DirectoryInfo] $this.Root).Name.Replace('\','')
$drive = Get-WmiObject Win32_LogicalDisk -Filter "DeviceId='$driveRoot'"
$drive.Size - $drive.FreeSpace
}
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>Free</Name>
<GetScriptBlock>
## Ensure that this is a FileSystem drive
if($this.Provider.ImplementingType -eq
[Microsoft.PowerShell.Commands.FileSystemProvider])
{
$driveRoot = ([System.IO.DirectoryInfo] $this.Root).Root.Name.Replace('\','')
$drive = Get-WmiObject Win32_LogicalDisk -Filter "DeviceId='$driveRoot'"
$drive.FreeSpace
}
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
Get-PSDrive | gm
Update-TypeData .\NewTypeDat.ps1xml
Module 3 Exercise 2
Heading foo
ddd
PS Code Here
$To = "Kevin Curran <kcurran@blah.com>"