Post on 21-Dec-2015
transcript
Powershell, .Net, Com, Wmi etc
Basic
Significant areas of support
Access to .Net Assemblies Support for COM objects Support for WMI objects XML A Powershell console for SQL Server Powershell 2.0 Features ….and more
What is “Dot” Net
It is the framework Microsoft designed to provide a consistent approach to application development and deployment. The .net framework is built on a set of foundational libraries. It is the basis of what is known as “the stack.”
.Net
Microsoft’s software framework that is now usually installed on most of its operating systems.
CLR:Common Language Runtime
Assemblies
Compiled form of classes Includes executables (files that end in
“.exe”) or Dynamic Link Libraries (files that end in “.dll”)
Actually contains one or more files with a manifest. An assembly is a logical grouping of modules.
Full name (not file name) or Meta Data: Module name Version number (allows multiple modules of the
same name) Public key token
Usefulness of meta data
Powershell
Assemblies already loaded Using additional .Net framework Loading your own assemblies
Directly using .Net Resources
Pre-loaded assemblies [AppDomain]::CurrentDomain.GetAssemblies()
[AppDomain]::CurrentDomain.GetAssemblies() | `ForEach-Object `
{ if ($_.GlobalAssemblyCache){ $_.Location.ToString().Split("\")[4]}}
SystemSystem.DrawingSystem.XmlSystem.ConfigurationSystem.Management.Automation
Microsoft.PowerShell.Commands.UtilityMicrosoft.PowerShell.ConsoleHostMicrosoft.PowerShell.Commands.ManagementMicrosoft.PowerShell.SecuritySystem.Web.Services
Examples using Static Members
[System.Console]::Clear()[System.Math]::Log10(100)$p = `
[System.Diagnostics.Process]::Start("notepad.exe");
$p | Get-Member[System.Int32]::Parse("254");
Testing your own DLL
#You must use the fully qualified path name[Reflection.Assembly]::LoadFile("C:/PSClass/LoanSupport.dll");[Bailey.LoanSupport.LoanSupport]::currentPrimeRate();$k = new-object Bailey.LoanSupport.LoanSupport("Pat", [decimal]120000, 10)$k.Payments()
Creating an Instance$client = New-Object System.Net.WebClient$url = "http://www.cs.calvin.edu/personnel/list"$data = $client.downloadstring($url)$data.Split("`n") | `
ForEach-Object { if ($_ -match ".*@calvin\.edu" ) {($_ -replace ".*mailto:", "") -replace "`".*","" } }
WMI Support
Get-WmiObject -listget-WmiObject win32_computersystem
XML
[xml] Support of XML elements to work through
document Start with the following command:
$vXml = [xml](Get-Content bookorder.xml)
Access Com Objects
COM : Common Object Model Microsoft’s technology to support
interoperation between software regardless of original language
Examples include MS Word, Excel While .Net is slowly replacing COM,
COM is supported by Powershell
http://www.computerperformance.co.uk/powershell/powershell_com.htm
Pulling text out MS Word
$tObj = New-Object -ComObject word.application#$tObj | Get-Member#after exploring let's play#$tObj.Visible = $true$tObj.Version$file = (dir C:\PSClass\small.docx).FullName$doc = $tObj.Documents.Open($file)$text = $doc.Content.TextWrite-Host "Word document had: `n" $text$tObj.Quit()
SQL Server Support
The application sqlps.exe is provided with SQL Server 2008
It can be installed on a client and support SQL Server 2005
Check to see if it is supporting 2.0, if not you can add its functionality to your 2.0 environment.
Adding SQL Server Support
Go to this page: http://www.microsoft.com/downloads/details.aspx?FamilyID=228de03f-3b5a-428a-923f-58a033d316e1&DisplayLang=en
Download and install in the following order:SQLSysClrTypes.msiSharedManagementObjects.msiPowerShellTools.msi
In C:\Program Files\Microsoft SQL Server\100\Tools\Binn\Redist you will find SQLPS.exe. You will also find the files the DLL's for the provider.
Enter the following commands to install the providers (cmdlet and PSDrive) and then add them C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.exe Microsoft.SqlServer.Management.PSSnapins.dll C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.exe Microsoft.SqlServer.Management.PSProvider.dllAdd-PSSnapin SqlServerProviderSnapinget-pssnapin -reg #look for the names of the providers here. Then use the Add-PSSnapin as belowAdd-PSSnapin SqlServerCmdletSnapin100Add-PSSnapin SqlServerProviderSnapin100
Job control in Version 2.0
Start-job : places a job in the background which is not interactive with the console
Get-job : gets information about a job.
Receive-job : Displays all the output that would have gone to the console.
fan-in / fan-out
Tutorial at
http://powershell.com/cs/blogs/tobias/archive/2009/01/17/xml-part-1-playing-with-rss-feeds-and-xml-content.aspx