PowerShell

From AlphaBook
Jump to: navigation, search

Help

  • Update-Help
  • Get-Help Get-Service -Example
  • Get-Help Get-Service -Online
  • Get-Command *process*
  • Get-Command -Verb Get -Noun Process
  • $PSVersionTable

Execution Policy

  • Get-ExecutionPolicy
  • Set-ExecutionPolicy

Module

  • Get-Module -ListAvailable
  • Import-Module -Name ActiveDirectory
  • $env:PSModulePath

Output Format

  • Select-Object
  • Sort-Object
  • Where-Object
  • Get-Member
  • Format-Table
  • Format-List
  • Export-Csv
  • Export-Csv -Encoding UTF8
  • ConvertTo-Csv | Out-File
  • Get-ADUser -Filter * -Properties * | Select-Object SamAccountName,Name,Title,Department,City | Sort-Object City | Format-Table
  • Get-ADUser -Filter * -Properties * | Where-Object City -eq "Beijing" | Select-Object SamAccountName,Name,Title,Department,City | Format-Table
  • Get-Process | Get-Member
  • Get-Process | Where-Object Responding -eq $False

@

  • ###### Example 1 ######
  • $Cities = @("Beijing","Shanghai","Shenzhen")
  • ###### Example 2 ######
  • @{Name='FreeSpace (G)';Expression={$_.FreeSpace/1GB -as [int]}}
  • ###### Example 3 ######
  • $User = @{
    • GivenName = “Terry”
    • Surname = “Shen”
    • Name = “Terry Shen”
    • DisplayName = “Terry Shen”
    • SamAccountName = “Terry.Shen”
    • UserPrincipalName = “Terry.Shen@corp.alphabook.cn”
    • AccountPassword = (ConvertTo-SecureString "123.com" -AsPlainText -Force)
    • ChangePasswordAtLogon = $True
    • Office = “Wudaokou, Haidian”
    • OfficePhone = “010-12345678”
    • City = “Beijing”
    • Country = “CN”
    • Title = “System Engineer”
    • Department = “IT”
    • Manager = “Peter.Li”
    • Enable = $True
  • }
  • New-ADUser @User

Import data

  • Get-Content C:\ServerList.txt
  • Import-csv C:\Users.csv -Delimiter ;

ForEach

  • $Servers = Get-Content C:\ServerList.txt
  • ForEach ($Server in $Servers) {}

Try Catch

  • Try {}
  • Catch {}

Compare

  • -eq --equal to
  • -ne --not equal to
  • -lt -- less than
  • -le --less then or equal to
  • -gt --greater than
  • -ge --greater than or equal to
  • -like --like
  • -and
  • -or

PowerShell Example

  • Get-WmiObject -Class Win32_BIOS

PowerShell (Active Directory)

  • Get-WindowsFeature
  • Install-WindowsFeature AD-Domain-Services -IncludeAllSubFeature -IncludeManagementTools
  • Unlock-ADAccount -Identify user01
  • Set-ADAccountPassword -Identity user01
  • Set-ADUser -Identity user01 -ChangePasswordAtLogon $True
  • Get-EventLog -LogName "Security" | where {$_.eventID -eq 4648}
  • Get-Service -DisplayName "*Print*"

Save username and password into PowerShell script

  • Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File "D:\Passwd.txt"
  • $Username = "alphabook\Monitor"
  • $Password = Get-Content -Path "D:\Passwd.txt" | ConvertTo-SecureString
  • $Credential = New-Object -TypeName System.Management.Automation.PSCredential ($Username, $Password)
  • This method will not prevent others from using the password, but at least it is not in cleartext anymore

Script Library