"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
Introduction to windows power shell in sharepoint 2010
1. Introduction to Windows PowerShell in SharePoint 2010 Gia Duy Mai Solutions Assistant Lead Bamboo Solutions
2. Agenda Introduction to PowerShell Why PowerShell for SharePoint? When use PowerShell? PowerShellvs STSADM Getting started with PowerShell Controlling PowerShell with variables, loops, logic, and functions Creating your own PowerShell scripts Scheduling your PowerShell scripts Demo Q&A
10. Pipelining allow one Cmdlet to return an object as input to anotherC#: X = Class.Method(); Y = OtherClass.Method(X); Z = OtherOtherClass.Method(Y) PowerShell: Class.Method() | OtherClass.Method() | OtherOtherClass.Method()
13. PowerShell vs STSADM - Performance Performance benefits for batch operations STSADM Batch Operation PowerShell Cmdlets in Batch Start Thread New STSADM Execution Start Thread Load DLLs Start logging Repeat Start Logging Execute Command Repeat Execute Command Stop logging Stop Logging Stop Thread Close STSADM & Thread
14. PowerShell vs STSADM - Performance PowerShell “Wrapped” STSADM to enable a feature on every SPSite Same command in SharePoint 2010 PowerShell: $rawdata = stsadm.exe -o enumsites -url $WebAppURL $sitexml = [XML]$rawdata $sitesxml.Sites | foreach-object{ $_.Site } | foreach-object{ stsadm -o activatefeature -url$_.url -filename $featureFileName if( $lastexitcode -ne 0 ){ Write-Host “Failure:$lastexitcode " -Fore Red; $failure = $true; } } 12+ Hours (5300 SPSites) 30 Minutes (5300 SPSites) Get-SPSite –Limit ALL –WebApplication $WebAppNameorUrl |%{ Enable-SPFeature$FeatureIdOrName –url $_.Url }
15.
16.
17. Getting Started with PowerShell (1) Execute a Cmdlet (2) Filter results using where clause (3) Add formatting instructions (4) Redirect output to new text file
20. Variables in PowerShell A variable is a place for you to store some value so you can revisit it later. All variables that you create begin with $ Ex: $my_first_variable= “Hello World” Variables Types Loosely typed variables $f = “bar” #implicitly typed as string $ary= 4,2,5,2 #typed as object[] Strongly typed variables [string]$f = “bar” Enhanced Types variables [xml]$d = “<a><b><c>c stuff 1</c><c>c stuff 2</c></b></a>”
21. Looping in PowerShell Classic PowerShell Loop - For Loop $i = 10 For($j = 1; $j-lt $i; $j++){ Write-Host ”$j”; } - ForeachLoop $NumArray= (1,2,3,4,5,6,7,8,9,10)Foreach($Item in $NumArray) {$Item * 3} - While Loop $i =8 While ($i-le 96) { $i+=8; Write-Host$i; } - Do Until Loop $i = 7; do { $i+=7; Write-Host$i; } until ($i-gt 85) ForeachLoop with Pipeline Get-SPSite -Limit All | foreach { Remove-SPSite -identity $_.Id } $_ and is a variable that represents the current object
22. Adding logic in PowerShell Logic is what enables you to do something at a specific point in the program. Statements If/ ElseIf/ Else Switch While Comparison Operators -eq/-ne -lt / -gt -le / -ge -like / -notlike -contains/-notcontains -replace …
32. Tools Notepad PowerShell Command Windows PowerShell Integrated Scripting Environment (ISE) PowerGUI - Download from powergui.org
33. How to Create and Run a PowerShell Script The steps to create a script follow: Create the script in Notepad and save with a .PS1 file extension (hello.ps1). Run the script by entering the full path to the script (c:criptsello.ps1), or if it’s in the current directory (.ello.ps1). If the path to the script contains a space, enclose the full path in quotation marks and prefix the entire thing with an ampersand ("C:y scriptsello.ps1").
34. Scripts and Execution Policy Local execution policy must be configured Scripts will not execute under default execution policy of restricted Scripts can execute under execution policy of unrestricted. Scripts that are not signed result in prompting user for permission to execute. Scripts can execute under execution policy of bypass. This mode suppresses prompting user for permission to execute.
35. Editing Scripts using PowerShell ISE Supports Color-coding, IntelliSense and debugging Writing scripts here Run cmdlets or functions here Results
37. The SharePoint PowerShell Snapin Explicitly load SharePoint PowerShell snap-in from console or script Implicitly load SharePoint PowerShell snap-in by using SharePoint 2010 Management Shell Begin using SharePoint Cmdlets
38. Scheduling Your Scripts Sometimes commands need to be scheduled to run automatically PowerShell Scripts (.ps1) can be scheduled using Task Scheduler Just put the above command into a .bat or .cmd file and schedule it like you would normally schedule a script to be run with Task Scheduler.
39. Demo – Series Of Scripts Run hello.ps1 script How to use PowerShell to create sites. How to use PowerShell to populate items in a List for testing, adding attachments How to use PowerShell to copy items from one list to another. How to use PowerShell to upload documents to a document libraries. How to use PowerShell to backup sites.
What => Why => Who => Where => When => How
What is Shell?Working with a command-line interface, often called a shell, can be a daunting task. If you grew up working with DOS or Unix-based system, you'll feel right at home with PowerShell.If you're used to a computer that always presents you with a Graphical User Interface(GUI), then typing your commands into PowerShell may seem like going backward in time. After you become familiar with a command-line user interface, however, don't be surprised if you get fond of the speed, flexibility, and robustness you find there; you may get a hooked.What is PowerShell?Unix-like shellObject oriented.NETCommand lineScripting languageA single mindset for management of many different systems (e.g.: AD, Exchange, Windows)Extensible framework that can be embedded inside other applications
Over the last few years, PowerShell scripting has begun to replace older DOS-style batch files and VBScript files as the preferred way to manage and automate administrative tasksCmdlets are callable functions Pipelining allow one Cmdlet to return an object as input to anotherPowerShell includes formatting features to display output using lists or tablesPowerShell is based on a provider-based model based on Snap-insSharePoint support added through Microsoft.SharePoint.Powershell..Snapin: Group of PowerShell functionality
Previous version of WSS relies on STSADM.EXEUsed for command line administration and scriptingPSDrivesFileSystem Provider- access to the file systemMap to key SharePoint directoriesSTSADM via PowerShellExecute operations via the PowerShell hostIntegrate STSADM operations into the pipelineScripting via Object ModelAccess SharePoint objectsCreate reusable SharePoint-aware commandsCustom DevelopmentCmdLetsProvidersHost ApplicationsAutomated build and deploy: => Save time for Deploy/Test Cycle phases
This is expensive! The first operation essentially iterates over every site twice—AND it has to start and stop the stsadm logging/process/etc for each feature activation!!The first took 12+ hours and NEVER FINISHED.The latter took roughly 30 minutes.
Automation: using Task Scheduler for running Backup and Restore.Development:Testing: usingPowerShell for stress test
The first example shows calling a Cmdlet that takes no parametersThe second example adds filtering by adding a Where commandWhere-Object {$_.name –eq "F*"}The syntax $_ refers to the object in question$_.name refers to the object's name property-eq is the operator for equalsThe third example adds in formatting instructionsThe last example redirects output so it is stored in a new text files.
Get-Help cmdlet
You can use aliases to save yourself the time and effort of typing cmdletsthat you use frequently. If you want to see all the aliases that PowerShell has mapped to specific cmdlets, use the Get-Alias cmdlet.
By default, PowerShell does not allow script to runAdministrator must change execution policy to enable script executionExecution Policy Settingsrestricted (default) – scripts prohibited from executingunrestricted - scripts can execute. Scripts that are signed can run with user interaction. Scripts that are not signed result in prompting user for permission to execute.bypass (developer mode) – scripts can execute and user interaction is suppressed.
With the initial release of PowerShell, administrators and developers usually resorted to writing scripts with NotePad. PowerShell 2 provides a nice development environment for writing PowerShell scripts named the PowerShell Integrated Scripting Environment (ISE). When it comes t writing complex scripts with control of flow logic, it is really nice to be able to debug and single step through your code.
Thisexample shows a PowerShell script which uses the New-SPSite cmdlet to create a new site. You should observe that the New-SPSite cmdlet returns a SPSite object which is then used to access the SPWeb object for the top-level site.
SharePoint Server 2010 adds many Cmdlets for SharePoint administration through a Snap-in provider named Microsoft.SharePoint.PowerShell. The SharePoint cmdlets cannot be called until the SharePoint snap-in has been loaded.There are two common ways to load the SharePoint snap-inCall the Add-PSSnapin cmdlet and pass a parameter with the snap-in nameLaunch the PowerShell console using SharePoint Management Console. This link points to an XML file that loads the SharePoint snap-in in a declarative fashion.