2. Agenda Introduction to PowerShell Lifecycle Language Usability PowerShell with SQL PowerShell difficulties
3. Introduction Command-line shell with an associated scripting language for automation. Heavily integrated with .NET Framework. Provides full access to COM and WMI allowing local and remote administration of Windows Systems. Commands referred to as cmdlets - .NET Classes allowing implementation of specific operations. Utilized by most Microsoft applications and Systems (Windows Server, MSSQL, Exchange Server, SCOM, SCDMP etc.
4. Lifecycle Code name “Monad” – Sept 2003 Version 1.0 released 2006 for Windows XP SP2/SP3 Version 2.0 Integrated with Windows 7 and Window Server 2008 R2. Downloadable for earlier versions of Windows. Can execute four kinds of commands. Cmdlets – specialized commands implementing specific functions. PowerShell scripts – suffixed as .ps1 PowerShell functions Executable programs
5. Language .Net Aspect Property -- can get Method -- can do CMDLETS Integrated with command Line Built with WMI in mind. Language format – verb-noun Arranged in hard drives Variables
6. Scripting Getting RAM from Local Machine Get-WmiObject Win32_ComputerSystem Getting RAM from Local Machine Option ExplicitDim objWMIService, objComputer, colComputerDim strLogonUser, strComputer strComputer = "." Set objWMIService = GetObject("winmgmts:" _& "{impersonationLevel=impersonate}!" _ & strComputer & "ootimv2") Set colComputer = objWMIService.ExecQuery _("Select * from Win32_ComputerSystem") For Each objComputer in colComputerWscript.Echo "System Name: " & objComputer.Name _& vbCr & "Total RAM " & objComputer.TotalPhysicalMemoryNext WScript.Quit
7. Usability - basics Functions like DOS Store everything as a drive psdrive Variables $varname Displaying you variables after assigning it $dt = Get-Date | $dt – Wont work $dt = Get-Date ; $dt Can pass doss command to PowerShell $varname = "Localhost“ Piping and Pipeline Get-Service | Sort-Object -Property ServiceType | Format-Table name, ServiceType, status, CanStop, -Auto Filtering Get-Service | Where {$_.Status -eq "Stopped"} | ft name, status, CanStop -AutoSize Looping For-each {$_.} -- current iteration of the loop
8. Usability - basics Get-Member Using Alias New-Alias myalias Get-Service Reference previously used commands get-history Signing Scripts – configured to run in most secure mode (Restricted) AllSigning, RemoreSigned, UnRestricted HELP
9. PowerShell & SQL SQLPS – accessible from any where in SSMS dir| Get-Member From a table dir | ft Name, RowCount, State, Owner, Triggers SQL Server Management Objects (SMO) – Objects collections for SQL Server Management. [reflection.assembly]::LoadWithPartialName("Microsoft.SQLServer.SMO") Assign variables