DNK Gif

Dot Net Knowledge

Labels

Friday, 22 April 2016

Call .NET object or methods from PowerShell scripts

 

*********************Call System Assembly Class Object/Static Class *****************

PS C:\Users\837853> Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show("Hello World");
OK

***********************Call Customized .NET DLL Class Object**********************

PS C:\Users\837853> [System.Reflection.Assembly]::LoadFile("C:\Users\837853\Umesh\Development\Test\PowerShellExecution\W
indowsFormPowerShellExecution\WindowsFormPowerShellExecution\bin\Debug\WindowsFormPowerShellExecution.dll"); $classname=
New-Object MyClass; $classname.ShowMessage()

Exception calling "LoadFile" with "1" argument(s): "This assembly is built by a runtime newer than the currently loaded
 runtime and cannot be loaded. (Exception from HRESULT: 0x8013101B)"
At line:1 char:39
+ [System.Reflection.Assembly]::LoadFile <<<< ("C:\Users\837853\Umesh\Development\Test\PowerShellExecution\WindowsFormP
owerShellExecution\WindowsFormPowerShellExecution\bin\Debug\WindowsFormPowerShellExecution.dll"); $classname=New-Object
 MyClass; $classname.ShowMessage()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
New-Object : Cannot find type [MyClass]: make sure the assembly containing this type is loaded.
At line:1 char:231
+ [System.Reflection.Assembly]::LoadFile("C:\Users\837853\Umesh\Development\Test\PowerShellExecution\WindowsFormPowerSh
ellExecution\WindowsFormPowerShellExecution\bin\Debug\WindowsFormPowerShellExecution.dll"); $classname=New-Object <<<<
 MyClass; $classname.ShowMessage()
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
You cannot call a method on a null-valued expression.
At line:1 char:263
+ [System.Reflection.Assembly]::LoadFile("C:\Users\837853\Umesh\Development\Test\PowerShellExecution\WindowsFormPowerSh
ellExecution\WindowsFormPowerShellExecution\bin\Debug\WindowsFormPowerShellExecution.dll"); $classname=New-Object MyCla
ss; $classname.ShowMessage <<<< ()
    + CategoryInfo          : InvalidOperation: (ShowMessage:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

******************After Changing the .NET DLL Framework to V2.0*******************

PS C:\Users\837853> [System.Reflection.Assembly]::LoadFile("C:\Users\837853\Umesh\Development\Test\PowerShellExecution\W
indowsFormPowerShellExecution\WindowsFormPowerShellExecution\bin\Debug\WindowsFormPowerShellExecution.dll");  $classname
=New-Object WindowsFormPowerShellExecution.MyClass; $classname.ShowMessage()
GAC    Version        Location
---    -------        --------
False  v2.0.50727     C:\Users\837853\Umesh\Development\Test\PowerShellExecution\WindowsFormPowerShellExecution\Wind...

PS C:\Users\837853> [System.Reflection.Assembly]::LoadFile("C:\Users\837853\Umesh\Development\Test\PowerShellExecution\W
indowsFormPowerShellExecution\WindowsFormPowerShellExecution\bin\Debug\WindowsFormPowerShellExecution.dll");  $classname
=New-Object WindowsFormPowerShellExecution.MyClass; $classname.Welcome("Avik",456988);
GAC    Version        Location
---    -------        --------
False  v2.0.50727     C:\Users\837853\Umesh\Development\Test\PowerShellExecution\WindowsFormPowerShellExecution\Wind...
Returned value: Hello Avik Id: 456988

***********************Check Powershell Vresion in System***************************

PS C:\Users\837853> $PsVersionTable
Name                           Value
----                           -----
CLRVersion                     2.0.50727.5485
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

PS C:\Users\837853> Get-WmiObject -Class Win32_OperatingSystem | Format-Table Caption, ServicePackMajorVersion -AutoSize

Caption                         ServicePackMajorVersion
-------                         -----------------------
Microsoft Windows 7 Enterprise                        1


Check .NET Frame work V4.5 installed or not which is a pre-requisite for PowerShell Version-4 or  Windows Management Framework 4.0


PS C:\Users\837853> (Get-ItemProperty -Path 'HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full' -ErrorAction Sile
ntlyContinue).Version -like '4.5*'
True


******************To Update Powershell V2.0 to Powershell V4.0 ********************


******************************Check for Powershell Version *************************

PS C:\Users\837853> $PsVersionTable
Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.18408
BuildVersion                   6.3.9600.16406
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2

No comments:

Post a Comment